once method

RxReaction once(
  1. void callback(
    1. T
    ), {
  2. bool condition(
    1. T
    )?,
})

the callback will be executed only 1 time

If condition is not null the callback only is called if condition returns true.

Implementation

RxReaction once(void Function(T) callback, {bool Function(T)? condition}) {
  late RxReaction reaction;
  // ignore: cancel_subscriptions
  StreamSubscription subscription = stream.listen((event) {
    if (condition != null) {
      if (condition(event)) {
        callback(event);
        reaction.dispose();
      }
    } else {
      callback(event);
      reaction.dispose();
    }
  });
  return reaction = RxReaction(subscription, null);
}