Typed Event Bus
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 errorJava (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.
Parameters
The root event type this bus accepts.
Properties
Functions
Returns true if at least one handler is registered for eventClass or any of its supertypes.
Returns true if at least one handler is registered for T or any of its supertypes.
Dispatches event asynchronously and returns a CompletableFuture that completes with the event once all handlers have finished.
Registers all instance methods on subscriber annotated with @Subscribe.
Registers a lambda handler for event type T with synchronous dispatch.
Registers a Consumer handler for event type T and returns a Subscription token.
Registers a lambda handler for event type T and returns a Subscription token.
Registers a lambda handler for event type T on this TypedEventBus and returns a Subscription token.
Registers a wildcard Consumer handler that fires for every event posted to the underlying bus.
Registers a wildcard handler that fires for every event posted to the underlying bus.
Registers a wildcard handler on this TypedEventBus with Priority.NORMAL as the default priority.
Registers all static methods on klass annotated with @Subscribe.
Removes all instance handlers previously registered for subscriber.
Removes all static handlers previously registered for klass.