workflow-swift icon indicating copy to clipboard operation
workflow-swift copied to clipboard

[DNM] Ideas to make state mutations from Child Workflows easier

Open dhavalshreyas opened this issue 4 years ago • 2 comments

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)
    }
)

dhavalshreyas avatar Jul 24 '20 02:07 dhavalshreyas

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)

bencochran avatar Jul 27 '20 22:07 bencochran

Also, just realized this is a PR not an issue, will look at the keyPath bit.

bencochran avatar Jul 27 '20 22:07 bencochran