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

necati

09/01/2019, 11:19 PM
https://android.jlelse.eu/rxbus-kotlin-listen-where-ever-you-want-e6fc0760a4a8 Hello all, how can I create an event bus like this with current coroutine apis?
o

octylFractal

09/01/2019, 11:21 PM
dispatching events to listeners is up to you, but you can use
Channel
or
Flow
to setup async event transfer, e.g. by sending events to
Channel
and having something
receive
in a loop and dispatch
to replicate "rx", you probably want to use
Flow
--
publisher
field would be a flow derived from a
BroadcastChannel
, you would also store said channel.
publish(e)
would send to the channel, and
listen(Class)
would simply call
filterIsInstance
on the flow
a

asad.awadia

09/02/2019, 12:32 AM
How do you implement the listener
o

octylFractal

09/02/2019, 12:32 AM
collect
?
a

asad.awadia

09/02/2019, 12:33 AM
No i mean how to keep track of listeners since multiple
o

octylFractal

09/02/2019, 12:34 AM
for what?
n

necati

09/02/2019, 12:34 AM
created something like that but couldn’t be sure about
runBlocking
part
o

octylFractal

09/02/2019, 12:35 AM
ideally
send
should be
suspend
and if it needs to be blocking, use
runblocking
at that location, or add a blocking variant that uses
sendBlocking
also, it's probably better to store a single
asFlow()
return value, rather than making a new one each time, though I don't think there's a huge difference
n

necati

09/02/2019, 12:38 AM
in this case, i can only call
send
in a coroutine. i want to call it from adapters etc
o

octylFractal

09/02/2019, 12:39 AM
it's better to use
sendBlocking
only then
n

necati

09/02/2019, 12:44 AM
looks lovely, thanks