Reading from a channel using select() is nice until it gets closed.
You can see an example of that problem here:
val p1 = 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...