@@ -27,14 +27,22 @@ public protocol WorkflowAction<WorkflowType> {
27
27
///
28
28
/// - Returns: An optional output event for the workflow. If an output event is returned, it will be passed up
29
29
/// the workflow hierarchy to this workflow's parent.
30
- func apply( toState state: inout WorkflowType . State ) -> WorkflowType . Output ?
30
+ func apply(
31
+ toState state: inout WorkflowType . State ,
32
+ context: ApplyContext < WorkflowType >
33
+ ) -> WorkflowType . Output ?
34
+ }
35
+
36
+ extension WorkflowAction {
37
+ /// Closure type signature matching `WorkflowAction`'s `apply()` method.
38
+ public typealias ActionApplyClosure = ( inout WorkflowType . State , ApplyContext < WorkflowType > ) -> WorkflowType . Output ?
31
39
}
32
40
33
41
/// A type-erased workflow action.
34
42
///
35
43
/// The `AnyWorkflowAction` type forwards `apply` to an underlying workflow action, hiding its specific underlying type.
36
44
public struct AnyWorkflowAction < WorkflowType: Workflow > : WorkflowAction {
37
- private let _apply : ( inout WorkflowType . State ) -> WorkflowType . Output ?
45
+ private let _apply : ActionApplyClosure
38
46
39
47
/// The underlying type-erased `WorkflowAction`
40
48
public let base : Any
@@ -50,7 +58,9 @@ public struct AnyWorkflowAction<WorkflowType: Workflow>: WorkflowAction {
50
58
self = anyEvent
51
59
return
52
60
}
53
- self . _apply = { base. apply ( toState: & $0) }
61
+ self . _apply = {
62
+ base. apply ( toState: & $0, context: $1)
63
+ }
54
64
self . base = base
55
65
self . isClosureBased = false
56
66
}
@@ -59,7 +69,7 @@ public struct AnyWorkflowAction<WorkflowType: Workflow>: WorkflowAction {
59
69
///
60
70
/// - Parameter apply: the apply function for the resulting action.
61
71
public init (
62
- _ apply: @escaping ( inout WorkflowType . State ) -> WorkflowType . Output ? ,
72
+ _ apply: @escaping ActionApplyClosure ,
63
73
fileID: StaticString = #fileID,
64
74
line: UInt = #line
65
75
) {
@@ -71,16 +81,35 @@ public struct AnyWorkflowAction<WorkflowType: Workflow>: WorkflowAction {
71
81
self . init ( closureAction: closureAction)
72
82
}
73
83
84
+ /// Creates a type-erased workflow action with the given `apply` implementation.
85
+ ///
86
+ /// - Parameter apply: the apply function for the resulting action.
87
+ @_disfavoredOverload
88
+ public init (
89
+ _ apply: @escaping ( inout WorkflowType . State ) -> WorkflowType . Output ? ,
90
+ fileID: StaticString = #fileID,
91
+ line: UInt = #line
92
+ ) {
93
+ self . init (
94
+ { state, _ in apply ( & state) } ,
95
+ fileID: fileID,
96
+ line: line
97
+ )
98
+ }
99
+
74
100
/// Private initializer forwarded to via `init(_ apply:...)`
75
101
/// - Parameter closureAction: The `ClosureAction` wrapping the underlying `apply` closure.
76
102
fileprivate init ( closureAction: ClosureAction < WorkflowType > ) {
77
- self . _apply = closureAction. apply ( toState: )
103
+ self . _apply = closureAction. apply ( toState: context : )
78
104
self . base = closureAction
79
105
self . isClosureBased = true
80
106
}
81
107
82
- public func apply( toState state: inout WorkflowType . State ) -> WorkflowType . Output ? {
83
- _apply ( & state)
108
+ public func apply(
109
+ toState state: inout WorkflowType . State ,
110
+ context: ApplyContext < WorkflowType >
111
+ ) -> WorkflowType . Output ? {
112
+ _apply ( & state, context)
84
113
}
85
114
}
86
115
@@ -89,15 +118,15 @@ extension AnyWorkflowAction {
89
118
///
90
119
/// - Parameter output: The output event to send when this action is applied.
91
120
public init ( sendingOutput output: WorkflowType . Output ) {
92
- self = AnyWorkflowAction { state in
121
+ self = AnyWorkflowAction { _ , _ in
93
122
output
94
123
}
95
124
}
96
125
97
126
/// Creates a type-erased workflow action that does nothing (it leaves state unchanged and does not emit an output
98
127
/// event).
99
128
public static var noAction : AnyWorkflowAction < WorkflowType > {
100
- AnyWorkflowAction { state in
129
+ AnyWorkflowAction { _ , _ in
101
130
nil
102
131
}
103
132
}
@@ -109,12 +138,12 @@ extension AnyWorkflowAction {
109
138
/// Mainly used to provide more useful debugging/telemetry information for `AnyWorkflow` instances
110
139
/// defined via a closure.
111
140
struct ClosureAction < WorkflowType: Workflow > : WorkflowAction {
112
- private let _apply : ( inout WorkflowType . State ) -> WorkflowType . Output ?
141
+ private let _apply : ActionApplyClosure
113
142
let fileID : StaticString
114
143
let line : UInt
115
144
116
145
init (
117
- _apply: @escaping ( inout WorkflowType . State ) -> WorkflowType . Output ? ,
146
+ _apply: @escaping ActionApplyClosure ,
118
147
fileID: StaticString ,
119
148
line: UInt
120
149
) {
@@ -123,8 +152,11 @@ struct ClosureAction<WorkflowType: Workflow>: WorkflowAction {
123
152
self . line = line
124
153
}
125
154
126
- func apply( toState state: inout WorkflowType . State ) -> WorkflowType . Output ? {
127
- _apply ( & state)
155
+ func apply(
156
+ toState state: inout WorkflowType . State ,
157
+ context: ApplyContext < WorkflowType >
158
+ ) -> WorkflowType . Output ? {
159
+ _apply ( & state, context)
128
160
}
129
161
}
130
162
0 commit comments