Skip to content

Instantly share code, notes, and snippets.

@kafkadev
Forked from fabriceleal/EventEmitter.once.js
Created November 29, 2023 22:51
Show Gist options
  • Select an option

  • Save kafkadev/e1826d9392f3bbc5908d7b29c095d501 to your computer and use it in GitHub Desktop.

Select an option

Save kafkadev/e1826d9392f3bbc5908d7b29c095d501 to your computer and use it in GitHub Desktop.

Revisions

  1. @fabriceleal fabriceleal revised this gist Sep 4, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion EventEmitter.once.js
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ EventEmitter.prototype.once = function(events, handler){

    // This will allow any args you put in xxx.emit('event', ...) to be sent
    // to your handler
    handler.apply(null, Array.prototype.slice.call(arguments, 0));
    handler.apply(_this, Array.prototype.slice.call(arguments, 0));
    };

    events.forEach(function(e){
  2. @fabriceleal fabriceleal revised this gist Sep 4, 2012. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion EventEmitter.once.js
    Original file line number Diff line number Diff line change
    @@ -16,8 +16,9 @@ EventEmitter.prototype.once = function(events, handler){

    var cb = function(){
    events.forEach(function(e){
    // This only removes the event itself
    // 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);
    });

  3. @fabriceleal fabriceleal revised this gist Sep 4, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions EventEmitter.once.js
    Original file line number Diff line number Diff line change
    @@ -25,6 +25,7 @@ EventEmitter.prototype.once = function(events, handler){
    // to your handler
    handler.apply(null, Array.prototype.slice.call(arguments, 0));
    };

    events.forEach(function(e){
    _this.addListener(e, cb);
    });
  4. @fabriceleal fabriceleal revised this gist Sep 4, 2012. 1 changed file with 23 additions and 4 deletions.
    27 changes: 23 additions & 4 deletions EventEmitter.once.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // "Extend" the EventEmitter like this:

    var EventEmitter = require('events').EventEmitter;

    EventEmitter.prototype.once = function(events, handler){
    @@ -9,14 +11,18 @@ EventEmitter.prototype.once = function(events, handler){
    // 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 event itself
    // from all the events that are listening to it
    _this.removeListener(e, cb);
    });


    // This will allow any args you put in xxx.emit('event', ...) to be sent
    // to your handler
    handler.apply(null, Array.prototype.slice.call(arguments, 0));
    };
    events.forEach(function(e){
    @@ -43,5 +49,18 @@ game.once(['player:quit', 'player:disconnect'], function (a, b, c) {
    console.log('arg 3: ' + c);
    game.endGame()
    });

    // This will exec our handler...
    game.emit('player:disconnect', 1, 2, 3);
    game.emit('player:quit', 1, 2, 3);

    // This one will exec no handler...
    game.emit('player:quit', 4, 5, 6);

    /*
    This outputs:
    Disconnecting...
    arg 1: 1
    arg 2: 2
    arg 3: 3
    Game ended!
    */
  5. @fabriceleal fabriceleal created this gist Sep 4, 2012.
    47 changes: 47 additions & 0 deletions EventEmitter.once.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    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.removeListener(e, cb);
    });

    handler.apply(null, Array.prototype.slice.call(arguments, 0));
    };
    events.forEach(function(e){
    _this.addListener(e, cb);
    });
    };


    // EXAMPLE
    var game = new EventEmitter();
    game.endGame = function(){
    console.log('Game ended!');
    };
    game.on('player:quit', function(){
    console.log('quit')
    });
    game.on('player:disconnect', function(){
    console.log('disconnect')
    });
    game.once(['player:quit', 'player:disconnect'], function (a, b, c) {
    console.log('Disconnecting...');
    console.log('arg 1: ' + a);
    console.log('arg 2: ' + b);
    console.log('arg 3: ' + c);
    game.endGame()
    });
    game.emit('player:disconnect', 1, 2, 3);
    game.emit('player:quit', 1, 2, 3);