workflow-swift
workflow-swift copied to clipboard
[DNM] Ideas to make state mutations from Child Workflows easier
Explores introducing APIs to make State mutations easier.
Proposal 1:
Introduce:
extension AnyWorkflow {
public func applyOutputToState<Parent: Workflow>(in context: RenderContext<Parent>,
stateMutation: @escaping ((inout Parent.State, Output) -> Void))
-> AnyWorkflow<Rendering, AnyWorkflowAction<Parent>>
}
that let's us do:
WelcomeWorkflow()
.mapOutput { .demo(name: $0) }
.applyOutputToState(in: context, keyPath: \State.self)
.rendered(in: context)
Proposal 2:
Introduce:
extension RenderContext {
public func makeStateMutationSink() -> StateMutationSink<WorkflowType>
}
that let's us do:
let sink = context.makeStateMutationSink()
return FooScreen(
title: "Leading Foo screen",
viewTapped: {
sink.send(\State.self, value: state + 1)
}
)
Proposal 1 is one of the things I was thinking in the old brain dump (internal link, sorry external people). I think we skipped it because it wasn’t 1.0-blocking, but my onOutput
was exactly what your applyOutputToState
signature looks like. Not sure what your keyPath-based one is doing though, can you expand on that?
(Haven’t had a chance to look at / think about proposal 2 yet)
Also, just realized this is a PR not an issue, will look at the keyPath
bit.