イベント名として'*'
を使用しても特別な効果はありませんが、通常のイベントとして動作します。
あなたが
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 –