-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathWorkflowHost.swift
102 lines (79 loc) · 3.83 KB
/
WorkflowHost.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
/*
* Copyright 2019 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 ReactiveSwift
/// Defines a type that receives debug information about a running workflow hierarchy.
public protocol WorkflowDebugger {
/// Called once when the workflow hierarchy initializes.
///
/// - Parameter snapshot: Debug information about the workflow hierarchy.
func didEnterInitialState(snapshot: WorkflowHierarchyDebugSnapshot)
/// Called when an update occurs anywhere within the workflow hierarchy.
///
/// - Parameter snapshot: Debug information about the workflow hierarchy *after* the update.
/// - Parameter updateInfo: Information about the update.
func didUpdate(snapshot: WorkflowHierarchyDebugSnapshot, updateInfo: WorkflowUpdateDebugInfo)
}
/// Manages an active workflow hierarchy.
public final class WorkflowHost<WorkflowType: Workflow> {
private let debugger: WorkflowDebugger?
private let (outputEvent, outputEventObserver) = Signal<WorkflowType.Output, Never>.pipe()
private let rootNode: WorkflowNode<WorkflowType>
private let mutableRendering: MutableProperty<WorkflowType.Rendering>
/// Represents the `Rendering` produced by the root workflow in the hierarchy. New `Rendering` values are produced
/// as state transitions occur within the hierarchy.
public let rendering: Property<WorkflowType.Rendering>
/// Initializes a new host with the given workflow at the root.
///
/// - Parameter workflow: The root workflow in the hierarchy
/// - Parameter debugger: An optional debugger. If provided, the host will notify the debugger of updates
/// to the workflow hierarchy as state transitions occur.
public init(workflow: WorkflowType, debugger: WorkflowDebugger? = nil) {
self.debugger = debugger
self.rootNode = WorkflowNode(workflow: workflow)
self.mutableRendering = MutableProperty(self.rootNode.render())
self.rendering = Property(mutableRendering)
self.rootNode.enableEvents()
debugger?.didEnterInitialState(snapshot: self.rootNode.makeDebugSnapshot())
rootNode.onOutput = { [weak self] output in
self?.handle(output: output)
}
}
/// Update the input for the workflow. Will cause a render pass.
public func update(workflow: WorkflowType) {
rootNode.update(workflow: workflow)
// Treat the update as an "output" from the workflow originating from an external event to force a render pass.
let output = WorkflowNode<WorkflowType>.Output(
outputEvent: nil,
debugInfo: WorkflowUpdateDebugInfo(
workflowType: "\(WorkflowType.self)",
kind: .didUpdate(source: .external)))
handle(output: output)
}
private func handle(output: WorkflowNode<WorkflowType>.Output) {
mutableRendering.value = rootNode.render()
if let outputEvent = output.outputEvent {
outputEventObserver.send(value: outputEvent)
}
debugger?.didUpdate(
snapshot: rootNode.makeDebugSnapshot(),
updateInfo: output.debugInfo)
rootNode.enableEvents()
}
/// A signal containing output events emitted by the root workflow in the hierarchy.
public var output: Signal<WorkflowType.Output, Never> {
return outputEvent
}
}