-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathWorkflowObserver.swift
371 lines (323 loc) · 13 KB
/
WorkflowObserver.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Copyright 2022 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 Foundation
// MARK: - WorkflowObserver
/// The `WorkflowObserver` protocol provides an interface to receive updates during the runtime's
/// execution loop. All requirements are optional, and no-ops are provided by default.
public protocol WorkflowObserver {
/// Indicates the start of a `WorkflowSession`, which tracks the life of the underlying `WorkflowNode` used to provide renderings for a given `Workflow` type and rendering key.
/// - Parameter session: The `WorkflowSession` that began.
func sessionDidBegin(
_ session: WorkflowSession
)
/// Marks the end of a `WorkflowSession`, indicating that the corresponding `WorkflowNode` has been removed from the tree of Workflows.
/// - Parameter session: The `WorkflowSession` that ended.
func sessionDidEnd(
_ session: WorkflowSession
)
/// Indicates a `Workflow` produced its initial state value.
/// - Parameters:
/// - workflow: The `Workflow` that just produced its initial state.
/// - initialState: The `State` that was created.
/// - session: The `WorkflowSession` corresponding to the backing `WorkflowNode`
func workflowDidMakeInitialState<WorkflowType: Workflow>(
_ workflow: WorkflowType,
initialState: WorkflowType.State,
session: WorkflowSession
)
/// Called before a `Workflow` is queried for its latest `Rendering`.
/// - Parameters:
/// - workflow: The `Workflow` that is about to be render.
/// - state: The corresponding `State` that will be used during the render call.
/// - session: The `WorkflowSession` corresponding to the backing `WorkflowNode`.
/// - Returns: An optional closure to be called immediately after the new `Rendering` is produced, which takes the rendering as the only parameter.
func workflowWillRender<WorkflowType: Workflow>(
_ workflow: WorkflowType,
state: WorkflowType.State,
session: WorkflowSession
) -> ((WorkflowType.Rendering) -> Void)?
/// Called after an existing `Workflow` is updated.
/// - Parameters:
/// - oldWorkflow: The previous `Workflow`
/// - newWorkflow: The new `Workflow`
/// - state: The state **after** the update has occurred.
/// - session: The `WorkflowSession` corresponding to the backing `WorkflowNode`.
func workflowDidChange<WorkflowType: Workflow>(
from oldWorkflow: WorkflowType,
to newWorkflow: WorkflowType,
state: WorkflowType.State,
session: WorkflowSession
)
/// Called after a `WorkflowAction` is received, but **before** it has been handled and propagated up the tree.
/// - Parameters:
/// - action: The action that was received.
/// - session: The `WorkflowSession` corresponding to the backing `WorkflowNode`.
func workflowDidReceiveAction<Action: WorkflowAction>(
_ action: Action,
workflow: Action.WorkflowType,
session: WorkflowSession
)
/// Called when a `WorkflowAction` will be applied to its corresponding Workflow's State
/// - Parameters:
/// - action: The action that will be applied.
/// - workflow: The action's corresponding `Workflow`.
/// - state: The state to which the action will be applied.
/// - session: The `WorkflowSession` corresponding to the backing `WorkflowNode`.
/// - Returns: An optional closure to be called immediately after the action is applied to the `State`, and an optional `Output` has been produced. The closure takes the updated state and optional output as its arguments.
func workflowWillApplyAction<Action: WorkflowAction>(
_ action: Action,
workflow: Action.WorkflowType,
state: Action.WorkflowType.State,
session: WorkflowSession
) -> ((Action.WorkflowType.State, Action.WorkflowType.Output?) -> Void)?
}
// MARK: - WorkflowSession
/// A `WorkflowSession`encapsulates the information that gives a `WorkflowNode` its identity.
/// In particular, it captures:
/// - The type of the corresponding `Workflow` conformance.
/// - The `String` key used when the workflow was rendered.
/// - An `Identifier` type that is unique across program execution.
/// - An optional reference to a parent `WorkflowSession`, to differentiate root nodes.
public struct WorkflowSession {
public struct Identifier: Hashable {
private static var _nextRawID: UInt64 = 0
private static func _makeNextSessionID() -> UInt64 {
let nextID = _nextRawID
_nextRawID += 1
return nextID
}
let rawIdentifier: UInt64 = Self._makeNextSessionID()
}
/// As structs cannot contain stored properties of their own type, we use an indirect enum
/// to allow referencing the parent session.
private indirect enum IndirectParent {
case some(WorkflowSession)
case none
init(_ parent: WorkflowSession?) {
switch parent {
case .some(let value):
self = .some(value)
case .none:
self = .none
}
}
}
public let workflowType: Any.Type
public let renderKey: String
public let sessionID = Identifier()
private let _indirectParent: IndirectParent
public var parent: WorkflowSession? {
switch _indirectParent {
case .some(let parent):
parent
case .none:
nil
}
}
/// Creates a new `WorkflowSession` instance. Note, construction of this type
/// is not safe to perform concurrently with respect to other instance initialization.
/// - Parameters:
/// - workflow: The associated `Workflow` instance
/// - renderKey: The string key used to render `workflow`
/// - parent: The parent Workflow's session, if any
init<WorkflowType: Workflow>(
workflow: WorkflowType,
renderKey: String,
parent: WorkflowSession?
) {
self.workflowType = WorkflowType.self
self.renderKey = renderKey
self._indirectParent = IndirectParent(parent)
}
}
// MARK: - No-op Defaults
extension WorkflowObserver {
public func sessionDidBegin(
_ session: WorkflowSession
) {}
public func sessionDidEnd(
_ session: WorkflowSession
) {}
public func workflowDidMakeInitialState<WorkflowType: Workflow>(
_ workflow: WorkflowType,
initialState: WorkflowType.State,
session: WorkflowSession
) {}
public func workflowWillRender<WorkflowType: Workflow>(
_ workflow: WorkflowType,
state: WorkflowType.State,
session: WorkflowSession
) -> ((WorkflowType.Rendering) -> Void)? { nil }
public func workflowDidChange<WorkflowType: Workflow>(
from oldWorkflow: WorkflowType,
to newWorkflow: WorkflowType,
state: WorkflowType.State,
session: WorkflowSession
) {}
public func workflowDidReceiveAction<Action: WorkflowAction>(
_ action: Action,
workflow: Action.WorkflowType,
session: WorkflowSession
) {}
public func workflowWillApplyAction<Action: WorkflowAction>(
_ action: Action,
workflow: Action.WorkflowType,
state: Action.WorkflowType.State,
session: WorkflowSession
) -> ((Action.WorkflowType.State, Action.WorkflowType.Output?) -> Void)? { nil }
}
// MARK: Chained Observer
/// 'Chained' observer implementation, which multiplexes a list of `WorkflowObservers` into a
/// single observer interface.
final class ChainedWorkflowObserver: WorkflowObserver {
let observers: [WorkflowObserver]
init(observers: [WorkflowObserver]) {
self.observers = observers
}
func sessionDidBegin(_ session: WorkflowSession) {
for observer in observers {
observer.sessionDidBegin(session)
}
}
func sessionDidEnd(_ session: WorkflowSession) {
for observer in observers {
observer.sessionDidEnd(session)
}
}
func workflowDidMakeInitialState<WorkflowType>(_ workflow: WorkflowType, initialState: WorkflowType.State, session: WorkflowSession) where WorkflowType: Workflow {
for observer in observers {
observer.workflowDidMakeInitialState(workflow, initialState: initialState, session: session)
}
}
func workflowWillRender<WorkflowType>(
_ workflow: WorkflowType,
state: WorkflowType.State,
session: WorkflowSession
) -> ((WorkflowType.Rendering) -> Void)? where WorkflowType: Workflow {
let callbacks = observers.compactMap {
$0.workflowWillRender(workflow, state: state, session: session)
}
guard !callbacks.isEmpty else {
return nil
}
// Invoke the callbacks in reverse order.
// This enables the first observer to 'bookend' the method and
// other observer callbacks, which can useful if trying to implement
// performance tracing instrumentation.
return { rendering in
for callback in callbacks.reversed() {
callback(rendering)
}
}
}
func workflowDidChange<WorkflowType>(
from oldWorkflow: WorkflowType,
to newWorkflow: WorkflowType,
state: WorkflowType.State,
session: WorkflowSession
) where WorkflowType: Workflow {
for observer in observers {
observer.workflowDidChange(
from: oldWorkflow,
to: newWorkflow,
state: state,
session: session
)
}
}
func workflowDidReceiveAction<Action: WorkflowAction>(
_ action: Action,
workflow: Action.WorkflowType,
session: WorkflowSession
) {
for observer in observers {
observer.workflowDidReceiveAction(
action,
workflow: workflow,
session: session
)
}
}
func workflowWillApplyAction<Action: WorkflowAction>(
_ action: Action,
workflow: Action.WorkflowType,
state: Action.WorkflowType.State,
session: WorkflowSession
) -> ((Action.WorkflowType.State, Action.WorkflowType.Output?) -> Void)? {
let callbacks = observers.compactMap {
$0.workflowWillApplyAction(
action,
workflow: workflow,
state: state,
session: session
)
}
guard !callbacks.isEmpty else {
return nil
}
// Invoke the callbacks in reverse order.
// This enables the first observer to 'bookend' the method and
// other observer callbacks, which can useful if trying to implement
// performance tracing instrumentation.
return { state, output in
for callback in callbacks.reversed() {
callback(state, output)
}
}
}
}
extension [WorkflowObserver] {
func chained() -> WorkflowObserver? {
if count <= 1 {
// no wrapping needed if empty or a single element
first
} else {
ChainedWorkflowObserver(observers: self)
}
}
}
// MARK: - Global Observation (SPI)
@_spi(WorkflowGlobalObservation)
public protocol ObserversInterceptor {
/// Provides a single access point to provide the final list of `WorkflowObserver` used by the runtime.
/// This may be used to ensure a known set of observers is used in a particular order for all
/// `WorkflowHost`s created over the life of a program.
/// - Parameter initialObservers: Array of observers passed to a `WorkflowHost` constructor
/// - Returns: The array of `WorkflowObserver`s to be used by the `WorkflowHost`
func workflowObservers(for initialObservers: [WorkflowObserver]) -> [WorkflowObserver]
}
@_spi(WorkflowGlobalObservation)
public enum WorkflowObservation {
private static var _sharedInterceptorStorage: ObserversInterceptor = NoOpObserversInterceptor()
/// The `DefaultObserversProvider` used by all runtimes.
public static var sharedObserversInterceptor: ObserversInterceptor! {
get {
_sharedInterceptorStorage
}
set {
guard newValue != nil else {
_sharedInterceptorStorage = NoOpObserversInterceptor()
return
}
_sharedInterceptorStorage = newValue
}
}
private struct NoOpObserversInterceptor: ObserversInterceptor {
func workflowObservers(for initialObservers: [WorkflowObserver]) -> [WorkflowObserver] {
initialObservers
}
}
}