myanmarking
10/04/2021, 4:27 PM@Test
fun `test flow` (): Unit = runBlocking{
val flow = callbackFlow {
trySend(Unit)
delay(5000)
close()
awaitClose {
println("Closed")
}
}
val job = launch {
flow.collect { }
}
job.cancel()
}
The doc says on close and cancellation. But in this case, it is not called. Why ? When it is cancelled, awaitClose not called.Joffrey
10/04/2021, 4:38 PMdelay()
so the flow's body doesn't even have the time to register the awaitClose
callback, nor to close the underlying channel of the callbackFlow
. To be fair that's not really how callbackFlow
is designed to be used (it's rather meant to wrap listener based APIs)myanmarking
10/04/2021, 4:40 PMJoffrey
10/04/2021, 4:41 PMclose()
call. awaitClose()
will actually wait for cancellation