TypedEventBus

interface TypedEventBus<E : Event>

Type-safe view over an EventBus that restricts posting and subscribing to subtypes of E.

Kotlin (reified):

sealed interface NetworkEvent : Event
class PacketReceived(val bytes: ByteArray) : NetworkEvent

val networkBus = EventBus().typed<NetworkEvent>()
networkBus.post(PacketReceived(bytes)) // OK
// networkBus.post(ButtonClickEvent()) // compile error

Java (class token):

TypedEventBus<NetworkEvent> networkBus =
TypedEventBusFactory.typed(underlying, NetworkEvent.class);

The compile-time guarantee is strongest when E is a sealed interface or sealed class; no external type can satisfy T : E, so the constraint cannot be bypassed from outside the module.

TypedEventBus<E> is a thin adapter with zero runtime overhead beyond a single delegation. The underlying bus is accessible via delegate for operations outside this interface (e.g. EventBus.subscribeStatic, EventBus.unsubscribeAll). Handlers registered on either the typed view or the underlying bus are visible to both.

Note: subscribeAll accepts (Event) -> Unit rather than (E) -> Unit because wildcard handlers observe the raw underlying bus and may see events outside E if the bus is shared between multiple typed views.

Type Parameters

E

The root event type this bus accepts.

Properties

Link copied to clipboard
abstract val delegate: EventBus

The underlying untyped EventBus backing this typed view.

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> TypedEventBus<*>.isListening(): Boolean

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

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

Dispatches event to all registered handlers in descending priority order and returns it.

Link copied to clipboard
abstract fun <T : E> 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 : E> subscribe(eventClass: Class<T>, priority: Int, handler: Consumer<T>): Subscription

Registers a Consumer handler for event type T with synchronous dispatch.

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

Registers a lambda handler for event type T with synchronous dispatch.

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

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

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

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

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

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

Registers a wildcard Consumer handler that fires for every event posted to the underlying bus.

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

Registers a wildcard handler that fires for every event posted to the underlying bus.

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

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

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

Registers all static methods on klass annotated with @Subscribe.

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

Removes all instance handlers previously registered for subscriber.

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

Removes all static handlers previously registered for klass.