EventBus

interface EventBus

Central event dispatch interface.

val bus = EventBus()
bus.subscribe(MyListener())
bus.post(MyEvent())

Java:

EventBus bus = EventBus.create();
bus.subscribe(new MyListener());
bus.post(new MyEvent());
// With async dispatch:
EventBus bus = EventBus.create(Executors.newVirtualThreadPerTaskExecutor());

Handlers are registered via annotation scanning (subscribe) or lambda (subscribe overloads). For Cancellable events, dispatch stops as soon as any handler sets Cancellable.isCancelled to true. Exceptions thrown inside handlers are routed to the configured exceptionHandler and never propagate out of post or postAsync.

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 registered for eventClass 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 in descending priority order and returns it.

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

Dispatches event asynchronously and returns a CompletableFuture that completes with the event once all handlers have finished.

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 Consumer handler for eventClass with synchronous dispatch.

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

Registers a lambda handler for eventClass with synchronous dispatch.

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

Registers a Consumer handler for eventClass and returns a Subscription token.

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

Registers a lambda handler for eventClass and returns a Subscription token.

Link copied to clipboard
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.

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

Registers a wildcard Consumer handler that fires for every posted event.

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

Registers a wildcard handler that fires for every posted event regardless of type.

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

Registers a wildcard handler with Priority.NORMAL as the default priority.

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>
fun <E : Event> EventBus.typed(rootClass: Class<E>): TypedEventBus<E>

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

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 registered handlers from this bus: instance, static, lambda, and wildcard.

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

Removes all static handlers previously registered for klass.