ca11back-queue
v3.0.0
Published
<pre><code>npm i ca11back-queue</code></pre>
Downloads
7
Readme
callback queue
const CallbackQueue = require("ca11back-queue");
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");