You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+40-5Lines changed: 40 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Molecule
2
2
3
-
Build a `StateFlow` stream using Jetpack Compose[^1].
3
+
Build a `StateFlow`or `Flow`stream using Jetpack Compose[^1].
4
4
5
5
```kotlin
6
6
fun CoroutineScope.launchCounter(): StateFlow<Int> = launchMolecule {
@@ -108,7 +108,7 @@ This model-producing composable function can be run with `launchMolecule`.
108
108
```kotlin
109
109
val userFlow = db.users()
110
110
val balanceFlow = db.balances()
111
-
val models = scope.launchMolecule {
111
+
val models:StateFlow<ProfileModel>= scope.launchMolecule {
112
112
ProfilePresenter(userFlow, balanceFlow)
113
113
}
114
114
```
@@ -126,6 +126,40 @@ fun Profile(models: StateFlow<ProfileModel>) {
126
126
}
127
127
```
128
128
129
+
For more information see [the `launchMolecule` documentation](https://cashapp.github.io/molecule/docs/latest/molecule-runtime/molecule-runtime/app.cash.molecule/launch-molecule.html).
130
+
131
+
### Flow
132
+
133
+
In addition to `StateFlow`s, Molecule can create regular `Flow`s.
134
+
The flow-returning function does not require a `CoroutineScope` as it will be inherited from the collector.
135
+
136
+
Here is the presenter example updated to use a regular `Flow`:
137
+
```kotlin
138
+
val userFlow = db.users()
139
+
val balanceFlow = db.balances()
140
+
val models:Flow<ProfileModel> = moleculeFlow {
141
+
ProfilePresenter(userFlow, balanceFlow)
142
+
}
143
+
```
144
+
145
+
And the counter example:
146
+
```kotlin
147
+
funcounter(): Flow<Int> = moleculeFlow {
148
+
val count by remember { mutableStateOf(0) }
149
+
150
+
LaunchedEffect(Unit) {
151
+
while (true) {
152
+
delay(1_000)
153
+
count++
154
+
}
155
+
}
156
+
157
+
count
158
+
}
159
+
```
160
+
161
+
For more information see [the `moleculeFlow` documentation](https://cashapp.github.io/molecule/docs/latest/molecule-runtime/molecule-runtime/app.cash.molecule/molecule-flow.html).
162
+
129
163
## Usage
130
164
131
165
Add the buildscript dependency and apply the plugin to every module which wants to call `launchMolecule` or define `@Composable` functions for use with Molecule.
The entrypoint to the library is [the `launchMolecule` function](https://cashapp.github.io/molecule/docs/latest/molecule-runtime/molecule-runtime/app.cash.molecule/launch-molecule.html) which is an extension on `CoroutineScope`.
172
-
That scope must contain a `MonotonicFrameClock` key which is used to determine when recomposition occurs and a new value is produced.
205
+
Molecule requires a `MonotonicFrameClock` key in your `CoroutineScope`.
206
+
This applies to [the `launchMolecule` extension's](https://cashapp.github.io/molecule/docs/latest/molecule-runtime/molecule-runtime/app.cash.molecule/launch-molecule.html) receiver and the scope in which you collect [the `moleculeFlow` function]()-returned flow.
207
+
The clock is used to determine when recomposition occurs and a new value is produced.
173
208
174
209
On Android, [`AndroidUiDispatcher.Main`](https://cashapp.github.io/molecule/docs/latest/molecule-runtime/molecule-runtime/app.cash.molecule/-android-ui-dispatcher/-companion/-main.html) can be used for running your composables on the main thread with recomposition synchronized to the frame rate.
175
210
For any other rate or to recompose on a background thread, create a [`BroadcastFrameClock`](https://developer.android.com/reference/kotlin/androidx/compose/runtime/BroadcastFrameClock) and a timer to invoke its `sendFrame` function at your desired rate.
176
211
177
212
### Testing
178
213
179
-
While the created `StateFlow` can be tested normally, the use of the frame clock to control recomposition makes it harder than it should be.
214
+
While the created `StateFlow`s and `Flow`s can be tested normally, the use of the frame clock to control recomposition makes it harder than it should be.
180
215
The 'molecule-testing' dependency provides a `testMolecule` function which simplifies your test code by managing the threading, coroutine scope, and frame clock for you.
0 commit comments