subscribe

inline fun <T : Event> EventBus.subscribe(priority: Int = Priority.NORMAL, async: Boolean = false, noinline handler: (T) -> Unit): Subscription

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

val sub = bus.subscribe<PlayerJumpEvent> { e -> println(e.player) }
val sub2 = bus.subscribe<BlockBreakEvent>(Priority.HIGH) { e -> e.cancel() }
val sub3 = bus.subscribe<PacketReceived>(async = true) { e -> handle(e) }

Return

a Subscription whose Subscription.cancel removes this handler.

Parameters

async

If true, dispatches on the bus's executor; falls back to sync if none configured.


inline fun <T : Event> TypedEventBus<in T>.subscribe(priority: Int = Priority.NORMAL, async: Boolean = false, noinline handler: (T) -> Unit): Subscription

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

val sub = networkBus.subscribe<PacketReceived> { e -> handle(e) }
val sub2 = networkBus.subscribe<ConnectionLost>(Priority.HIGH) { reconnect() }
val sub3 = networkBus.subscribe<PacketReceived>(async = true) { e -> handle(e) }

Return

a Subscription whose Subscription.cancel removes this handler.

Parameters

async

If true, dispatches on the bus's executor; falls back to sync if none configured.