callback queue
npm i ca11back-queueconst CallbackQueue = require("ca11back-queue");
Class: CallbackQueue
The callbackQueue offers a synchronous execution of queued asynchronous functions (callbacks) and not blocking the eventloop. When pushing callbacks into the queue arguments can be passed over. Arguments can also be passed over when invoking the next function from within a queued callback. During the construction of a callbackQueue certain defaultArgs can be set.
new CallbackQueue([...initArgs])
initArgs optional
When initArgs are initialized they are set the become the first arguments of every callback located even before the next function argument.
callbackQueue.push(callback[, ...args])
calback <Function>
function callback([...initArgs, ]next[, ...args]) {}
initArgs
When initArgs were initialized they are the first arguments in every callback.
next <Function> Required!
next([...args]);
Every callback must take a next as first parameter and this is a function. Invoking the next function from within the callback triggers the next callback in queue to be invoked. When passing arguments to the next function these arguments are added on top of the initial arguments that were passed over to the push method.
args optional
A combination captured arguments:
1. arguments captured when invoking the the push method
2. arguments captured when invoking the next function from the previous callback in the queue.
1. arguments captured when invoking the the push method
2. arguments captured when invoking the next function from the previous callback in the queue.
The asynchronous function to push into queue is the callback parameter.
args optional
Arguments passed over when invoking the push methods are set to be the first arguments after the next function argument within the queued callback.
Returns this <CallbackQueue>
For chaining methods.
The first callback to be pushed is invoked immediately, more callbacks to be pushed are queued.
callbackQueue.clear()
Returns this <CallbackQueue>
Allows chaining methods.
Empties the queue, removing any queued callbacks and their arguments and sets the index to
0.
callbackQueue.destroy()
Empties the queue, removing any queued callbacks and their arguments and removes any references to functios and arguments. Make sure to remove all references to the instance so it can be garbage collected.
callbackQueue.index
Returns <integer>
The index keeps increasing untill it reaches the end of the queue, then the index is set to
0.
Readable property of the current index in the queue.
callbackQueue.lastIndex
Returns <Boolean>
The lastIndex is calculated as
index >= queue.length - 1.
Readable property that returns the state if the current index in the queue has reached the end of the queue.
callbackQueue.length
Returns <integer>
The length keeps increasing the more callbacks are pushed into the queue untill the index reaches the end of the queue, then queue is cleared and it's lenght becomes 0.
Readable property of the length of the queue.
Example
class MyModule {
#queue = null;
#c = 0;
constructor(...initArgs) {
this.#queue = new CallbackQueue(this, ...initArgs);
}
a() {
this.#queue.push(this.#afterTimeoutA, 1000);
return this;
}
#afterTimeoutA(self, initArg, next, msec) {
setTimeout(() => {
self.#privMethod();
next(console.log("finished a"));
}, msec);
}
#privMethod() {
console.log("counted priv:", ++this.#c);
}
b() {
this.#queue.push(this.#aAfterTimeoutB, 500);
return this;
}
#aAfterTimeoutB(self, initArg, next, msec) {
setTimeout(() => next(console.log("finished b")), msec);
}
destroy() {
this.#queue.push((self, initArg) => {
console.log(initArg);
this.#queue.destroy();
});
}
}
const inst1 = new MyModule({ "i am always the second argument": true });
inst1.a().b().a().b().a().b()
.b().b().b().a().a().a()
.destroy();
// FilestreamLogger uses a CallbackQueue under the hood.
const FilestreamLogger = require("filestream-logger");
const logger = {};
logger.log = new FilestreamLogger("log");
logger.log("this module uses ca11back-queue");
logger.log("to alter asynchronous flow");
logger.log("to appear synchronous");
logger.error = new FilestreamLogger("error", { extend: [logger.log] });
loger.error("and also used ca11back-queue");
loger.error("to ensure that multiple references");
loger.log("to the same instance");
loger.error("invoking their asynchronous methods");
logger.log("... suddenly gets destroyed");
logger.log.destroy();
// the next is queued in logger.log but the queue is also cleared
loger.error("do not collide with eachother");