rxNext<T> function

Future<T> rxNext<T>(
  1. RxValueListenable<T> rx, {
  2. Function? onAction,
  3. Duration timeLimit = const Duration(seconds: 10),
})

Wait the next change of a RxNotifier. The timeLimit is 10 seconds by default. onAction callback execute after register listener.

Implementation

Future<T> rxNext<T>(
  RxValueListenable<T> rx, {
  Function? onAction,
  Duration timeLimit = const Duration(seconds: 10),
}) async {
  final completer = Completer<T>();
  final disposable = rxObserver<T>(
    () => rx.value,
    effect: completer.complete,
  );
  final actionResult = onAction?.call();
  assert(
    () {
      if (actionResult is Future) {
        throw FlutterError.fromParts(<DiagnosticsNode>[
          ErrorSummary('onAction returned a Future.'),
        ]);
      }
      return true;
    }(),
    'onAction returned a Future.',
  );
  final result = await completer.future.timeout(
    timeLimit,
    onTimeout: () => rx.value,
  );
  disposable();
  return result;
}