Skip to content

[task]: attempt to improve debugging info for AnyWorkflowAction #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 54 additions & 4 deletions Workflow/Sources/WorkflowAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ public protocol WorkflowAction<WorkflowType> {

/// A type-erased workflow action.
///
/// The `AnyWorkflowAction` type forwards `apply` to an underlying workflow action, hiding its specific underlying type,
/// or to a closure that implements the `apply` logic.
/// The `AnyWorkflowAction` type forwards `apply` to an underlying workflow action, hiding its specific underlying type.
public struct AnyWorkflowAction<WorkflowType: Workflow>: WorkflowAction {
private let _apply: (inout WorkflowType.State) -> WorkflowType.Output?

/// The underlying type-erased `WorkflowAction`
public let base: Any

/// Creates a type-erased workflow action that wraps the given instance.
///
/// - Parameter base: A workflow action to wrap.
Expand All @@ -46,13 +48,30 @@ public struct AnyWorkflowAction<WorkflowType: Workflow>: WorkflowAction {
return
}
self._apply = { return base.apply(toState: &$0) }
self.base = base
}

/// Creates a type-erased workflow action with the given `apply` implementation.
///
/// - Parameter apply: the apply function for the resulting action.
public init(_ apply: @escaping (inout WorkflowType.State) -> WorkflowType.Output?) {
self._apply = apply
public init(
_ apply: @escaping (inout WorkflowType.State) -> WorkflowType.Output?,
fileID: StaticString = #fileID,
line: UInt = #line
) {
let closureAction = ClosureAction<WorkflowType>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we expose a way for consumers who receive AnyWorkflowAction to know if this is wrapping a closure vs. a real action? could maybe make ClosureAction public to achieve that

let action: AnyWorkflowAction<SomeWorkflow> = ...

if action.base is ClosureAction<SomeWorkflow> {

}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah we can do that. do you think full access to the underlying type is needed? or would a flag suffice?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general my default is "provide more access" but I know as the SDK author its the opposite for you :p for now I suppose a flag would work

_apply: apply,
fileID: fileID,
line: line
)
self.init(closureAction: closureAction)
}

/// Private initializer forwarded to via `init(_ apply:...)`
/// - Parameter closureAction: The `ClosureAction` wrapping the underlying `apply` closure.
fileprivate init(closureAction: ClosureAction<WorkflowType>) {
self._apply = closureAction.apply(toState:)
self.base = closureAction
}

public func apply(toState state: inout WorkflowType.State) -> WorkflowType.Output? {
Expand All @@ -78,3 +97,34 @@ extension AnyWorkflowAction {
}
}
}

// MARK: Closure Action

/// A `WorkflowAction` that wraps an `apply(...)` implementation defined by a closure.
/// Mainly used to provide more useful debugging/telemetry information for `AnyWorkflow` instances
/// defined via a closure.
struct ClosureAction<WorkflowType: Workflow>: WorkflowAction {
private let _apply: (inout WorkflowType.State) -> WorkflowType.Output?
let fileID: StaticString
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice touch!

let line: UInt

init(
_apply: @escaping (inout WorkflowType.State) -> WorkflowType.Output?,
fileID: StaticString,
line: UInt
) {
self._apply = _apply
self.fileID = fileID
self.line = line
}

func apply(toState state: inout WorkflowType.State) -> WorkflowType.Output? {
_apply(&state)
}
}

extension ClosureAction: CustomStringConvertible {
var description: String {
"\(Self.self)(fileID: \(fileID), line: \(line))"
}
}
117 changes: 117 additions & 0 deletions Workflow/Tests/AnyWorkflowActionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2023 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import XCTest
@testable import Workflow

final class AnyWorkflowActionTests: XCTestCase {
func testRetainsBaseActionTypeInfo() {
let action = ExampleAction()
let erased = AnyWorkflowAction(action)

XCTAssertEqual(action, erased.base as? ExampleAction)
}

func testRetainsClosureActionTypeInfo() throws {
do {
let erased = AnyWorkflowAction<ExampleWorkflow> { _ in
nil
}

XCTAssertNotNil(erased.base as? ClosureAction<ExampleWorkflow>)
}

do {
let fileID: StaticString = #fileID
// must match line # the initializer is on
let line: UInt = #line; let erased = AnyWorkflowAction<ExampleWorkflow> { _ in
nil
}

let closureAction = try XCTUnwrap(erased.base as? ClosureAction<ExampleWorkflow>)
XCTAssertEqual("\(closureAction.fileID)", "\(fileID)")
XCTAssertEqual(closureAction.line, line)
}
}

func testMultipleErasure() {
// standard init
do {
let action = ExampleAction()
let erasedOnce = AnyWorkflowAction(action)
let erasedTwice = AnyWorkflowAction(erasedOnce)

XCTAssertEqual(
erasedOnce.base as? ExampleAction,
erasedTwice.base as? ExampleAction
)
}

// closure init
do {
let action = AnyWorkflowAction<ExampleWorkflow> { _ in nil }
let erasedAgain = AnyWorkflowAction(action)

XCTAssertEqual(
"\(action.base.self)",
"\(erasedAgain.base.self)"
)
}
}

func testApplyForwarding() {
var log: [String] = []
let action = ObservableExampleAction {
log.append("action invoked")
}

let erased = AnyWorkflowAction(action)

XCTAssertEqual(log, [])

var state: Void = ()
_ = erased.apply(toState: &state)

XCTAssertEqual(log, ["action invoked"])
}
}

private struct ExampleWorkflow: Workflow {
typealias State = Void
typealias Output = Never
typealias Rendering = Void

func render(state: Void, context: RenderContext<ExampleWorkflow>) {}
}

private struct ExampleAction: WorkflowAction, Equatable {
typealias WorkflowType = ExampleWorkflow

func apply(toState state: inout WorkflowType.State) -> WorkflowType.Output? {
return nil
}
}

private struct ObservableExampleAction: WorkflowAction {
typealias WorkflowType = ExampleWorkflow

var block: () -> Void = {}

func apply(toState state: inout WorkflowType.State) -> WorkflowType.Output? {
block()
return nil
}
}
6 changes: 3 additions & 3 deletions WorkflowUI/Sources/Screen/AnyScreen/AnyScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ import UIKit

public struct AnyScreen: Screen {
/// The original screen, retained for debugging
public let wrappedScreen: Screen
public let base: Screen

public init<T: Screen>(_ screen: T) {
if let anyScreen = screen as? AnyScreen {
self = anyScreen
return
}
self.wrappedScreen = screen
self.base = screen
}

public func viewControllerDescription(environment: ViewEnvironment) -> ViewControllerDescription {
return wrappedScreen.viewControllerDescription(environment: environment)
return base.viewControllerDescription(environment: environment)
}
}

Expand Down