emit method

  1. @mustCallSuper
bool emit(
  1. String eventName, [
  2. List? positionalArguments,
  3. Map<Symbol, dynamic>? namedArguments
])

Call all listeners registered for eventName event

All listeners found are called with positionalArguments and namedArguments

Listeners registered as once is then cleared

Implementation

@mustCallSuper
bool emit(
  String eventName, [
  List? positionalArguments,
  Map<Symbol, dynamic>? namedArguments,
]) {
  assert(eventName != null);
  assert(eventName.isNotEmpty);

  if (!_events.containsKey(eventName)) {
    return false;
  }

  final _listeners = listeners(eventName)!;
  final _onceListeners = <_KuzzleListener>[];

  for (final listener in _listeners) {
    if (listener.once!) {
      _onceListeners.add(listener);
    }

    try {
      Function.apply(listener.fn, positionalArguments, namedArguments);
    } on Exception {
      print('$runtimeType.emit($eventName) => listener.fn errored');
      print('Did the listener was registered correctly?');
    }
  }

  for (final listener in _onceListeners) {
    removeListener(eventName, listener.fn);
  }

  return true;
}