Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 0 additions & 4 deletions Workflow/Sources/WorkflowNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ final class WorkflowNode<WorkflowType: Workflow> {
/// allowing the underlying conformance to be applied to the Workflow's State
let outputEvent = openAndApply(
action,
to: &state,
isExternal: source == .external
)

Expand Down Expand Up @@ -182,13 +181,10 @@ private extension WorkflowNode {
/// Applies an appropriate `WorkflowAction` to advance the underlying Workflow `State`
/// - Parameters:
/// - action: The `WorkflowAction` to apply
/// - state: The `State` to which the action will be applied
/// - observerInfo: Optional observation info to notify registered `WorkflowObserver`s
/// - isExternal: Whether the handled action came from the 'outside world' vs being bubbled up from a child node
/// - Returns: An optional `Output` produced by the action application
func openAndApply<A: WorkflowAction>(
_ action: A,
to state: inout WorkflowType.State,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the diff makes this a bit confusing, but removing this parameter just means that self.state is no longer shadowed in this method

isExternal: Bool
) -> WorkflowType.Output? where A.WorkflowType == WorkflowType {
let output: WorkflowType.Output?
Expand Down
58 changes: 58 additions & 0 deletions Workflow/Tests/WorkflowObserverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,35 @@ final class WorkflowObserverTests: XCTestCase {
XCTAssertEqual(actions, [.toggle])
}

// added to exercise a simultaneous memory access bug when observing action
// application for some Workflow's where State == Void
func test_didReceiveActionCallbacks_voidState() {
var actions: [VoidStateWorkflow.Action] = []
observer.onDidReceiveAction = { action, workflow, session in
guard let action = action as? VoidStateWorkflow.Action else {
XCTFail("unexpected action. expecting \(VoidStateWorkflow.Action.self), got \(type(of: action))")
return
}

actions.append(action)
}

let node = WorkflowNode(
workflow: VoidStateWorkflow(),
parentSession: nil,
observer: observer
)

let rendering = node.render()
node.enableEvents()

XCTAssertEqual(actions, [])

rendering.onTapped()

XCTAssertEqual(actions, [.actionValue])
}

func test_didReceiveActionCallbacks_onlyInvokedForExternalEvents() {
var actionsReceived: [InjectableWorkflow.Action] = []
observer.onDidReceiveAction = { action, workflow, session in
Expand Down Expand Up @@ -687,3 +716,32 @@ private struct DefaultObservers: ObserversInterceptor {
observers + initialObservers
}
}

// MARK: Void State Observation Crash Example

/// Example that cause a memory exclusivity violation in prior observation code
private struct VoidStateWorkflow: Workflow {
typealias State = Void
typealias Output = Never

enum Action: WorkflowAction {
typealias WorkflowType = VoidStateWorkflow

case actionValue

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

struct Rendering {
var onTapped: () -> Void
}

func render(state: VoidStateWorkflow.State, context: RenderContext<VoidStateWorkflow>) -> Rendering {
let sink = context.makeSink(of: VoidStateWorkflow.Action.self)
return Rendering(
onTapped: { sink.send(.actionValue) }
)
}
}