jdemeulenaere
06/25/2020, 7:03 AMaddListener(listener)
and removeListener(listener)
methods) and replace it with a Flow based API. Unfortunately, the only way that I found to do it is by using the deprecated BroadcastChannel.openSubscription()
. The reason for this is that I want the hot flow returned by my interface to emits all events that happen right after it is returned, not right after we start to collect it. Here is an example, that highlight the issue : https://pl.kotl.in/LE8oUe6xg. I want the first collected click target to be foo
, but if I use BroadcastChannel.asFlow()
that's not what I get. Any idea how to do that in a proper way ?Zach Klippenstein (he/him) [MOD]
06/25/2020, 1:11 PMshareIn
is released, I think you could do something like this:
private val listeningJob = Job()
private val eventChannel = Channel<ClickEvent>(UNLIMITED)
val events: Flow<ClickEvent> =
eventChannel.receiveAsFlow()
.shareIn(CoroutineScope(listeningJob), replay = 0, sharingStart = WhileSubscribed())
fun startListening() {
val listener = {
eventChannel.offer(it)
}
registerListener(listener)
listeningJob.invokeOnCompletion {
unregisterListener(listener)
}
}
fun stopListening () {
listeningJob.cancel()
}
Unfortunately, the shareIn
operator hasn't been released yet.jdemeulenaere
06/26/2020, 11:37 AMrkeazor
06/27/2020, 12:31 AMrkeazor
06/27/2020, 12:32 AMelizarov
06/27/2020, 5:53 AMChannel
. You can convert it to the Flow
with .receiveAsFlow()
extension.