( filter?: (cmd: QueuedCommand) => boolean, )
| 165 | * the existing while-loop patterns. |
| 166 | */ |
| 167 | export function dequeue( |
| 168 | filter?: (cmd: QueuedCommand) => boolean, |
| 169 | ): QueuedCommand | undefined { |
| 170 | if (commandQueue.length === 0) { |
| 171 | return undefined |
| 172 | } |
| 173 | |
| 174 | // Find the first command with the highest priority (respecting filter) |
| 175 | let bestIdx = -1 |
| 176 | let bestPriority = Infinity |
| 177 | for (let i = 0; i < commandQueue.length; i++) { |
| 178 | const cmd = commandQueue[i]! |
| 179 | if (filter && !filter(cmd)) continue |
| 180 | const priority = PRIORITY_ORDER[cmd.priority ?? 'next'] |
| 181 | if (priority < bestPriority) { |
| 182 | bestIdx = i |
| 183 | bestPriority = priority |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if (bestIdx === -1) return undefined |
| 188 | |
| 189 | const [dequeued] = commandQueue.splice(bestIdx, 1) |
| 190 | notifySubscribers() |
| 191 | logOperation('dequeue') |
| 192 | return dequeued |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Remove and return all commands from the queue. |
no test coverage detected