How to name this pattern? Does similar exist? Queue of triggered callback -
i'll need name or identify existing pattern that. application i'm developing need create queue of callbacks fired event. once event triggered, consecutive callbacks added queue fired immediately.
in code like that:
var queue = new triggeredcallbackqueue(); queue.ontriggerornow(() => console.log('callback 1')); // nothing logged console queue.ontriggerornow(() => console.log('callback 2')); // nothing logged console queue.trigger(); // 'callback1' , 'callback 2' logged console queue.ontriggerornow(() => console.log('callback 3')); // 'callback 3' logged console queue.ontriggerornow(() => console.log('callback 4')); // 'callback 4' logged console
do know if such pattern exists , classified anywhere? how name it? think triggeredcallbackqueue
name?
i think it's promise
design pattern, changed api:
class triggeredcallbackqueue { constructor() { this.promise = new promise((res, rej) => { this._res = res; this._rej = rej; }); } trigger() { this._res(); } ontriggerornow(cb) { this.promise = this.promise.then(cb); } } var queue = new triggeredcallbackqueue(); queue.ontriggerornow(() => console.log('callback 1')); // nothing logged console queue.ontriggerornow(() => console.log('callback 2')); // nothing logged console settimeout(() => { queue.trigger(); // 'callback1' , 'callback 2' logged console }, 2000); settimeout(() => { queue.ontriggerornow(() => console.log('callback 3')); // 'callback 3' logged console queue.ontriggerornow(() => console.log('callback 4')); // 'callback 4' logged console }, 4000); settimeout(() => { queue.trigger(); // nothing happens }, 5000);
Comments
Post a Comment