onEvent<E extends Event> method

void Function() onEvent<E extends Event>(
  1. void listener(
    1. E event
    )
)

Subscribes to events of type E. The returned closure removes the subscription when called. Subtype propagation: a derived event sent via sendEvent is delivered to base-typed listeners as well.

Implementation

void Function() onEvent<E extends Event>(void Function(E event) listener) {
  void wrapper(Event event) {
    // Closure captures E reified; the `is E` check is correct at runtime.
    if (event is E) listener(event);
  }

  _eventListeners.add(wrapper);
  return () => _eventListeners.remove(wrapper);
}