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.
“Extend” the EventEmitter like this:
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
oncethat removes only the event that was called, as requested in the comments:I found out that node.js already has a
oncemethod in the EventEmitter: check here the source code, but this is probably a recent addition.