TypedEventBus

interface TypedEventBus<E : Event>

A type-safe view over an EventBus that restricts post and subscribe to a specific event hierarchy E. Provides compile-time enforcement that only subtypes of E can be posted or subscribed to through this interface.

Obtaining an instance

Kotlin (reified):

sealed interface NetworkEvent : Event
class PacketReceived(val bytes: ByteArray) : NetworkEvent
class ConnectionLost(val reason: String) : 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);

Sealed hierarchies

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 circumvented from outside the module.

Wildcard listeners

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

Underlying bus

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

Parameters

E

The root event type this bus accepts. All posted events must be subtypes of E.

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 supertype.

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

Posts event to the bus and returns it after all handlers have been called. The event type T is compile-time enforced to be a subtype of E.

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 java.util.function.Consumer handler for event type T and returns a Subscription token for removal.

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

Registers a lambda handler for event type T and returns a Subscription token. Hidden from Java; Java callers use the java.util.function.Consumer overload.

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

Registers a lambda handler for event type T on this TypedEventBus.

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

Registers a wildcard java.util.function.Consumer handler that fires for every event posted to the underlying bus. Returns a Subscription token for removal.

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

Registers a wildcard handler that fires for every event posted to the underlying bus, regardless of type. Returns a Subscription token for removal. Hidden from Java; Java callers use the java.util.function.Consumer overload.

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 default.

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 handlers previously registered by subscribe for the given subscriber.

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

Removes all static handlers previously registered for klass.