Skip to content
runtoolkit edited this page Apr 13, 2026 · 1 revision

Queue

FIFO function queue with a configurable drain rate per flush.

Constructor

import { Queue } from './src/queue.js';

const q = new Queue({ rate: 1 }); // default: 1 item per flush

rate must be a positive integer. Throws RangeError otherwise.


Adding items

q.push(fn);              // add a function
q.pushMany([fn1, fn2]);  // add multiple

q.pushAs(fn, ctx);       // wraps fn so it receives ctx when called
                         // equivalent to: q.push(() => fn(ctx))

Only functions are accepted. Throws TypeError for other types.


Flushing

q.flush();     // calls up to `rate` items, returns count processed
q.flushAll();  // calls every item regardless of rate, returns count

When Engine is used, queue.flush() is called automatically every tick via the _queue channel.


Introspection

q.size();   // number of queued items
q.clear();  // discard all without running

Changing rate

q.setRate(5); // process up to 5 items per flush

Error handling

Errors thrown inside queued functions are caught and logged to console.error. The queue continues processing remaining items.

Clone this wiki locally