suspendHandler

inline fun <T : Event> EventBus.suspendHandler(priority: Int = Priority.NORMAL, behaviour: SuspendHandlerBehaviour = SuspendHandlerBehaviour.Parallel, scope: CoroutineScope? = null, crossinline handler: suspend (T) -> Unit): Subscription

Registers a suspend handler for event type T and returns a Subscription token.

The subscription is registered synchronously before returning. Events can be posted immediately after this call returns without any timing concerns.

val sub = bus.suspendHandler<PlayerJumpEvent> { e ->
delay(100)
println(e.player)
}
sub.cancel()

Parameters

priority

Dispatch priority; defaults to Priority.NORMAL.

behaviour

Controls concurrency when events arrive faster than handlers complete.

scope

Coroutine scope for handler execution. When null (default), an internal scope is created and cancelled when the subscription is cancelled. When an explicit scope is provided, it is used but never cancelled by this subscription - the caller retains full lifecycle ownership. Prefer providing an explicit scope in production code.

handler

The suspend function invoked for each matching event.


inline fun <T : Event> TypedEventBus<in T>.suspendHandler(priority: Int = Priority.NORMAL, behaviour: SuspendHandlerBehaviour = SuspendHandlerBehaviour.Parallel, scope: CoroutineScope? = null, crossinline handler: suspend (T) -> Unit): Subscription

Registers a suspend handler for event type T on this TypedEventBus and returns a Subscription token.

Delegates to EventBus.suspendHandler on the underlying bus.

val networkBus = EventBus().typed<NetworkEvent>()
val sub = networkBus.suspendHandler<PacketReceived>(scope = viewModelScope) { e ->
delay(100)
handle(e)
}
sub.cancel()

Parameters

priority

Dispatch priority; defaults to Priority.NORMAL.

behaviour

Controls concurrency when events arrive faster than handlers complete.

scope

Coroutine scope for handler execution. When null (default), an internal scope is created and cancelled when the subscription is cancelled. When an explicit scope is provided, it is used but never cancelled by this subscription - the caller retains full lifecycle ownership. Prefer providing an explicit scope in production code.

handler

The suspend function invoked for each matching event.