Lukas Lechner
11/24/2022, 11:17 AMsuspend fun main(): Unit = coroutineScope {
val sharedFlow = MutableSharedFlow<Int>()
launch {
sharedFlow
.collect {
println("Process $it")
delay(1000)
println("$it processed")
}
}
launch {
// 1 should be processed
sharedFlow.emit(1)
println("sharedFlow emits 1")
// 2 should not be processed since downstream is busy
sharedFlow.emit(2)
println("sharedFlow emits 2")
// 3 should be processed again
delay(2000)
sharedFlow.emit(3)
println("sharedFlow emits 3")
}
}
I tried BufferOverflow.DROP_LATEST, but there we need to have a buffer value > 0Lukas Lechner
11/24/2022, 11:27 AMChris Fillmore
11/24/2022, 11:47 AMextraBufferCapacity
to its constructor.Casey Brooks
11/24/2022, 1:20 PMDROP_LATEST
only works when there is some “buffer” (either replay
or extraBufferCapacity
), and the behavior is the same with both Channels and SharedFlows. I’m not really sure why this is the case, but I guess it needs that internal buffer to know when its full or busy?franztesca
11/24/2022, 1:55 PMJoffrey
11/24/2022, 2:09 PMcollect
call by the time you emit the first element to the shared flow