dequeue method

QueuedCommand? dequeue({
  1. bool filter(
    1. QueuedCommand
    )?,
})

Remove and return the highest-priority command, or null if empty.

An optional filter narrows the candidates: only commands for which the predicate returns true are considered.

Implementation

QueuedCommand? dequeue({bool Function(QueuedCommand)? filter}) {
  if (_commandQueue.isEmpty) return null;

  int bestIdx = -1;
  int bestPriority = 999;

  for (int i = 0; i < _commandQueue.length; i++) {
    final cmd = _commandQueue[i];
    if (filter != null && !filter(cmd)) continue;
    final priority = cmd.priority.order;
    if (priority < bestPriority) {
      bestIdx = i;
      bestPriority = priority;
    }
  }

  if (bestIdx == -1) return null;

  final dequeued = _commandQueue.removeAt(bestIdx);
  _notifySubscribers();
  _logOperation(QueueOperation.dequeue);
  return dequeued;
}