EventBus

interface EventBus

Central event dispatch interface.

Obtain an instance via the companion factory:

val bus = EventBus()

From Java:

EventBus bus = EventBus.create();

Subscribing

Pass any object whose methods are annotated with @Subscribe:

bus.subscribe(myListener)

Pass a class (or Kotlin object) to register @JvmStatic / companion-object handlers:

bus.subscribeStatic(MyListener::class.java)

Posting

bus.post(MyEvent())

For Cancellable events the bus stops dispatching as soon as Cancellable.isCancelled is true.

Error handling

By default, exceptions thrown inside handlers are printed to stderr. Supply a custom handler at construction time via EventBus.invoke:

val bus = EventBus { e -> logger.error("Handler error", e) }

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
abstract fun isListening(eventClass: Class<out Event>): Boolean

Returns true if at least one handler is currently registered for the given event type or any of its supertypes.

Link copied to clipboard
inline fun <T : Event> EventBus.isListening(): Boolean

Returns true if at least one handler is registered for T or any of its supertypes.

Link copied to clipboard
abstract fun isListeningAll(): Boolean

Returns true if at least one wildcard handler is currently registered.

Link copied to clipboard
abstract fun <T : Event> post(event: T): T

Dispatches event to all registered handlers whose parameter type matches event's runtime type or any of its supertypes, in descending priority order.

Link copied to clipboard
abstract fun subscribe(subscriber: Any)

Registers all instance methods on subscriber annotated with @Subscribe.

open fun <T : Event> subscribe(eventClass: Class<T>, priority: Int, handler: Consumer<T>): Subscription

Registers a java.util.function.Consumer as a handler for eventClass.

abstract fun subscribe(eventClass: Class<out Event>, priority: Int, handler: (Event) -> Unit): Subscription

Kotlin-facing handler registration. Hidden from Java via @JvmSynthetic; Java callers use the java.util.function.Consumer overload below.

Link copied to clipboard
inline fun <T : Event> EventBus.subscribe(priority: Int = Priority.NORMAL, noinline handler: (T) -> Unit): Subscription

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

Link copied to clipboard
open fun subscribeAll(priority: Int, handler: Consumer<Event>): Subscription

Java-facing wildcard handler registration. Kotlin callers use the subscribeAll extension which provides a default Priority.NORMAL value.

abstract fun subscribeAll(priority: Int, handler: (Event) -> Unit): Subscription

Registers a wildcard handler that fires for every event posted to this bus, regardless of type. Returns a Subscription token for removal.

Link copied to clipboard
fun EventBus.subscribeAll(priority: Int = Priority.NORMAL, handler: (Event) -> Unit): Subscription

Registers a wildcard handler with Priority.NORMAL default.

Link copied to clipboard
abstract fun subscribeStatic(klass: Class<*>)

Registers all static methods on klass annotated with @Subscribe.

Link copied to clipboard
inline fun <E : Event> EventBus.typed(): TypedEventBus<E>

Returns a TypedEventBus view over this bus restricted to subtypes of E.

fun <E : Event> EventBus.typed(rootClass: Class<E>): TypedEventBus<E>

Java interop overload of typed. The rootClass token is unused at runtime (generics are erased) but satisfies Java's type inference:

Link copied to clipboard
abstract fun unsubscribe(subscriber: Any)

Removes all instance handlers previously registered for subscriber.

Link copied to clipboard
abstract fun unsubscribeAll()

Removes all handlers from this bus; both instance, static, lambda-based, and wildcard. Useful for teardown in tests or on world unload.

Link copied to clipboard
abstract fun unsubscribeStatic(klass: Class<*>)

Removes all static handlers previously registered for klass.