Search code examples
javascriptnode.jsdom-eventseventemitter

Javascript eventemitter multiple events once


I'm using node's eventemitter though other event library suggestions are welcomed.

I want to run a function once if several events are fired. Multiple events should be listened to, but all of them are removed if any one of the events fires. Hopefully this code sample demonstrates what I'm looking for.

var game = new eventEmitter();
game.once(['player:quit', 'player:disconnect'], function () {
  endGame()
});

What is the cleanest way to handle this?

Note: Need to remove bound functions individually because there will be other listeners bound.


Solution

  • "Extend" the EventEmitter like this:

    var EventEmitter = require('events').EventEmitter;
    
    EventEmitter.prototype.once = function(events, handler){
        // no events, get out!
        if(! events)
            return; 
    
        // Ugly, but helps getting the rest of the function 
        // short and simple to the eye ... I guess...
        if(!(events instanceof Array))
            events = [events];
    
        var _this = this;
    
        var cb = function(){
            events.forEach(function(e){     
                // This only removes the listener itself 
                // from all the events that are listening to it
                // i.e., does not remove other listeners to the same event!
                _this.removeListener(e, cb); 
            });
    
            // This will allow any args you put in xxx.emit('event', ...) to be sent 
            // to your handler
            handler.apply(_this, Array.prototype.slice.call(arguments, 0));
        };
    
        events.forEach(function(e){ 
            _this.addListener(e, cb);
        }); 
    };
    

    I created a gist here: https://gist.github.com/3627823 which includes an example (your example, with some logs)

    [UPDATE] Following is an adaptation of my implementation of once that removes only the event that was called, as requested in the comments:

    var EventEmitter = require('events').EventEmitter;
    
    EventEmitter.prototype.once = function(events, handler){
        // no events, get out!
        if(! events)
            return; 
    
        // Ugly, but helps getting the rest of the function 
        // short and simple to the eye ... I guess...
        if(!(events instanceof Array))
            events = [events];
    
        var _this = this;
    
        // A helper function that will generate a handler that 
        // removes itself when its called
        var gen_cb = function(event_name){
            var cb = function(){
                _this.removeListener(event_name, cb);
                // This will allow any args you put in 
                // xxx.emit('event', ...) to be sent 
                // to your handler
                handler.apply(_this, Array.prototype.slice.call(arguments, 0));
            };
            return cb;
        };
    
    
        events.forEach(function(e){ 
            _this.addListener(e, gen_cb(e));
        }); 
    };
    

    I found out that node.js already has a once method in the EventEmitter: check here the source code, but this is probably a recent addition.