ansman
02/24/2019, 4:14 PMfun events(channel: SendChannel<Event>)
fun events(scope: CoroutineScope): ReceiveChannel<Event>
In my case I need to add a listener when calling events
and remove it when the channel is closedDominaezzz
02/24/2019, 4:17 PMansman
02/24/2019, 4:18 PMDominaezzz
02/24/2019, 4:18 PMevents
function.ansman
02/24/2019, 4:18 PMDominaezzz
02/24/2019, 4:20 PMansman
02/24/2019, 4:21 PMDominaezzz
02/24/2019, 4:22 PMfun CoroutineScope.events(): ReceiveChannel<Event>
is more idiomatic if you're a spawning a coroutine in the function.ansman
02/24/2019, 4:25 PMoverride fun events(scope: CoroutineScope): ReceiveChannel<Unit> =
scope.produce(capacity = Channel.CONFLATED) {
val listener = { offer(Unit) }
addListener(listener)
invokeOnClose { removeListener(listener) }
}
Dominaezzz
02/24/2019, 4:27 PMproduce
.ansman
02/24/2019, 4:28 PMDominaezzz
02/24/2019, 4:32 PMansman
02/24/2019, 4:34 PMDico
02/24/2019, 4:45 PMproduce
coroutine completes, when that is not the intention.
Here is a potential workaround:
produce {
// your stuff, done with procuding ...
suspendCoroutineCancellable<Unit> { cont ->
channel.invokeOnClose { cont.resume(Unit) }
}
}
ansman
02/24/2019, 4:46 PMDico
02/24/2019, 4:47 PMansman
02/24/2019, 4:53 PMDico
02/24/2019, 4:53 PM