add method

  1. @override
void add(
  1. Future<T> task
)
override

Wait for task to complete.

Implementation

@override
void add(Future<T> task) {
  if (_closed) throw StateError('The FutureGroup is closed.');

  // Ensure that future values are put into [values] in the same order they're
  // added to the group by pre-allocating a slot for them and recording its
  // index.
  var index = _values.length;
  _values.add(null);

  _pending++;
  task.then((value) {
    if (_completer.isCompleted) return null;

    _pending--;
    _values[index] = value;

    if (_pending != 0) return null;
    var onIdleController = _onIdleController;
    if (onIdleController != null) onIdleController.add(null);

    if (!_closed) return null;
    if (onIdleController != null) onIdleController.close();
    _completer.complete(_values.whereType<T>().toList());
  }).catchError((Object error, StackTrace stackTrace) {
    if (_completer.isCompleted) return null;
    _completer.completeError(error, stackTrace);
  });
}