Reputation: 193
Is there a way to "eat" other listeners of an event?
x.on("test", function(x){
if(x==4) {
console.log("Event1");
}
});
x.on("test", function(x){
if(x==5) {
console.log("Event2");
}
});
x.on("test", function(x){
console.log("Event3");
});
x.emit("test", 4);
x.emit("test", 5);
x.emit("test", -1);
Is there a way to make event 1 "eat" (not allow it to happen) the other events if x is 4?
Without adding if(x!=4&&x!=5) to event 3 (if there are many listeners it may get annoying quickly).
Bonus: Can I have a "fallback" event to catch events that don't have any listener.
Upvotes: 2
Views: 137
Reputation: 28
you can simply try this
x.on("test", function(x){
switch(x){
case 4:
console.log("Event1");
break;
case 5:
console.log("Event2");
break;
default:
console.log("Event3");
}
});
x.emit("test", 4);
x.emit("test", 5);
x.emit("test", -1);
Upvotes: 0
Reputation: 106736
The EventEmitter
built into node does not support "eating" of other event handlers or having a "catch-all" event handler. If you need that kind of functionality you will have to use one of the other EventEmitter
implementations on npm.
Upvotes: 1