-
-
Save kafkadev/e1826d9392f3bbc5908d7b29c095d501 to your computer and use it in GitHub Desktop.
Revisions
-
fabriceleal revised this gist
Sep 4, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(_this, Array.prototype.slice.call(arguments, 0)); }; events.forEach(function(e){ -
fabriceleal revised this gist
Sep 4, 2012 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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); }); -
fabriceleal revised this gist
Sep 4, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); }); -
fabriceleal revised this gist
Sep 4, 2012 . 1 changed file with 23 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); // 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! */ -
fabriceleal created this gist
Sep 4, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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);