Skip to content

Commit dfa2ac1

Browse files
authored
[feat]: add primary associated types to Workflow protocol (#181)
- adds `Rendering` and `Output` as the primary associated types of the `Workflow` protocol
1 parent 657f2ab commit dfa2ac1

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Workflow/Sources/Workflow.swift

+34
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,39 @@
4949
/// }
5050
/// ```
5151
///
52+
#if swift(>=5.7)
53+
public protocol Workflow<Rendering, Output>: AnyWorkflowConvertible {
54+
/// Defines the state that is managed by this workflow.
55+
associatedtype State
56+
57+
/// `Output` defines the type that can be emitted as output events.
58+
associatedtype Output = Never
59+
60+
/// `Rendering` is the type that is produced by the `render` method: it
61+
/// is commonly a view / screen model.
62+
associatedtype Rendering
63+
64+
/// This method is invoked once when a workflow node comes into existence.
65+
///
66+
/// - Returns: The initial state for the workflow.
67+
func makeInitialState() -> State
68+
69+
/// Called when a new workflow is passed down from the parent to an existing workflow node.
70+
///
71+
/// - Parameter previousWorkflow: The workflow before the update.
72+
/// - Parameter state: The current state.
73+
func workflowDidChange(from previousWorkflow: Self, state: inout State)
74+
75+
/// Called by the internal Workflow infrastructure to "render" the current state into `Rendering`.
76+
/// A workflow's `Rendering` type is commonly a view or screen model.
77+
///
78+
/// - Parameter state: The current state.
79+
/// - Parameter context: The workflow context is the composition point for the workflow tree. To use a nested
80+
/// workflow, instantiate it based on the current state, then call `rendered(in:key:outputMap:)`.
81+
/// This will return the child's `Rendering` type after creating or updating the nested workflow.
82+
func render(state: State, context: RenderContext<Self>) -> Rendering
83+
}
84+
#else
5285
public protocol Workflow: AnyWorkflowConvertible {
5386
/// Defines the state that is managed by this workflow.
5487
associatedtype State
@@ -80,6 +113,7 @@ public protocol Workflow: AnyWorkflowConvertible {
80113
/// This will return the child's `Rendering` type after creating or updating the nested workflow.
81114
func render(state: State, context: RenderContext<Self>) -> Rendering
82115
}
116+
#endif
83117

84118
extension Workflow {
85119
public func workflowDidChange(from previousWorkflow: Self, state: inout State) {}

0 commit comments

Comments
 (0)