@@ -143,9 +143,11 @@ private class RootScopeImpl<PropsT, OutputT>(
143143) : RootScope<PropsT, OutputT>, CoroutineScope by coroutineScope {
144144
145145 override fun emitOutput (output : OutputT ) {
146- actionSink.send(action(" emitOutput" ) {
147- setOutput(output)
148- })
146+ actionSink.send(
147+ action(" emitOutput" ) {
148+ setOutput(output)
149+ }
150+ )
149151 }
150152
151153 override suspend fun <ChildPropsT , ChildOutputT , R > showWorkflow (
@@ -170,9 +172,11 @@ private class ShowWorkflowChildScopeImpl<PropsT, OutputT, R>(
170172) : ShowWorkflowChildScope<OutputT, R>, CoroutineScope by coroutineScope {
171173
172174 override fun emitOutput (output : OutputT ) {
173- actionSink.send(action(" emitOutput" ) {
174- setOutput(output)
175- })
175+ actionSink.send(
176+ action(" emitOutput" ) {
177+ setOutput(output)
178+ }
179+ )
176180 }
177181
178182 @Suppress(" UNCHECKED_CAST" )
@@ -207,7 +211,9 @@ private class Frame<PropsT, OutputT, ChildPropsT, ChildOutputT, R>(
207211 private val props : ChildPropsT ,
208212 private val callerJob : Job ,
209213 val frameScope : CoroutineScope ,
210- private val onOutput : suspend ShowWorkflowChildScopeImpl <PropsT , OutputT , R >.(ChildOutputT ) -> Unit ,
214+ private val onOutput : suspend ShowWorkflowChildScopeImpl <PropsT , OutputT , R >.(
215+ ChildOutputT
216+ ) -> Unit ,
211217 private val actionSink : Sink <WorkflowAction <PropsT , ThingyState , OutputT >>,
212218 private val parent : Frame <* , * , * , * , * >? ,
213219) {
@@ -227,22 +233,24 @@ private class Frame<PropsT, OutputT, ChildPropsT, ChildOutputT, R>(
227233 * Pops everything off the stack that comes after this.
228234 */
229235 fun popTo () {
230- actionSink.send(action(" popTo" ) {
231- val stack = state.stack
232- val index = stack.indexOf(this @Frame)
233- check(index != - 1 ) { " Frame was not in the stack!" }
234-
235- // Cancel all the frames we're about to drop, starting from the top.
236- for (i in stack.lastIndex downTo index + 1 ) {
237- // Don't just cancel the frame job, since that would only cancel output handlers the frame
238- // is running. We want to cancel the whole parent's output handler that called showWorkflow,
239- // in case the showWorkflow is in a try/catch that tries to make other suspending calls.
240- stack[i].callerJob.cancel()
241- }
236+ actionSink.send(
237+ action(" popTo" ) {
238+ val stack = state.stack
239+ val index = stack.indexOf(this @Frame)
240+ check(index != - 1 ) { " Frame was not in the stack!" }
241+
242+ // Cancel all the frames we're about to drop, starting from the top.
243+ for (i in stack.lastIndex downTo index + 1 ) {
244+ // Don't just cancel the frame job, since that would only cancel output handlers the frame
245+ // is running. We want to cancel the whole parent's output handler that called showWorkflow,
246+ // in case the showWorkflow is in a try/catch that tries to make other suspending calls.
247+ stack[i].callerJob.cancel()
248+ }
242249
243- val newStack = stack.take(index + 1 )
244- state = state.copy(stack = newStack)
245- })
250+ val newStack = stack.take(index + 1 )
251+ state = state.copy(stack = newStack)
252+ }
253+ )
246254 }
247255
248256 private fun onOutput (output : ChildOutputT ): WorkflowAction <PropsT , ThingyState , OutputT > {
@@ -276,9 +284,11 @@ private class Frame<PropsT, OutputT, ChildPropsT, ChildOutputT, R>(
276284 // This will eventually cancel the frame scope.
277285 result.complete(it)
278286 // TODO figure out how to coalesce this action into the one for showWorkflow. WorkStealingDispatcher?
279- sink.send(action(" unshowWorkflow" ) {
280- state = state.removeFrame(this @Frame)
281- })
287+ sink.send(
288+ action(" unshowWorkflow" ) {
289+ state = state.removeFrame(this @Frame)
290+ }
291+ )
282292 },
283293 thisFrame = this @Frame,
284294 parentFrame = parent
@@ -309,26 +319,30 @@ private suspend fun <PropsT, OutputT, ChildPropsT, ChildOutputT, R> showWorkflow
309319 lateinit var frame: Frame <PropsT , OutputT , ChildPropsT , ChildOutputT , R >
310320
311321 // Tell the workflow runtime to start rendering the new workflow.
312- actionSink.sendAndAwaitApplication(action(" showWorkflow" ) {
313- frame = Frame (
314- workflow = workflow,
315- props = props,
316- callerJob = callerJob,
317- frameScope = frameScope,
318- onOutput = onOutput,
319- actionSink = actionSink,
320- parent = parentFrame,
321- )
322- state = state.appendFrame(frame)
323- })
322+ actionSink.sendAndAwaitApplication(
323+ action(" showWorkflow" ) {
324+ frame = Frame (
325+ workflow = workflow,
326+ props = props,
327+ callerJob = callerJob,
328+ frameScope = frameScope,
329+ onOutput = onOutput,
330+ actionSink = actionSink,
331+ parent = parentFrame,
332+ )
333+ state = state.appendFrame(frame)
334+ }
335+ )
324336
325337 return try {
326338 frame.awaitResult()
327339 } finally {
328340 frameScope.cancel()
329- actionSink.send(action(" unshowWorkflow" ) {
330- state = state.removeFrame(frame)
331- })
341+ actionSink.send(
342+ action(" unshowWorkflow" ) {
343+ state = state.removeFrame(frame)
344+ }
345+ )
332346 }
333347}
334348
0 commit comments