Queue
Extends: EventEmitter
Source: queue.js:61
An ordered, event-driven task runner.
Queue extends Node's EventEmitter, so the inheritance chain and the emitted events both show up in the generated page.
Tasks pushed after
Queue#clearruns are rejected — clearing aborts the in-flight signal.
Fires
Queue#event:drainQueue#event:error
Listens
module:forge/cache.Cache#event:evict
Example
Draining a queue
const q = new Queue({ concurrency: 2 });
q.on('drain', () => console.log('done'));
q.push({ id: 't1', run: async () => doWork() });
await q.drain();- Since: 1.1.0
- Version: 1.3.0
- Copyright: 2024 The clean-jsdoc-theme team
- Author: The clean-jsdoc-theme team
- Requires:
module:forge/cache - TODO:
- Add backpressure when the pending list exceeds a high-water mark.
Constructor
new Queue([options])
Parameters
options(Object, optional, default: "{}") — Queue configuration.options.concurrency(number, optional, default: 1) — How many tasks run at once (clamped to MAX_CONCURRENCY).options.autoStart(boolean, optional, default: true) — Start draining as soon as a task is pushed.
Instance Methods
push(task) -> this
Add a task to the queue, inserted by Priority.
Parameters
task(Task) — The task to enqueue.
Returns
this— The queue, for chaining.
drain() -> Promise.<number>
Run every pending task and resolve once the queue is empty.
Returns
Promise.<number>— The number of tasks that ran.
Fires
Queue#event:drain
Example
const ran = await queue.drain();tasks()
Iterate pending tasks in scheduling order, highest priority first.
Yields
Task— The next pending task.
Example
The @lang directive forces this block's language
for (const task of queue.tasks()) {
console.log(task.id satisfies string);
}onError(error) -> void
Handle a task failure. Subclasses must implement this.
Parameters
error(Error) — The error a task threw.
Returns
void
_next() -> Task | undefined
Pull and start the next task. Internal scheduling step.
Returns
Task | undefined— The task that was started, if any.
clear() -> void
Stop and clear the queue, aborting any in-flight task.
Returns
void
Instance Fields
concurrency
Effective concurrency, clamped to MAX_CONCURRENCY.
Type
number