on method

EventListener on(
  1. String event,
  2. Object context,
  3. EventCallback? callback
)

API to register for notification. It is mandatory to pass event name and callback parameters.

Implementation

EventListener on(String event, Object context, EventCallback? callback) {
  if (null == callback) {
    throw ArgumentError.notNull("callback");
  }

  // Check if the particular listener is there in the listeners collection
  // Return the listener instance, if already registered.
  EventListener? listener;

  Set<EventListener> subs =
      this._listeners.putIfAbsent(event, () => new Set<EventListener>());

  if (null == listener) {
    // Create new element.
    listener = new EventListener(event, context, callback, () {
      subs.remove(listener);
      if (subs.length == 0) {
        this._listeners.remove(listener?.eventName);
      }
    });
    subs.add(listener);
  }

  return listener;
}