|
| 1 | +package com.squareup.workflow1 |
| 2 | + |
| 3 | +import kotlinx.coroutines.CoroutineScope |
| 4 | + |
| 5 | +/** |
| 6 | + * An extension of [StatefulWorkflow] that gives [initialState] a [CoroutineScope] |
| 7 | + * that corresponds with the lifetime of _session_ driven by this Workflow. |
| 8 | + * |
| 9 | + * A session begins the first time a parent passes a child [Workflow] of a particular type to |
| 10 | + * [renderChild] with a particular [key] parameter. It ends when the parent executes [render] |
| 11 | + * without making a matching [renderChild] call. The [CoroutineScope] that is passed to |
| 12 | + * [initialState] is created when a session starts (when [renderChild] is first called), and |
| 13 | + * [cancelled][kotlinx.coroutines.Job.cancel] when the session ends. |
| 14 | + * |
| 15 | + * This API extension exists on [StatefulWorkflow] as well, but it is confusing because the version |
| 16 | + * of [initialState] that does not have the [CoroutineScope] must also be implemented as it is |
| 17 | + * an abstract fun, even though it would never be used. |
| 18 | + * With this version, that confusion is removed and only the version of [initialState] with the |
| 19 | + * [CoroutineScope] must be implemented. |
| 20 | + */ |
| 21 | +@WorkflowExperimentalApi |
| 22 | +public abstract class SessionWorkflow< |
| 23 | + in PropsT, |
| 24 | + StateT, |
| 25 | + out OutputT, |
| 26 | + out RenderingT |
| 27 | + > : StatefulWorkflow<PropsT, StateT, OutputT, RenderingT>() { |
| 28 | + |
| 29 | + /** |
| 30 | + * @see [StatefulWorkflow.initialState] for kdoc on the base function of this method. |
| 31 | + * |
| 32 | + * This version adds the following: |
| 33 | + * @param workflowScope A [CoroutineScope] that has been created when this Workflow is first |
| 34 | + * rendered and canceled when it is no longer rendered. |
| 35 | + * |
| 36 | + * This [CoroutineScope] can be used to: |
| 37 | + * |
| 38 | + * - set reliable teardown hooks, e.g. via [Job.invokeOnCompletion][kotlinx.coroutines.Job.invokeOnCompletion]. |
| 39 | + * |
| 40 | + * - own the transforms on a [StateFlow][kotlinx.coroutines.flow.StateFlow], |
| 41 | + * linking them to the lifetime of a Workflow session. For example, |
| 42 | + * here is how you might safely combine two `StateFlow`s: |
| 43 | + * |
| 44 | + * data class MyState( |
| 45 | + * val derivedValue: String, |
| 46 | + * val derivedWorker: Worker<String> |
| 47 | + * ) |
| 48 | + * |
| 49 | + * override fun initialState( |
| 50 | + * props: Unit, |
| 51 | + * snapshot: Snapshot?, |
| 52 | + * workflowScope: CoroutineScope |
| 53 | + * ): MyState { |
| 54 | + * val transformedStateFlow = stateFlow1.combine(stateFlow2, {val1, val2 -> val1 - val2}). |
| 55 | + * stateIn(workflowScope, SharingStarted.Eagerly, ${stateFlow1.value}-${stateFlow2.value}) |
| 56 | + * |
| 57 | + * return MyState( |
| 58 | + * transformedStateFlow.value, |
| 59 | + * transformedStateFlow.asWorker() |
| 60 | + * ) |
| 61 | + * } |
| 62 | + * |
| 63 | + * **Note Carefully**: Neither [workflowScope] nor any of these transformed/computed dependencies |
| 64 | + * should be stored by this Workflow instance. This could be re-created, or re-used unexpectedly |
| 65 | + * and should not have its own state. Instead, the transformed/computed dependencies must be |
| 66 | + * put into the [StateT] of this Workflow in order to be properly maintained. |
| 67 | + */ |
| 68 | + public abstract override fun initialState( |
| 69 | + props: PropsT, |
| 70 | + snapshot: Snapshot?, |
| 71 | + workflowScope: CoroutineScope |
| 72 | + ): StateT |
| 73 | + |
| 74 | + /** |
| 75 | + * Do not use this in favor of the version of [initialState] above that includes the Workflow's |
| 76 | + * [CoroutineScope] |
| 77 | + */ |
| 78 | + public final override fun initialState( |
| 79 | + props: PropsT, |
| 80 | + snapshot: Snapshot? |
| 81 | + ): StateT { |
| 82 | + error("SessionWorkflow should never call initialState without the CoroutineScope.") |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Returns a [SessionWorkflow] implemented via the given functions. |
| 88 | + */ |
| 89 | +@WorkflowExperimentalApi |
| 90 | +public inline fun <PropsT, StateT, OutputT, RenderingT> Workflow.Companion.sessionWorkflow( |
| 91 | + crossinline initialState: (PropsT, Snapshot?, CoroutineScope) -> StateT, |
| 92 | + crossinline render: BaseRenderContext<PropsT, StateT, OutputT>.( |
| 93 | + props: PropsT, |
| 94 | + state: StateT |
| 95 | + ) -> RenderingT, |
| 96 | + crossinline snapshot: (StateT) -> Snapshot?, |
| 97 | + crossinline onPropsChanged: ( |
| 98 | + old: PropsT, |
| 99 | + new: PropsT, |
| 100 | + state: StateT |
| 101 | + ) -> StateT = { _, _, state -> state } |
| 102 | +): SessionWorkflow<PropsT, StateT, OutputT, RenderingT> = |
| 103 | + object : SessionWorkflow<PropsT, StateT, OutputT, RenderingT>() { |
| 104 | + override fun initialState( |
| 105 | + props: PropsT, |
| 106 | + snapshot: Snapshot?, |
| 107 | + workflowScope: CoroutineScope |
| 108 | + ): StateT = initialState(props, snapshot, workflowScope) |
| 109 | + |
| 110 | + override fun onPropsChanged( |
| 111 | + old: PropsT, |
| 112 | + new: PropsT, |
| 113 | + state: StateT |
| 114 | + ): StateT = onPropsChanged(old, new, state) |
| 115 | + |
| 116 | + override fun render( |
| 117 | + renderProps: PropsT, |
| 118 | + renderState: StateT, |
| 119 | + context: RenderContext |
| 120 | + ): RenderingT = render(context, renderProps, renderState) |
| 121 | + |
| 122 | + override fun snapshotState(state: StateT) = snapshot(state) |
| 123 | + } |
| 124 | + |
| 125 | +/** |
| 126 | + * Returns a [SessionWorkflow], with no props, implemented via the given functions. |
| 127 | + */ |
| 128 | +@WorkflowExperimentalApi |
| 129 | +public inline fun <StateT, OutputT, RenderingT> Workflow.Companion.sessionWorkflow( |
| 130 | + crossinline initialState: (Snapshot?, CoroutineScope) -> StateT, |
| 131 | + crossinline render: BaseRenderContext<Unit, StateT, OutputT>.(state: StateT) -> RenderingT, |
| 132 | + crossinline snapshot: (StateT) -> Snapshot? |
| 133 | +): SessionWorkflow<Unit, StateT, OutputT, RenderingT> = sessionWorkflow( |
| 134 | + { _, initialSnapshot, workflowScope -> initialState(initialSnapshot, workflowScope) }, |
| 135 | + { _, state -> render(state) }, |
| 136 | + snapshot |
| 137 | +) |
| 138 | + |
| 139 | +/** |
| 140 | + * Returns a [SessionWorkflow] implemented via the given functions. |
| 141 | + * |
| 142 | + * This overload does not support snapshotting, but there are other overloads that do. |
| 143 | + */ |
| 144 | +@WorkflowExperimentalApi |
| 145 | +public inline fun <PropsT, StateT, OutputT, RenderingT> Workflow.Companion.sessionWorkflow( |
| 146 | + crossinline initialState: (PropsT, CoroutineScope) -> StateT, |
| 147 | + crossinline render: BaseRenderContext<PropsT, StateT, OutputT>.( |
| 148 | + props: PropsT, |
| 149 | + state: StateT |
| 150 | + ) -> RenderingT, |
| 151 | + crossinline onPropsChanged: ( |
| 152 | + old: PropsT, |
| 153 | + new: PropsT, |
| 154 | + state: StateT |
| 155 | + ) -> StateT = { _, _, state -> state } |
| 156 | +): SessionWorkflow<PropsT, StateT, OutputT, RenderingT> = sessionWorkflow( |
| 157 | + { props, _, workflowScope -> initialState(props, workflowScope) }, |
| 158 | + render, |
| 159 | + { null }, |
| 160 | + onPropsChanged |
| 161 | +) |
| 162 | + |
| 163 | +/** |
| 164 | + * Returns a [SessionWorkflow], with no props, implemented via the given function. |
| 165 | + * |
| 166 | + * This overload does not support snapshots, but there are others that do. |
| 167 | + */ |
| 168 | +@WorkflowExperimentalApi |
| 169 | +public inline fun <StateT, OutputT, RenderingT> Workflow.Companion.sessionWorkflow( |
| 170 | + crossinline initialState: (CoroutineScope) -> StateT, |
| 171 | + crossinline render: BaseRenderContext<Unit, StateT, OutputT>.(state: StateT) -> RenderingT |
| 172 | +): SessionWorkflow<Unit, StateT, OutputT, RenderingT> = sessionWorkflow( |
| 173 | + { _, workflowScope -> initialState(workflowScope) }, |
| 174 | + { _, state -> render(state) } |
| 175 | +) |
0 commit comments