asFlow

inline fun <T : Event> EventBus.asFlow(priority: Int = Priority.NORMAL): Flow<T>

Returns a Flow that emits every event of type T posted to this EventBus.

The handler is registered when the flow is collected and unregistered when the collector is cancelled, via awaitClose.

When to use asFlow vs suspendHandler

Use asFlow when you need Flow operators (filter, map, take, merge, etc.). Use suspendHandler when you only need to handle each event with suspend logic

  • it registers synchronously and has no subscription timing concerns.

Subscription timing

The subscription is registered inside the collector coroutine body. In a tight kotlinx.coroutines.runBlocking + kotlinx.coroutines.launch context, use kotlinx.coroutines.Dispatchers.Unconfined when launching the collector to guarantee registration before posting:

val job = launch(Dispatchers.Unconfined) {
bus.asFlow<MyEvent>().take(N).collect { e -> handle(e) }
}
bus.post(MyEvent()) // safe - subscription guaranteed registered
job.join()

In a real coroutine scope (viewModelScope, lifecycleScope, etc.) the collector is already running when you post, so no special dispatcher is needed.

Parameters

priority

Dispatch priority; defaults to Priority.NORMAL.


inline fun <T : Event> TypedEventBus<in T>.asFlow(priority: Int = Priority.NORMAL): Flow<T>

Returns a Flow that emits every event of type T posted to this TypedEventBus.

Delegates to EventBus.asFlow on the underlying bus.

val networkBus = EventBus().typed<NetworkEvent>()
networkBus.asFlow<PacketReceived>().collect { e -> handle(e) }

Parameters

priority

Dispatch priority; defaults to Priority.NORMAL.