add<T> method

Future<T> add<T>(
  1. Future<T> closure(), {
  2. bool addToFront = false,
})

Adds the future-returning closure to the queue.

It will be executed after futures returned by preceding closures have been awaited if addToFront is false. If addToFront is true it gets executed within the next free slot by being added to the front of the queue.

Will throw an exception if the queue has been cancelled.

Implementation

Future<T> add<T>(Future<T> Function() closure, {bool addToFront = false}) {
  if (isCancelled) throw QueueCancelledException();
  final completer = Completer<T>();
  final item = _QueuedFuture<T>(closure, completer, timeout);
  addToFront ? _nextCycle.insert(0, item) : _nextCycle.add(item);
  _updateRemainingItems();
  unawaited(_process());
  return completer.future;
}