@@ -18,6 +18,7 @@ package app.cash.molecule
18
18
import androidx.compose.runtime.AbstractApplier
19
19
import androidx.compose.runtime.Composable
20
20
import androidx.compose.runtime.Composition
21
+ import androidx.compose.runtime.MonotonicFrameClock
21
22
import androidx.compose.runtime.Recomposer
22
23
import androidx.compose.runtime.snapshots.Snapshot
23
24
import kotlinx.coroutines.CoroutineScope
@@ -30,6 +31,13 @@ import kotlinx.coroutines.flow.channelFlow
30
31
import kotlinx.coroutines.job
31
32
import kotlinx.coroutines.launch
32
33
34
+ /* *
35
+ * Create a [Flow] which will continually recompose `body` to produce a stream of [T] values
36
+ * when collected.
37
+ *
38
+ * The [CoroutineScope] in which the returned [Flow] is collected must contain a
39
+ * [MonotonicFrameClock] key which controls when recomposition occurs.
40
+ */
33
41
@OptIn(ExperimentalCoroutinesApi ::class ) // Marked as stable in kotlinx.coroutines 1.6.
34
42
fun <T > moleculeFlow (body : @Composable () -> T ): Flow <T > {
35
43
return channelFlow {
@@ -42,6 +50,13 @@ fun <T> moleculeFlow(body: @Composable () -> T): Flow<T> {
42
50
}
43
51
}
44
52
53
+ /* *
54
+ * Launch a coroutine into this [CoroutineScope] which will continually recompose `body`
55
+ * to produce a [StateFlow] stream of [T] values.
56
+ *
57
+ * The [CoroutineScope] in which this [StateFlow] is created must contain a
58
+ * [MonotonicFrameClock] key which controls when recomposition occurs.
59
+ */
45
60
fun <T > CoroutineScope.launchMolecule (
46
61
body : @Composable () -> T ,
47
62
): StateFlow <T > {
@@ -62,6 +77,13 @@ fun <T> CoroutineScope.launchMolecule(
62
77
return flow!!
63
78
}
64
79
80
+ /* *
81
+ * Launch a coroutine into this [CoroutineScope] which will continually recompose `body`
82
+ * to invoke [emitter] with each returned [T] value.
83
+ *
84
+ * The [CoroutineScope] in which this [StateFlow] is created must contain a
85
+ * [MonotonicFrameClock] key which controls when recomposition occurs.
86
+ */
65
87
fun <T > CoroutineScope.launchMolecule (
66
88
emitter : (value: T ) -> Unit ,
67
89
body : @Composable () -> T ,
0 commit comments