dispatch method

bool dispatch(
  1. ControlEvent emitted
)

Runs the one handler that answers emitted, if any.

Returns whether anything ran, so a device can report the event as handled.

Every match is found before the winner runs, so a handler is free to bind and unbind as it answers.

Implementation

bool dispatch(ControlEvent emitted) {
  List<_Control>? matched;

  for (final control in _controls) {
    if (!_eligible(control)) continue;
    if (!control.matchers.any((matcher) => matcher.accepts(emitted))) continue;
    (matched ??= []).add(control);
  }

  if (matched == null) return false;
  final winner = _winner(matched);
  if (winner == null) return false;

  winner.handler(emitted);
  return true;
}