@@ -130,6 +130,45 @@ public interface BaseRenderContext<out PropsT, StateT, in OutputT> {
130
130
* given [update] function, and immediately passes it to [actionSink]. Handy for
131
131
* attaching event handlers to renderings.
132
132
*
133
+ * It is important to understand that the [update] lambda you provide here
134
+ * may not run synchronously. This function and its overloads provide a short cut
135
+ * that lets you replace this snippet:
136
+ *
137
+ * return SomeScreen(
138
+ * onClick = {
139
+ * context.actionSink.send(
140
+ * action { state = SomeNewState }
141
+ * }
142
+ * }
143
+ * )
144
+ *
145
+ * with this:
146
+ *
147
+ * return SomeScreen(
148
+ * onClick = context.eventHandler { state = SomeNewState }
149
+ * )
150
+ *
151
+ * Notice how your [update] function is passed to the [actionSink][BaseRenderContext.actionSink]
152
+ * to be eventually executed as the body of a [WorkflowAction]. If several actions get stacked
153
+ * up at once (think about accidental rapid taps on a button), that could take a while.
154
+ *
155
+ * If you require something to happen the instant a UI action happens, [eventHandler]
156
+ * is the wrong choice. You'll want to write your own call to `actionSink.send`:
157
+ *
158
+ * return SomeScreen(
159
+ * onClick = {
160
+ * // This happens immediately.
161
+ * MyAnalytics.log("SomeScreen was clicked")
162
+ *
163
+ * context.actionSink.send(
164
+ * action {
165
+ * // This happens eventually.
166
+ * state = SomeNewState
167
+ * }
168
+ * }
169
+ * }
170
+ * )
171
+ *
133
172
* @param name A string describing the update, included in the action's [toString]
134
173
* as a debugging aid
135
174
* @param update Function that defines the workflow update.
@@ -264,7 +303,7 @@ public interface BaseRenderContext<out PropsT, StateT, in OutputT> {
264
303
public fun <E1 , E2 , E3 , E4 , E5 , E6 , E7 , E8 , E9 > eventHandler (
265
304
name : () -> String = { "eventHandler" },
266
305
update : WorkflowAction <@UnsafeVariance PropsT , StateT , @UnsafeVariance OutputT >
267
- .Updater .(E1 , E2 , E3 , E4 , E5 , E6 , E7 , E8 , E9 ) -> Unit
306
+ .Updater .(E1 , E2 , E3 , E4 , E5 , E6 , E7 , E8 , E9 ) -> Unit
268
307
): (E1 , E2 , E3 , E4 , E5 , E6 , E7 , E8 , E9 ) -> Unit {
269
308
return { e1, e2, e3, e4, e5, e6, e7, e8, e9 ->
270
309
actionSink.send(action(name) { update(e1, e2, e3, e4, e5, e6, e7, e8, e9) })
@@ -274,7 +313,7 @@ public interface BaseRenderContext<out PropsT, StateT, in OutputT> {
274
313
public fun <E1 , E2 , E3 , E4 , E5 , E6 , E7 , E8 , E9 , E10 > eventHandler (
275
314
name : () -> String = { "eventHandler" },
276
315
update : WorkflowAction <@UnsafeVariance PropsT , StateT , @UnsafeVariance OutputT >
277
- .Updater .(E1 , E2 , E3 , E4 , E5 , E6 , E7 , E8 , E9 , E10 ) -> Unit
316
+ .Updater .(E1 , E2 , E3 , E4 , E5 , E6 , E7 , E8 , E9 , E10 ) -> Unit
278
317
): (E1 , E2 , E3 , E4 , E5 , E6 , E7 , E8 , E9 , E10 ) -> Unit {
279
318
return { e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 ->
280
319
actionSink.send(action(name) { update(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10) })
@@ -287,30 +326,30 @@ public interface BaseRenderContext<out PropsT, StateT, in OutputT> {
287
326
*/
288
327
public fun <PropsT , StateT , OutputT , ChildOutputT , ChildRenderingT >
289
328
BaseRenderContext <PropsT , StateT , OutputT >.renderChild (
290
- child : Workflow <Unit , ChildOutputT , ChildRenderingT >,
291
- key : String = "",
292
- handler : (ChildOutputT ) -> WorkflowAction <PropsT , StateT , OutputT >
293
- ): ChildRenderingT = renderChild(child, Unit , key, handler)
329
+ child : Workflow <Unit , ChildOutputT , ChildRenderingT >,
330
+ key : String = "",
331
+ handler : (ChildOutputT ) -> WorkflowAction <PropsT , StateT , OutputT >
332
+ ): ChildRenderingT = renderChild(child, Unit , key, handler)
294
333
295
334
/* *
296
335
* Convenience alias of [BaseRenderContext.renderChild] for workflows that don't emit output.
297
336
*/
298
337
public fun <PropsT , ChildPropsT , StateT , OutputT , ChildRenderingT >
299
338
BaseRenderContext <PropsT , StateT , OutputT >.renderChild (
300
- child : Workflow <ChildPropsT , Nothing , ChildRenderingT >,
301
- props : ChildPropsT ,
302
- key : String = ""
303
- ): ChildRenderingT = renderChild(child, props, key) { noAction() }
339
+ child : Workflow <ChildPropsT , Nothing , ChildRenderingT >,
340
+ props : ChildPropsT ,
341
+ key : String = ""
342
+ ): ChildRenderingT = renderChild(child, props, key) { noAction() }
304
343
305
344
/* *
306
345
* Convenience alias of [BaseRenderContext.renderChild] for children that don't take props or emit
307
346
* output.
308
347
*/
309
348
public fun <PropsT , StateT , OutputT , ChildRenderingT >
310
349
BaseRenderContext <PropsT , StateT , OutputT >.renderChild (
311
- child : Workflow <Unit , Nothing , ChildRenderingT >,
312
- key : String = ""
313
- ): ChildRenderingT = renderChild(child, Unit , key) { noAction() }
350
+ child : Workflow <Unit , Nothing , ChildRenderingT >,
351
+ key : String = ""
352
+ ): ChildRenderingT = renderChild(child, Unit , key) { noAction() }
314
353
315
354
/* *
316
355
* Ensures a [LifecycleWorker] is running. Since [worker] can't emit anything,
@@ -323,9 +362,9 @@ public fun <PropsT, StateT, OutputT, ChildRenderingT>
323
362
*/
324
363
public inline fun <reified W : LifecycleWorker , PropsT , StateT , OutputT >
325
364
BaseRenderContext <PropsT , StateT , OutputT >.runningWorker (
326
- worker : W ,
327
- key : String = ""
328
- ) {
365
+ worker : W ,
366
+ key : String = ""
367
+ ) {
329
368
runningWorker(worker, key) {
330
369
// The compiler thinks this code is unreachable, and it is correct. But we have to pass a lambda
331
370
// here so we might as well check at runtime as well.
@@ -348,9 +387,9 @@ public inline fun <reified W : LifecycleWorker, PropsT, StateT, OutputT>
348
387
)
349
388
public inline fun <reified W : Worker <Nothing >, PropsT , StateT , OutputT >
350
389
BaseRenderContext <PropsT , StateT , OutputT >.runningWorker (
351
- worker : W ,
352
- key : String = ""
353
- ) {
390
+ worker : W ,
391
+ key : String = ""
392
+ ) {
354
393
runningWorker(worker, key) {
355
394
// The compiler thinks this code is unreachable, and it is correct. But we have to pass a lambda
356
395
// here so we might as well check at runtime as well.
@@ -378,10 +417,10 @@ public inline fun <reified W : Worker<Nothing>, PropsT, StateT, OutputT>
378
417
*/
379
418
public inline fun <T , reified W : Worker <T >, PropsT , StateT , OutputT >
380
419
BaseRenderContext <PropsT , StateT , OutputT >.runningWorker (
381
- worker : W ,
382
- key : String = "",
383
- noinline handler : (T ) -> WorkflowAction <PropsT , StateT , OutputT >
384
- ) {
420
+ worker : W ,
421
+ key : String = "",
422
+ noinline handler : (T ) -> WorkflowAction <PropsT , StateT , OutputT >
423
+ ) {
385
424
runningWorker(worker, typeOf<W >(), key, handler)
386
425
}
387
426
@@ -396,11 +435,11 @@ public inline fun <T, reified W : Worker<T>, PropsT, StateT, OutputT>
396
435
@PublishedApi
397
436
internal fun <T , PropsT , StateT , OutputT >
398
437
BaseRenderContext <PropsT , StateT , OutputT >.runningWorker (
399
- worker : Worker <T >,
400
- workerType : KType ,
401
- key : String = "",
402
- handler : (T ) -> WorkflowAction <PropsT , StateT , OutputT >
403
- ) {
438
+ worker : Worker <T >,
439
+ workerType : KType ,
440
+ key : String = "",
441
+ handler : (T ) -> WorkflowAction <PropsT , StateT , OutputT >
442
+ ) {
404
443
val workerWorkflow = WorkerWorkflow <T >(workerType, key)
405
444
renderChild(workerWorkflow, props = worker, key = key, handler = handler)
406
445
}
0 commit comments