clearQueue method

int clearQueue()

Clear all pending operations from the queue

Cancels all queued tasks without executing them. Active operations are not affected, and pending task handles complete with CanceledException.

Returns the number of queued tasks that were canceled.

Example:

final limit = fLimit(1);
final handles = <TaskHandle<int>>[];

// Add 100 tasks...
for (var i = 0; i < 100; i++) {
  handles.add(limit(() async => i));
}

limit.clearQueue(); // Cancel all pending queued tasks
print(limit.pendingCount); // 0

for (final handle in handles) {
  handle.catchError((_) => -1);
}

Implementation

int clearQueue() {
  final clearedTasks = _queue.clear();
  for (final task in clearedTasks) {
    task.cancelPendingTimers();
    task.handle._cancel('Task was canceled because the queue was cleared');
  }
  _notifyIdleIfNeeded();
  return clearedTasks.length;
}