In Node.js, we have the standard event emitter:
const EE = require('events');
const ee = new EE();
ee.on('x', function(){
});
what I would like to do, is listen for when a client registers a listener. The purpose is so that if the emitter is in a particular state, it will take actions upon registration.
To be clear, when ee.on()
is called, for a particular event 'x', I want to take some actions.
How can I do this without monkey-patching all event emitters in Node.js?
If I do the following, am I patching the prototype method or the instance method?
let on = ee.on;
ee.on = function(){
if(ee.y === true){
process.next(function(){
ee.emit('done');
});
}
on.apply(ee, arguments);
};
this is of course the same as:
let on = ee.on;
ee.on = function(){
if(this.y === true){
process.next(() => {
this.emit('done');
});
}
on.apply(this, arguments);
};
I think this will work, but I am not sure. I think this will only effect this one particular event emitter, not all event emitters, because it will not change the prototype...
via Alexander Mills
No comments:
Post a Comment