subscribe<T extends GameEvent> method
StreamSubscription<GameEvent>
subscribe<T extends GameEvent>(
- void onEvent(
- T event
- void onError(
- Object error,
- 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);
}
});
}