Selecting on close
Reading from a channel using select()
is nice until it gets closed.
You can see an example of that problem here:
valp1 = produce {
repeat(10) {
send("A")
}
}
val p2 = produce {
repeat(5) {
send("B")
}
}
runBlocking {
repeat(15) {
val result = selectUnbiased<String> {
p1.onReceive {
it
}
p2.onReceive {
it
}
}
println(result)
}
}
Although the numbers add up, we may often receive ClosedReceiveChannelException
running this code. That's because the second producer has fewer items, and as soon as it finishes, it will close its channel.
To avoid that, we can use onReceiveOrNull
, which will return a nullable version at the same time. Once the channel gets closed, we'll receive null
in our select
.
We can handle this null value in any way we want, for example, by making use of the elvis
operator:
repeat(15) { val result = selectUnbiased<String> { p1.onReceiveOrNull { // Can throw my own exception...