Is there a way to tell from within an event handler callback which function and/or object emitted (called) the event?
Here is an example program:
var EventEmitter, ee, rand, obj;
EventEmitter = require("events").EventEmitter;
ee = new EventEmitter();
ee.on('do it', cFunc);
obj = {
maybeMe: true,
emitting: function() {
ee.emit('do it');
}
}
function aFunc() {
ee.emit('do it');
}
function bFunc() {
ee.emit('do it');
}
function cFunc() {
console.log('Who called me to do it? aFunc or bFunc or obj (obj.emitting)?');
}
rand = Math.random();
if (rand < .3) {
aFunc();
} else if (rand < .6) {
bFunc();
} else {
obj.emitting();
}
Also, another use, if the source of the emitted event is from a node.js built in module. For example, 'error' events. If I use a common callback for error handling, can this callback know who emitted the event which called it?