Is it possible to listen to a `ReceiveChannel` to ...
# coroutines
s
Is it possible to listen to a
ReceiveChannel
to get notified when it is closed, either through an error or without one (just normally closed)?
s
Thanks! This issue is closed… will the
Channel.invokeOnClose
make it into the library or not…? 🙂
a
SendChannel.invokeOnClose
exists. Isn't that what you want? If you want to wait for all received events you might be able to consume all events (doing nothing) and handle stuff afterwards
Or you could try adding a deferred call in invoke on close
s
It want a
ReceiveChannel.invokeOnClose
, if possible.
I used a work around by creating a brand new channel, the target, and sending events from the source channel using a for-loop. Surrounding this in a try will get me a close-with-error. A regular close will be signaled by the for-loop terminating. Still, it’s extra code (creating new channel, for-loop, sending to new channel) for just wanting to listen to a close.
l
@streetsofboston The function is there, the commit adding it closed this issue I reported. I now happily use it in my projects.
s
@louiscad Is
ReceiveChannel.invokeOnClose
there? I can’t find it…. it’s only available on
SendChannel
l
@streetsofboston Which kotlinx.coroutines version and which target (JVM? JS?)? You can see the declaration in the commit closing the issue also.
s
@louiscad Coroutines lib version 1.0.1 on JVM
l
@streetsofboston My bad, it's on
SendChannel
(which makes more sense though). I can definitely see and use it in 1.1.1
s
It makes a lot of sense on both types of channels, maybe even more so than on SendChannel. The ReceiveChannel would want both to receive values and to receive closing-events (with or without error) 🙂
l
In that case, open an issue on GitHub, explain your use case there and link it back here.
s
Will do! Thanks!