I am dynamically creating event listener for some events. In that event listener i want to do another emit call based on the event name, so i want to get the event listener name
I am using node.js eventemitter.
var events = require('events').EventEmitter;
var util = require('util');
.....
.....
for(i in events) {
transport.on(events[i],function(userId) {
eventName = events[i];
var acct = accountsList[userId];
if(acct) {
acct.emit(events[i],userId);
}
});
}
The above method is working but the problem line is
acct.emit(events[i],userId);
that events[i] is having last value of the loop. so if received any event it always emitting the final loop of the events[i] value...
I am dynamically creating event listener for some events. In that event listener i want to do another emit call based on the event name, so i want to get the event listener name
I am using node.js eventemitter.
var events = require('events').EventEmitter;
var util = require('util');
.....
.....
for(i in events) {
transport.on(events[i],function(userId) {
eventName = events[i];
var acct = accountsList[userId];
if(acct) {
acct.emit(events[i],userId);
}
});
}
The above method is working but the problem line is
acct.emit(events[i],userId);
that events[i] is having last value of the loop. so if received any event it always emitting the final loop of the events[i] value...
Share Improve this question edited Mar 14, 2014 at 5:07 balaphp asked Mar 14, 2014 at 4:49 balaphpbalaphp 1,3265 gold badges20 silver badges41 bronze badges 1- Can we see a bit more code? Hard to tell what's happening without more context. – rossipedia Commented Mar 14, 2014 at 4:55
5 Answers
Reset to default 3So you are preserving value of event_name
in a closure. It is legal, but doesn't look very neat.
Instead, you could use EventEmitter2 module like this
var EventEmitter = require('eventemitter2').EventEmitter2;
var emitter = new EventEmitter();
emitter.on('ev1', function (line) {
console.log(this.event); // here's your fired event
});
emitter.emit('ev1', 'has fired');
Check out the documentation, you could do much more than the original EventEmitter
i overe this by function ...but i want to know this right way or not...
for(i in events) {
function test(event_name){
transport.purpleEvents.on(event_name,function(userId) {
var acct = accountsList[userId];
if(acct) {
acct.emit(event_name,userId);
}
});
}
test(events[i]);
}
The for loop you are using is async and results in all calls using the last value in events. If you replace that with forEach it will run sync. Try something like this untested code:
events.forEach(function(i) {
transport.on(events[i],function(userId) {
eventName = events[i];
var acct = accountsList[userId];
if(acct) {
acct.emit(events[i],userId);
}
});
});
you can use a closure like this which can be practical soln..
for(i in events) {
transport.on(events[i],closureplusopn(events[i]))
}
function closureplusopn(eventName){
return function(userID){
var acct = accountsList[userId];
if(acct) {
acct.emit(eventName,userID);
}
}
}
You can pass the event name through the context param. On EventEmitter3 the .on function params are (eventName, functionName, context). What helped me figure out how to get the eventName in the callback function was to pass this string as context and access it with the this keyword. Hope this helps whoever es to this question and has something like this as an issue.