once<T extends VyuhEvent> method

  1. @override
DisposeFunction once<T extends VyuhEvent>(
  1. VyuhEventListener<T> listener
)
override

Subscribe to a single occurrence of an event of type T.

The listener will be called only once when an event of type T is emitted, then the subscription will be automatically cancelled. Call the returned function to cancel it before the event arrives.

Example:

vyuh.event.once<AppStartEvent>((event) {
  print('App started at ${event.timestamp}');
});

Implementation

@override
DisposeFunction once<T extends VyuhEvent>(VyuhEventListener<T> listener) {
  late final StreamSubscription<T> subscription;
  subscription = _eventBus!.on<T>().listen((event) {
    subscription.cancel();
    listener(event);
  });

  return () {
    subscription.cancel();
  };
}