completeWith<R> method

FutureOr<R> completeWith<R>(
  1. FutureOr<R> consolodator(
    1. FutureOr<List<T>> values
    )
)

Evaluates all registered callbacks and returns the results, as determined by the consolodator function.

If any exceptions occur during the execution of callbacks, they are added to the exceptions list.

Implementation

FutureOr<R> completeWith<R>(
  FutureOr<R> Function(FutureOr<List<T>> values) consolodator,
) {
  final values = <FutureOr<T>>[];
  final sequential = Sequential();

  // Evaluate all callbacks and collect exceptions.
  for (final callback in _callbacks) {
    try {
      final test = sequential.add(callback);
      values.add(test);
      if (test is Future<T>) {
        test.catchError((Object e) {
          addException(e);
          return Future<T>.error(e);
        });
      }
    } catch (e) {
      addException(e);
    }
  }
  final last = sequential.last;
  if (last is Future) {
    return last.then(
      (_) => consolodator(Future.wait(values.map((e) async => await e))),
    );
  } else {
    return consolodator(values.map((e) => e as T).toList());
  }
}