add method

FutureOr<void> add(
  1. FutureOr<void> handler(), {
  2. Duration? buffer,
  3. _TOnPrevErr<T>? onPrevErr,
  4. bool? eagerError,
})

Adds a handler to the queue that processes the previous value.

The buffer duration can be used to throttle the execution.

Implementation

FutureOr<void> add(
  FutureOr<void> Function() handler, {
  Duration? buffer,
  _TOnPrevErr<T>? onPrevErr,
  bool? eagerError,
}) {
  final result = addSafe(
    (_) {
      final value = handler();
      switch (value) {
        case Future():
          return Async(() async {
            // TODO: false positive linter!
            // ignore: no_futures_allowed
            await value;
            return const None();
          });
        default:
          return syncNone();
      }
    },
    buffer: buffer,
    onPrevErr: onPrevErr,
    eagerError: eagerError,
  ).value;
  if (result is Future<Result<Option<T>>>) {
    return result.then<void>((e) {
      if (e.isErr()) {
        throw e;
      }
    });
  } else {
    if (result.isErr()) {
      throw result;
    }
  }
}