简体   繁体   中英

How to get event name in node.js event listener?

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...

So 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 overcome 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 comes to this question and has something like this as an issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM