使用'*'
作爲事件名稱沒有特殊效果,它表現爲正常事件。
你可以看看event emitter code,看到唯一的特別事件名稱爲:
示例(repl.it code)
const {
EventEmitter
} = require('events');
class BlaBla extends EventEmitter {
methodCall(stuff) {
this.emit("*", {
stuff // <-- this gets passed as an argument to the handler for the '*' event
});
}
}
const b = new BlaBla();
b.on('a', (...args) => console.log('nope', ...args)); // <-- this doesn't run
b.on('b', (...args) => console.log('nope', ...args)); // <-- this doesn't run
b.on('*', (...args) => console.log('this gets called', ...args)); // <-- This runs
b.methodCall('this gets passed down');
輸出
this gets called { stuff: 'this gets passed down' }
如果在這個特定的項目時this.emit('*')
被稱爲所有事件處理程序被調用,他們可能手工做這個。
下面是它是如何做一個簡單的例子:
const eventNames = ['a', 'b', 'c'];
this.on('*',() =>
eventNames.forEach(event => this.emit(event))
);
你可以發佈一個鏈接到這個項目? – nem035
@ nem035 - 感謝您的幫助!項目鏈接 - github.com/liangzeng/cqrs –