Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Listening once on array #66

Closed
langpavel opened this Issue · 4 comments

4 participants

@langpavel

This is similar to #31 but distinct.
Can be feature request.

Look at code below.

var EventEmitter2 = require('eventemitter2').EventEmitter2;
var game = new EventEmitter2;\

function endGame() { console.log('THE END'); }

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

//this does nothing - but should call endGame and unregister it
game.emit('player:quit');

//thid does nothing - but should'n call endGame because of previous call
game.emit('player:disconnect');

// this call endGame - this give no sense to me
game.emit(['player:quit', 'player:disconnect']);

I believe an array given to once shold be handled as one of given. There is no sense to listen to given array.

@gotwarlost

According to the README, this line

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

would be interpreted by EE2 as a listen on ONE event with the name player:quit.player:disconnect

which is why it doesn't meet your expectations. Seems to me that EE2 is working as documented.

@Charuru

@gotwarlost That's why this is a feature request so that it works with array of events.

@hij1nx
Owner

im not sure, why not just do this...

game.once('player:quit', function () { endGame() });
game.once('player:disconnect', function () { endGame() });

the only reason i say it might not be a good idea, is because its a huge performance sacrifice to add this feature.

@hij1nx hij1nx closed this
@langpavel

@hij1nx I think something like onceOneOf(['x:event1', 'x:event2'], callback) like this sample:

var clean = function() {
  game.removeListener('player:quit', clean);
  game.removeListener('player:disconnect', clean);
  // …
  endGame();
}

game.on('player:quit', function () { endGame() });
game.on('player:disconnect', function () { endGame() });

Can be simlified:

game.onceOneOf(['player:quit', 'player:disconnect'], function () { endGame() });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.