Skip to content

Dispatch and Listen

Jonathan edited this page Aug 26, 2019 · 8 revisions

Dispatch

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 eventType or uses an erased type.
  • Channel
    • Channel of listeners to dispatch to (@all means all).
  • Async variants
    • Dispatches the event asynchronously without blocking current thread.

Listener

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 listener

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 owner

Destruction

Events can have their properties destructed on listener function:

class MyListener {
  @Listener
  fun onBuy(event: BuyEvent, @Name("product") product: Product, @Name("amount") amount: Int) {
     ...
  }
}

Filtering

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.

Clone this wiki locally