subscribe<T extends GameEvent> method

StreamSubscription<GameEvent> subscribe<T extends GameEvent>(
  1. void onEvent(
    1. T event
    ), {
  2. void onError(
    1. Object error,
    2. StackTrace stackTrace
    )?,
})

Subscribes to events of type T (or a subtype). If onEvent throws, the error is caught and passed to onError (if given) rather than rethrown — one failing subscriber never crashes the bus or any other subscriber.

Implementation

StreamSubscription<GameEvent> subscribe<T extends GameEvent>(
  void Function(T event) onEvent, {
  void Function(Object error, StackTrace stackTrace)? onError,
}) {
  return _controller.stream.where((event) => event is T).listen((event) {
    try {
      onEvent(event as T);
    } catch (error, stackTrace) {
      onError?.call(error, stackTrace);
    }
  });
}