dimsuz
02/03/2022, 11:38 AMstateIn
which requires a CoroutineScope
?
val s1: StateFlow<Int>
val s2: StateFlow<Long>
val s3: StateFlow<Long> = combine(s1, s2) { v1, v2 ->
v1.toLong() + v2
}
// ^^^ can't do that, combine results in `Flow<Long>`,
// must do stateIn...
Joffrey
02/03/2022, 11:51 AMStateFlow
it would need to have a value (the current state) at all times, accessible without suspension. This means that the value needs to be kept up-to-date. stateIn
does exactly that: it launches a coroutine that reads from the source flow, and updates the current state. And to launch a coroutine... you need a scopedimsuz
02/03/2022, 11:53 AMJoffrey
02/03/2022, 11:54 AMdimsuz
02/03/2022, 12:30 PMStateFlow
...dimsuz
02/03/2022, 12:30 PMgildor
02/03/2022, 2:32 PMgildor
02/03/2022, 2:33 PMdimsuz
02/03/2022, 3:01 PMStateFlow
will highlight the contract to the consumer, i.e that this flow is conflating and always emits on subscription. But maybe I should cover that in docs instead and loosen this up...gildor
02/04/2022, 2:49 AMDragos Rachieru
02/04/2022, 8:36 AMcombine
can still change threads so the combine value may be emitted later(ex: after 2 seconds)gildor
02/04/2022, 9:02 AMcan still change threadsEvent without changing the thread you can just have delay() or any other suspend function