dimsuz
06/28/2020, 10:58 PMrunBlocking {
val events = BroadcastChannel<Int>(capacity = Channel.BUFFERED)
merge(events.asFlow(), flowOf(1,2,3))
.onStart { events.offer(42) }
.take(4)
.collect {
println("received $it")
}
}
This popped up while I was trying to write a unit test for a certain api and got me puzzled. It looks like event should be buffered in a non-blocking way.
I also use .take(4)
to unsubscribe from hot Flow, but it's that 42
never gets seen somehow.bezrukov
06/28/2020, 11:04 PMdimsuz
06/28/2020, 11:12 PM42
, hangs. It seems that scan
is somehow involved:
runBlocking {
val events = BroadcastChannel<Int>(capacity = Channel.BUFFERED)
events.asFlow()
.scan(1, { accumulator, value -> value })
.take(2)
.collect {
println("received $it")
if (it == 1) {
events.offer(42)
}
}
}