-
Notifications
You must be signed in to change notification settings - Fork 0
Dispatch and Listen
Description of EventManager.dispatch parameters:
- Event
- Event to dispatch to listeners through a channel (or all).
- Dispatcher
- The instance who requested dispatch of event (only used in exceptions for identification).
- EventType
- Generic information of event, should be specified only if the event to dispatch does not correctly implement
eventTypeor uses an erased type.
- Generic information of event, should be specified only if the event to dispatch does not correctly implement
- Channel
- Channel of listeners to dispatch to (
@allmeans all).
- Channel of listeners to dispatch to (
- Async variants
- Dispatches the event asynchronously without blocking current thread.
Listeners should be registered through EventManager.registerListener or EventManager.registerListeners.
Note: RxEventSys provides RxJava Observable<T> implementation as EventSys listener.
Description of EventManager.registerListener* parameters:
- Owner
- Owner of the listener (only used in exceptions).
- EventType
- Type of event to listen.
- EventListener
- Listener to receive events (Priority, channel and ignoreCancelled are provided by the implementation of this interface).
- Listener (registerListeners)
- Instance of class with listener functions (see below).
Function listeners must be annotated with @Listener and have at least a single parameter of event to listen:
class MyListener {
@Listener
fun onBuy(event: BuyEvent) {
...
}
}
And registered with EventManager.registerListeners, example:
EventManager.registerListeners(this, MyListener()) // First is the ownerEvents can have their properties destructed on listener function:
class MyListener {
@Listener
fun onBuy(event: BuyEvent, @Name("product") product: Product, @Name("amount") amount: Int) {
...
}
}The @Filter annotation allow event filtering, you can specify one or multiple events to filter. When listener function is annotated with @Filter, it cannot have first event parameter, example:
class MyListener {
@Filter(BuyEvent::class)
@Listener
fun onBuy(@Name("product") product: Product, @Name("amount") amount: Int) {
...
}
}If you want to access the event instance (in cases where you're filtering multiple events), you need to set @Filter.useEventArg to true:
class MyListener {
@Filter(value = [BuyEvent::class], useEventArg = true)
@Listener
fun onBuy(event: BuyEvent, @Name("product") product: Product, @Name("amount") amount: Int) {
...
}
}Note that the event parameter must have a type assignable to all events specified in @Filter.