3.0.0 • Published 2 years ago

ca11back-queue v3.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

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");
1.4.3

2 years ago

3.0.0

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

2.0.0

2 years ago

1.2.0

2 years ago

1.3.0

2 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago

0.0.1

3 years ago

0.0.0

3 years ago