我正在尝试设计一个场景,在特定事件被触发时,我想让一些听众执行某项任务。试图管理代码的S.R.P,我希望将侦听器放在不同的源文件中。我想知道使用事件Emitter是否可行。事件发射器只能在单个源文件上工作吗?
var events = require('events');
var em = new events.EventEmitter();
exports.saveScheme = function (req, res) {
var dal = dalFactory.createDAL(constants.SCHEME);
return new Promise.resolve(dal.PromiseSave(req.body))
.then(function(data){
var schemeId = data._id;
em.addListener('FirstEvent', function (data) {
console.log('First subscriber: ' + data);
});
em.emit('FirstEvent', 'Test event emitter');
}).catch(function(error){
console.log(error);
});
};
我的其他源文件是
var emitter = require('events').EventEmitter;
var em = new emitter();
//Subscribe FirstEvent
em.on('FirstEvent', function (data) {
console.log('First subscriber: ' + data);
});
答案 0 :(得分:6)
您创建的每个eventEmitter对象都是一个新实例,因此从第一个触发的事件不会在第二个触发,因此您的问题的答案是 - 不,它不可能。 但是,还有其他解决方案:
我认为最好的方法是创建一个集中的公共eventEmitter,如下所示:
//firstFile.js
var common = require('./common');
var commonEmitter = common.commonEmitter;
exports.saveScheme = function (req, res) {
var dal = dalFactory.createDAL(constants.SCHEME);
return new Promise.resolve(dal.PromiseSave(req.body))
.then(function(data){
var schemeId = data._id;
commonEmitter.addListener('FirstEvent', function (data) {
console.log('First subscriber: ' + data);
});
commonEmitter.emit('FirstEvent', 'Test event emitter');
}).catch(function(error){
console.log(error);
});
};
//secondFile.js
var common = require('./common');
var commonEmitter = common.commonEmitter;
//Subscribe FirstEvent
commonEmitter.on('FirstEvent', function (data) {
console.log('First subscriber: ' + data);
});
//common.js
var events = require('events');
var em = new events.EventEmitter();
module.exports.commonEmitter = em;
但是如果你想让源文件知道"知道"彼此 - 你可以这样做:
//firstFile.js
var events = require('events');
var em = new events.EventEmitter();
exports.saveScheme = function (req, res) {
var dal = dalFactory.createDAL(constants.SCHEME);
return new Promise.resolve(dal.PromiseSave(req.body))
.then(function(data){
var schemeId = data._id;
em.addListener('FirstEvent', function (data) {
console.log('First subscriber: ' + data);
});
em.emit('FirstEvent', 'Test event emitter');
}).catch(function(error){
console.log(error);
});
};
exports.emitter = em;
//secondFile.js
var firstFile = require('./firstFile');
var firstFileEmitter = firstFile.emitter;
//Subscribe FirstEvent
firstFileEmitter.on('FirstEvent', function (data) {
console.log('First subscriber: ' + data);
});
答案 1 :(得分:2)
事件监听器和发射器可以位于两个不同的文件中。请看下面的一个简单示例:
第一个文件,我们称之为 eventEx.js
var EventEmitter = require('events').EventEmitter
var myEmitter = new EventEmitter();
myEmitter.on('print', (arg) => {
console.log(arg);
})
exports.myEmitter = myEmitter
第二个文件如下:
var emitter = require('./eventEx.js')
var printEmitter = emitter.myEmitter
printEmitter.emit('print', 'how was Dunkirk?')
printEmitter.emit('print', 'Dunkirk was awesome!!')
答案 2 :(得分:1)
这就是我在测试中使用的方式。我将它与类语义一起使用
有一点需要注意的是,始终需要在发出事件之前注册一个侦听器,因为发出事件时,它会查找已注册的侦听器集,然后在那儿发出流。
//emitter.js
// The core module events has EventEmitter class which we are going to make use of
// note that we are not going to use that explicitly but by means of another class
const EventEmitter = require('events');
Class EventEmitterClass extends EventEmitter{
emitterMethod(){
this.emit('testEventEmitted', {obj:'testString object'});
}
}
module.exports = EventEmitterClass; // we export the module with the objectInstance
//listener.js
// now import the emitter.js and we get back a class - EventEmitterClass
const EventEmitterClass = require('./emitter.js');//give the actual path of js
const eventEmitterObj = new EventEmitterClass();
//now it is mandatory to register the listener before we even think of calling the //emitter method
eventEmitterObj.addListener('testEventEmitted', res => {
console.log('this is the result from the emitted event:', res);
});
//now call the method that emits the event in the class
eventEmitterObj.emitterMethod();
现在运行listener.js -> node listerner.js
对不起,如果我对事情的解释太详尽了
答案 3 :(得分:0)
我的第一个档案是
var events = require('events');
var em = new events.EventEmitter();
module.exports.saveScheme = function (req, res) {
em.emit('FirstEvent', 'Rahul event emitter');
};
exports.emitter = em;
第二个文件是
var firstFile = require('./abc');
var firstFileEmitter = firstFile.emitter;
//Subscribe FirstEvent
firstFileEmitter.on('FirstEvent', function (data) {
console.log('First subscriber: ' + data);
});