Skip to content

Commit f9f5494

Browse files
authored
[chore]: refactor some internal actions to use existential any (#190)
since we now have access to primary associated types, there are a couple places internally where it likely makes sense to leverage the language's 'existential' boxing facilities over our own. to start, we'll update a couple places that pass an `AnyWorkflowAction<T>` that can now be replaced with `any WorkflowAction<T>`.
1 parent 3fec0ee commit f9f5494

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

Workflow/Sources/SubtreeManager.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ extension WorkflowNode {
114114

115115
extension WorkflowNode.SubtreeManager {
116116
enum Output {
117-
case update(AnyWorkflowAction<WorkflowType>, source: WorkflowUpdateDebugInfo.Source)
117+
case update(any WorkflowAction<WorkflowType>, source: WorkflowUpdateDebugInfo.Source)
118118
case childDidUpdate(WorkflowUpdateDebugInfo)
119119
}
120120
}
@@ -175,7 +175,7 @@ extension WorkflowNode.SubtreeManager {
175175
/// Update the existing child
176176
existing.update(
177177
workflow: workflow,
178-
outputMap: { AnyWorkflowAction(outputMap($0)) },
178+
outputMap: { outputMap($0) },
179179
eventPipe: eventPipe
180180
)
181181
child = existing
@@ -184,7 +184,7 @@ extension WorkflowNode.SubtreeManager {
184184
/// This spins up a new workflow node, etc to host the newly created child.
185185
child = ChildWorkflow<Child>(
186186
workflow: workflow,
187-
outputMap: { AnyWorkflowAction(outputMap($0)) },
187+
outputMap: { outputMap($0) },
188188
eventPipe: eventPipe
189189
)
190190
}
@@ -272,7 +272,7 @@ extension WorkflowNode.SubtreeManager {
272272

273273
fileprivate final class ReusableSink<Action: WorkflowAction>: AnyReusableSink where Action.WorkflowType == WorkflowType {
274274
func handle(action: Action) {
275-
let output = Output.update(AnyWorkflowAction(action), source: .external)
275+
let output = Output.update(action, source: .external)
276276

277277
if case .pending = eventPipe.validationState {
278278
// Workflow is currently processing an `event`.
@@ -387,9 +387,13 @@ extension WorkflowNode.SubtreeManager {
387387

388388
fileprivate final class ChildWorkflow<W: Workflow>: AnyChildWorkflow {
389389
private let node: WorkflowNode<W>
390-
private var outputMap: (W.Output) -> AnyWorkflowAction<WorkflowType>
390+
private var outputMap: (W.Output) -> any WorkflowAction<WorkflowType>
391391

392-
init(workflow: W, outputMap: @escaping (W.Output) -> AnyWorkflowAction<WorkflowType>, eventPipe: EventPipe) {
392+
init(
393+
workflow: W,
394+
outputMap: @escaping (W.Output) -> any WorkflowAction<WorkflowType>,
395+
eventPipe: EventPipe
396+
) {
393397
self.outputMap = outputMap
394398
self.node = WorkflowNode<W>(workflow: workflow)
395399

@@ -408,7 +412,11 @@ extension WorkflowNode.SubtreeManager {
408412
return node.render(isRootNode: false)
409413
}
410414

411-
func update(workflow: W, outputMap: @escaping (W.Output) -> AnyWorkflowAction<WorkflowType>, eventPipe: EventPipe) {
415+
func update(
416+
workflow: W,
417+
outputMap: @escaping (W.Output) -> any WorkflowAction<WorkflowType>,
418+
eventPipe: EventPipe
419+
) {
412420
self.outputMap = outputMap
413421
self.eventPipe = eventPipe
414422
node.update(workflow: workflow)

Workflow/Sources/WorkflowNode.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,19 @@ final class WorkflowNode<WorkflowType: Workflow> {
4848
let output: Output
4949

5050
switch subtreeOutput {
51-
case .update(let event, let source):
52-
/// Apply the update to the current state
53-
let outputEvent = event.apply(toState: &state)
51+
case .update(let action, let source):
52+
53+
/// 'Opens' the existential `any WorkflowAction<WorkflowType>` value
54+
/// allowing the underlying conformance to be applied to the Workflow's State
55+
func openAndApply<A: WorkflowAction>(
56+
_ action: A,
57+
to state: inout WorkflowType.State
58+
) -> WorkflowType.Output? where A.WorkflowType == WorkflowType {
59+
/// Apply the update to the current state
60+
action.apply(toState: &state)
61+
}
62+
63+
let outputEvent = openAndApply(action, to: &state)
5464

5565
/// Finally, we tell the outside world that our state has changed (including an output event if it exists).
5666
output = Output(

Workflow/Tests/SubtreeManagerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ final class SubtreeManagerTests: XCTestCase {
7272
manager.onUpdate = {
7373
switch $0 {
7474
case .update(let event, _):
75-
events.append(event)
75+
events.append(AnyWorkflowAction(event))
7676
default:
7777
break
7878
}

0 commit comments

Comments
 (0)