|
| 1 | +package com.squareup.sample.thingy |
| 2 | + |
| 3 | +import com.squareup.workflow1.Sink |
| 4 | +import com.squareup.workflow1.StatefulWorkflow.RenderContext |
| 5 | +import com.squareup.workflow1.Workflow |
| 6 | +import com.squareup.workflow1.WorkflowAction |
| 7 | +import com.squareup.workflow1.WorkflowAction.Companion |
| 8 | +import com.squareup.workflow1.ui.Screen |
| 9 | +import kotlinx.coroutines.CompletableDeferred |
| 10 | +import kotlinx.coroutines.CoroutineScope |
| 11 | +import kotlinx.coroutines.CoroutineStart.UNDISPATCHED |
| 12 | +import kotlinx.coroutines.Job |
| 13 | +import kotlinx.coroutines.cancel |
| 14 | +import kotlinx.coroutines.currentCoroutineContext |
| 15 | +import kotlinx.coroutines.ensureActive |
| 16 | +import kotlinx.coroutines.job |
| 17 | +import kotlinx.coroutines.launch |
| 18 | + |
| 19 | +internal sealed interface BackStackFrame<R> { |
| 20 | + fun cancelCaller() |
| 21 | + suspend fun awaitResult(): R |
| 22 | + suspend fun cancelSelf(): Nothing |
| 23 | + fun cancel() |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Represents a call to [BackStackScope.showWorkflow]. |
| 28 | + */ |
| 29 | +internal class WorkflowFrame<PropsT, OutputT, ChildPropsT, ChildOutputT, R> private constructor( |
| 30 | + private val workflow: Workflow<ChildPropsT, ChildOutputT, Screen>, |
| 31 | + private val props: ChildPropsT, |
| 32 | + private val callerJob: Job, |
| 33 | + private val frameScope: CoroutineScope, |
| 34 | + private val onOutput: suspend BackStackWorkflowScope.(ChildOutputT) -> R, |
| 35 | + private val actionSink: Sink<WorkflowAction<PropsT, BackStackState, OutputT>>, |
| 36 | + private val parent: BackStackFrame<*>?, |
| 37 | + private val result: CompletableDeferred<R>, |
| 38 | +) : BackStackFrame<R> { |
| 39 | + |
| 40 | + constructor( |
| 41 | + workflow: Workflow<ChildPropsT, ChildOutputT, Screen>, |
| 42 | + initialProps: ChildPropsT, |
| 43 | + callerJob: Job, |
| 44 | + frameScope: CoroutineScope, |
| 45 | + onOutput: suspend BackStackWorkflowScope.(ChildOutputT) -> R, |
| 46 | + actionSink: Sink<WorkflowAction<PropsT, BackStackState, OutputT>>, |
| 47 | + parent: BackStackFrame<*>?, |
| 48 | + ) : this( |
| 49 | + workflow = workflow, |
| 50 | + props = initialProps, |
| 51 | + callerJob = callerJob, |
| 52 | + frameScope = frameScope, |
| 53 | + onOutput = onOutput, |
| 54 | + actionSink = actionSink, |
| 55 | + parent = parent, |
| 56 | + result = CompletableDeferred(parent = frameScope.coroutineContext.job) |
| 57 | + ) |
| 58 | + |
| 59 | + fun copy( |
| 60 | + props: ChildPropsT = this.props, |
| 61 | + ): WorkflowFrame<PropsT, OutputT, ChildPropsT, ChildOutputT, R> = WorkflowFrame( |
| 62 | + workflow = workflow, |
| 63 | + props = props, |
| 64 | + callerJob = callerJob, |
| 65 | + frameScope = frameScope, |
| 66 | + onOutput = onOutput, |
| 67 | + actionSink = actionSink, |
| 68 | + parent = parent, |
| 69 | + result = result |
| 70 | + ) |
| 71 | + |
| 72 | + override suspend fun awaitResult(): R = result.await() |
| 73 | + |
| 74 | + override fun cancelCaller() { |
| 75 | + callerJob.cancel() |
| 76 | + } |
| 77 | + |
| 78 | + private suspend fun finishWith(value: R): Nothing { |
| 79 | + result.complete(value) |
| 80 | + cancelSelf() |
| 81 | + } |
| 82 | + |
| 83 | + override suspend fun cancelSelf(): Nothing { |
| 84 | + cancel() |
| 85 | + val currentContext = currentCoroutineContext() |
| 86 | + currentContext.cancel() |
| 87 | + currentContext.ensureActive() |
| 88 | + error("Nonsense") |
| 89 | + } |
| 90 | + |
| 91 | + override fun cancel() { |
| 92 | + frameScope.cancel() |
| 93 | + } |
| 94 | + |
| 95 | + fun renderWorkflow( |
| 96 | + context: RenderContext<PropsT, BackStackState, OutputT> |
| 97 | + ): Screen = context.renderChild( |
| 98 | + child = workflow, |
| 99 | + props = props, |
| 100 | + handler = ::onOutput |
| 101 | + ) |
| 102 | + |
| 103 | + private fun onOutput(output: ChildOutputT): WorkflowAction<PropsT, BackStackState, OutputT> { |
| 104 | + var canAcceptAction = true |
| 105 | + var action: WorkflowAction<PropsT, BackStackState, OutputT>? = null |
| 106 | + val sink = object : Sink<WorkflowAction<PropsT, BackStackState, OutputT>> { |
| 107 | + override fun send(value: WorkflowAction<PropsT, BackStackState, OutputT>) { |
| 108 | + val sendToSink = synchronized(result) { |
| 109 | + if (canAcceptAction) { |
| 110 | + action = value |
| 111 | + canAcceptAction = false |
| 112 | + false |
| 113 | + } else { |
| 114 | + true |
| 115 | + } |
| 116 | + } |
| 117 | + if (sendToSink) { |
| 118 | + actionSink.send(value) |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // Run synchronously until first suspension point since in many cases it will immediately |
| 124 | + // either call showWorkflow, finishWith, or goBack, and so then we can just return that action |
| 125 | + // immediately instead of needing a whole separate render pass. |
| 126 | + frameScope.launch(start = UNDISPATCHED) { |
| 127 | + val showScope = BackStackWorkflowScopeImpl( |
| 128 | + actionSink = sink, |
| 129 | + coroutineScope = this, |
| 130 | + thisFrame = this@WorkflowFrame, |
| 131 | + parentFrame = parent |
| 132 | + ) |
| 133 | + finishWith(onOutput(showScope, output)) |
| 134 | + } |
| 135 | + // TODO collect WorkflowAction |
| 136 | + |
| 137 | + // Once the coroutine has suspended, all sends must go to the real sink. |
| 138 | + return synchronized(result) { |
| 139 | + canAcceptAction = false |
| 140 | + action ?: WorkflowAction.noAction() |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +/** |
| 146 | + * Represents a call to [BackStackScope.showScreen]. |
| 147 | + */ |
| 148 | +internal class ScreenFrame<OutputT, R>( |
| 149 | + private val callerJob: Job, |
| 150 | + private val frameScope: CoroutineScope, |
| 151 | + private val actionSink: Sink<WorkflowAction<Any?, BackStackState, OutputT>>, |
| 152 | + private val parent: BackStackFrame<*>?, |
| 153 | +) : BackStackFrame<R> { |
| 154 | + private val result = CompletableDeferred<R>() |
| 155 | + |
| 156 | + lateinit var screen: Screen |
| 157 | + private set |
| 158 | + |
| 159 | + fun initScreen(screenFactory: BackStackScreenScope<R>.() -> Screen) { |
| 160 | + val factoryScope = BackStackScreenScopeImpl<Any?, OutputT, R>( |
| 161 | + actionSink = actionSink, |
| 162 | + coroutineScope = frameScope, |
| 163 | + thisFrame = this, |
| 164 | + parentFrame = parent |
| 165 | + ) |
| 166 | + screen = screenFactory(factoryScope) |
| 167 | + } |
| 168 | + |
| 169 | + override suspend fun awaitResult(): R = result.await() |
| 170 | + |
| 171 | + override fun cancelCaller() { |
| 172 | + callerJob.cancel() |
| 173 | + } |
| 174 | + |
| 175 | + fun continueWith(value: R) { |
| 176 | + result.complete(value) |
| 177 | + cancel() |
| 178 | + } |
| 179 | + |
| 180 | + override suspend fun cancelSelf(): Nothing { |
| 181 | + cancel() |
| 182 | + val currentContext = currentCoroutineContext() |
| 183 | + currentContext.cancel() |
| 184 | + currentContext.ensureActive() |
| 185 | + error("Nonsense") |
| 186 | + } |
| 187 | + |
| 188 | + override fun cancel() { |
| 189 | + frameScope.cancel() |
| 190 | + } |
| 191 | +} |
0 commit comments