<https://android.jlelse.eu/rxbus-kotlin-listen-whe...
# coroutines
n
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
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
How do you implement the listener
o
collect
?
a
No i mean how to keep track of listeners since multiple
o
for what?
n
created something like that but couldn’t be sure about
runBlocking
part
o
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
in this case, i can only call
send
in a coroutine. i want to call it from adapters etc
o
it's better to use
sendBlocking
only then
n
looks lovely, thanks