Skip to content

Commit 07ba5d8

Browse files
Improve the kdoc on the newly-public collectToSink.
1 parent d60440d commit 07ba5d8

File tree

1 file changed

+23
-7
lines changed
  • workflow-core/src/commonMain/kotlin/com/squareup/workflow1

1 file changed

+23
-7
lines changed

workflow-core/src/commonMain/kotlin/com/squareup/workflow1/Sink.kt

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,34 @@ public fun <T1, T2> Sink<T1>.contraMap(transform: (T2) -> T1): Sink<T2> = Sink {
4040

4141
/**
4242
* Collects from a [Flow] by converting each item into a [WorkflowAction] and then sending them
43-
* to the [actionSink]. This operator propagates back pressure from the workflow runtime, so if there
44-
* is a lot of contention on the workflow runtime the flow will be suspended while the action is
45-
* queued.
43+
* to the [actionSink]. This may be used as an alternative to a [asWorker] for certain [Flow]s.
44+
*
45+
* Unlike merely calling [collect][Flow.collect] yourself and emitting an action directly to the
46+
* sink, this operator propagates back pressure from the workflow runtime, so if there is a lot of
47+
* contention on the workflow runtime the flow will be suspended while the action is queued and only
48+
* resumed _after_ the action has been applied.
4649
*
4750
* Example:
48-
* ```
49-
* context.runningSideEffect("collector") {
50-
* myFlow.collectToSink(context.actionSink) { value ->
51-
* action("collect") { setOutput(value) }
51+
* ```kotlin
52+
* class MyWorkflow(
53+
* private val myFlow: Flow<…>
54+
* ) {
55+
*
56+
* // Omitting types for brevity.
57+
* override fun render() {
58+
* context.runningSideEffect("collector") {
59+
* myFlow.collectToSink(context.actionSink) { value ->
60+
* action("collect") { setOutput(value) }
61+
* }
62+
* }
5263
* }
5364
* }
5465
* ```
66+
*
67+
* > **Warning:** Be careful using this from `runningSideEffect` when the source [Flow] comes from
68+
* > state or props. The side effect will capture those values when it first runs, and if the instance
69+
* > of the flow changes in a future render pass, the side effect will still be collecting the stale
70+
* > [Flow]. In this case
5571
*/
5672
public suspend fun <T, PropsT, StateT, OutputT> Flow<T>.collectToSink(
5773
actionSink: Sink<WorkflowAction<PropsT, StateT, OutputT>>,

0 commit comments

Comments
 (0)