@@ -143,9 +143,11 @@ private class RootScopeImpl<PropsT, OutputT>(
143
143
) : RootScope<PropsT, OutputT>, CoroutineScope by coroutineScope {
144
144
145
145
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
+ )
149
151
}
150
152
151
153
override suspend fun <ChildPropsT , ChildOutputT , R > showWorkflow (
@@ -170,9 +172,11 @@ private class ShowWorkflowChildScopeImpl<PropsT, OutputT, R>(
170
172
) : ShowWorkflowChildScope<OutputT, R>, CoroutineScope by coroutineScope {
171
173
172
174
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
+ )
176
180
}
177
181
178
182
@Suppress(" UNCHECKED_CAST" )
@@ -207,7 +211,9 @@ private class Frame<PropsT, OutputT, ChildPropsT, ChildOutputT, R>(
207
211
private val props : ChildPropsT ,
208
212
private val callerJob : Job ,
209
213
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 ,
211
217
private val actionSink : Sink <WorkflowAction <PropsT , ThingyState , OutputT >>,
212
218
private val parent : Frame <* , * , * , * , * >? ,
213
219
) {
@@ -227,22 +233,24 @@ private class Frame<PropsT, OutputT, ChildPropsT, ChildOutputT, R>(
227
233
* Pops everything off the stack that comes after this.
228
234
*/
229
235
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
+ }
242
249
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
+ )
246
254
}
247
255
248
256
private fun onOutput (output : ChildOutputT ): WorkflowAction <PropsT , ThingyState , OutputT > {
@@ -276,9 +284,11 @@ private class Frame<PropsT, OutputT, ChildPropsT, ChildOutputT, R>(
276
284
// This will eventually cancel the frame scope.
277
285
result.complete(it)
278
286
// 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
+ )
282
292
},
283
293
thisFrame = this @Frame,
284
294
parentFrame = parent
@@ -309,26 +319,30 @@ private suspend fun <PropsT, OutputT, ChildPropsT, ChildOutputT, R> showWorkflow
309
319
lateinit var frame: Frame <PropsT , OutputT , ChildPropsT , ChildOutputT , R >
310
320
311
321
// 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
+ )
324
336
325
337
return try {
326
338
frame.awaitResult()
327
339
} finally {
328
340
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
+ )
332
346
}
333
347
}
334
348
0 commit comments