如何命名这个模式?类似的存在吗?触发回调队列
How to name this pattern? Does similar exist? Queue of triggered callback
我需要你的帮助来命名或识别这样的现有模式。对于我正在开发的应用程序,我需要创建一个由事件触发的回调队列。一旦事件被触发,添加到队列中的连续回调将立即被触发。
在代码中它会是这样的:
var queue = new TriggeredCallbackQueue();
queue.onTriggerOrNow(() => console.log('Callback 1'));
// Nothing is logged to the console
queue.onTriggerOrNow(() => console.log('Callback 2'));
// Nothing is logged to the console
queue.trigger(); // 'Callback1' and 'Callback 2' are logged to console
queue.onTriggerOrNow(() => console.log('Callback 3'));
// 'Callback 3' is logged to the console
queue.onTriggerOrNow(() => console.log('Callback 4'));
// 'Callback 4' is logged to the console
你知道这样的模式是否已经存在并且在任何地方都被分类了吗?你会怎么命名它?你觉得 TriggeredCallbackQueue
名字怎么样?
我认为是 Promise
设计模式,稍作改动 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 is logged to the console
queue.onTriggerOrNow(() => console.log('Callback 2'));
// Nothing is logged to the console
setTimeout(() => {
queue.trigger(); // 'Callback1' and 'Callback 2' are logged to console
}, 2000);
setTimeout(() => {
queue.onTriggerOrNow(() => console.log('Callback 3'));
// 'Callback 3' is logged to the console
queue.onTriggerOrNow(() => console.log('Callback 4'));
// 'Callback 4' is logged to the console
}, 4000);
setTimeout(() => {
queue.trigger(); // Nothing happens
}, 5000);
我需要你的帮助来命名或识别这样的现有模式。对于我正在开发的应用程序,我需要创建一个由事件触发的回调队列。一旦事件被触发,添加到队列中的连续回调将立即被触发。
在代码中它会是这样的:
var queue = new TriggeredCallbackQueue();
queue.onTriggerOrNow(() => console.log('Callback 1'));
// Nothing is logged to the console
queue.onTriggerOrNow(() => console.log('Callback 2'));
// Nothing is logged to the console
queue.trigger(); // 'Callback1' and 'Callback 2' are logged to console
queue.onTriggerOrNow(() => console.log('Callback 3'));
// 'Callback 3' is logged to the console
queue.onTriggerOrNow(() => console.log('Callback 4'));
// 'Callback 4' is logged to the console
你知道这样的模式是否已经存在并且在任何地方都被分类了吗?你会怎么命名它?你觉得 TriggeredCallbackQueue
名字怎么样?
我认为是 Promise
设计模式,稍作改动 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 is logged to the console
queue.onTriggerOrNow(() => console.log('Callback 2'));
// Nothing is logged to the console
setTimeout(() => {
queue.trigger(); // 'Callback1' and 'Callback 2' are logged to console
}, 2000);
setTimeout(() => {
queue.onTriggerOrNow(() => console.log('Callback 3'));
// 'Callback 3' is logged to the console
queue.onTriggerOrNow(() => console.log('Callback 4'));
// 'Callback 4' is logged to the console
}, 4000);
setTimeout(() => {
queue.trigger(); // Nothing happens
}, 5000);