Joakim Forslund
12/13/2021, 10:01 AM.collect {}
on, however, I want to wait for a StateFlow/Boolean to be true before the collection can proceed, what is the recommended way of currently handling this? (None of the emits can be dropped, they need to wait for the StateFlow/Boolean to be true and emit as soon as it changes)simon.vergauwen
12/13/2021, 10:04 AMflow {
stateFlow.first { it == true }
originalFlow.collect(this)
}
You can create a new flow, that awaits the first true
signal from StateFlow
and then it collects the original flow.Joakim Forslund
12/13/2021, 10:56 AMJoffrey
12/13/2021, 1:48 PMcollect()
that is chained after the first()
call is a mistake/typo, first()
is already a terminal operatorsimon.vergauwen
12/13/2021, 1:50 PMursus
12/13/2021, 2:22 PMstateFlow.filter { it == true }.take(1).flatMap { originalFlow }
Joakim Forslund
12/13/2021, 9:48 PMJoakim Forslund
12/14/2021, 8:27 AMsuspend fun <T> Flow<T>.waitForTrue(stateFlow: StateFlow<Boolean>):Flow<T>{
stateFlow.first { it }
return this
}
Became the result, which seems kind of silly, but yeah first
is suspending without being terminating, so it is a way to solve it.Joffrey
12/14/2021, 8:35 AMJoakim Forslund
12/14/2021, 8:37 AM.collect
calls from proceeding?Joakim Forslund
12/14/2021, 8:38 AMsimon.vergauwen
12/14/2021, 8:51 AMFlow
which has no influence on the collection.Joakim Forslund
12/14/2021, 9:19 AMsuspend fun <T> Flow<T>.waitForTrue2(stateFlow: StateFlow<Boolean>): Flow<T> {
return flow {
stateFlow.first { it }
this@waitForTrue2.collect(this)
}
}
Would then adhere to this?Joakim Forslund
12/14/2021, 9:20 AM.collect
all together?simon.vergauwen
12/14/2021, 9:21 AMcollect
. The lambda inside flow { }
is executd when you call collect
. So first it will await the boolean, and then it’ll collect the original flow.Joakim Forslund
12/14/2021, 9:22 AMJoffrey
12/14/2021, 9:23 AMsuspend
keyword anymore 😉 Calling this operator immediately constructs the new flow without waiting. Collectors of this flow are the ones that will suspend when actually collectingJoakim Forslund
12/14/2021, 9:27 AM