add method

void add (Future<T> task)
override

Wait for task to complete.

Implementation

void add(Future<T> task) {
  if (_closed) throw new 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;
    if (_onIdleController != null) _onIdleController.add(null);

    if (!_closed) return null;
    if (_onIdleController != null) _onIdleController.close();
    _completer.complete(_values);
  }).catchError((error, stackTrace) {
    if (_completer.isCompleted) return null;
    _completer.completeError(error, stackTrace);
  });
}