https://kotlinlang.org logo
#coroutines
Title
# coroutines
s

streetsofboston

02/06/2019, 6:30 PM
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

streetsofboston

02/06/2019, 6:35 PM
Thanks! This issue is closed… will the
Channel.invokeOnClose
make it into the library or not…? 🙂
a

Allan Wang

02/06/2019, 6:37 PM
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

streetsofboston

02/06/2019, 6:48 PM
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

louiscad

02/06/2019, 7:16 PM
@streetsofboston The function is there, the commit adding it closed this issue I reported. I now happily use it in my projects.
s

streetsofboston

02/06/2019, 7:55 PM
@louiscad Is
ReceiveChannel.invokeOnClose
there? I can’t find it…. it’s only available on
SendChannel
l

louiscad

02/06/2019, 7:57 PM
@streetsofboston Which kotlinx.coroutines version and which target (JVM? JS?)? You can see the declaration in the commit closing the issue also.
s

streetsofboston

02/06/2019, 7:58 PM
@louiscad Coroutines lib version 1.0.1 on JVM
l

louiscad

02/06/2019, 8:39 PM
@streetsofboston My bad, it's on
SendChannel
(which makes more sense though). I can definitely see and use it in 1.1.1
s

streetsofboston

02/06/2019, 8:41 PM
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

louiscad

02/06/2019, 8:48 PM
In that case, open an issue on GitHub, explain your use case there and link it back here.
s

streetsofboston

02/06/2019, 8:50 PM
Will do! Thanks!
2 Views