-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
events: adding multiple listeners at once #15697
Comments
Just thinking about the polymorphism that comes with allowing arrays – would |
I've often thought about making improvements to this area but there are some challenges to keep in mind. I ran into some of these when introducing the Getting back to the original question, the approach I've considered is more along the lines of.... emitter.addListeners({
error() { /* ... */ },
clientError() { /* ... */ },
tlsClientError() { /* ... */ }
});
emitter.addOnceListeners({
error() { /* ... */ },
clientError() { /* ... */ },
tlsClientError() { /* ... */ }
});
function fn() {}
emitter.addListeners({
error: fn,
clientError: fn,
tlsClientError: fn
}); |
I'm not really happy with introducing additional methods. What issues are foreseeing when adding array support? Currently, when registering a listener with an array, a implicit |
@silverwind There could be performance issues due to the differing types being passed to the same function in addition to the added argument type checking. If this should get implemented, I would also prefer to see it as a separate function. |
To be honest, I think that this is a common issue to everyone. If we can agree on the relevance of the issue, maybe I could consider trying to work on this. By the way, I took at look at the code. It does not seem impossible to change, but this is an opinion of a beginner. Do you think that this is a beginner-level issue? |
The key issues I see with allowing the polymorphic signature are Performance and Discoverability... that is...
There is another approach to this that I've been considering... the ability to register a generic event sink for any event emitter... e.g. function mySink(event, ...args) {
switch (event) {
case 'foo': console.log('foo'); break;
case 'bar': console.log('bar'); break;
}
}
const ee = new EventEmitter();
ee.addEventSink(mySink);
ee.emit('foo');
ee.emit('bar'); |
I think I'm convinced that the downsides of polymorphism are outweighting the benefits. So for the question on what to implement, I'd propose:
I'm not sure of the value of similar methods for @Giovarco I guess this shouldn't be too hard to implement. |
If we went with the handler approach, there'd definitely be no need for a |
@silverwind Ok then tomorrow I'll give it a shot. |
If you don't concern the performance, you may use ES6 Proxy to achieve what you want in this way: const EE = require("events");
function EP(target){
return new Proxy(target, proxyHandler);
}
const proxyHandler = {
set(target, name, handler){
target.addListener(name, handler);
}
};
let e = new EE();
let p = EP(e);
p.error1 = p.error2 = p.error3 = function(){
console.log("This is an error");
}
e.emit("error1");
e.emit("error2");
e.emit("error3"); |
To be honest, the event modules is a little bit murky for me. I did not find a "node API for developers" that explain the inner workings of the module. Anyway, this is how I tried to add the feature: events.js
This is my test: test-event-emitter-onMultiple.js
I suspect that I'm far from a good implementation, but it works at least. Please don't be too strict with me ^_^" |
Anyone have examples other than the one @silverwind posted? Going over my own code, the places where I add the same listener for different events can be counted on one hand. |
For core modules, it's probably only useful for error handling. One more example: process.on('uncaughtException', handler)
process.on('unhandledRejection', handler) Other than that, I could've used in on modules that fire many different events like chokidar. |
@bnoordhuis I you're working on implementing this feature, please read this #16169 . I'm already working on it, so you can propose changes and we don't get duplicate code. |
Closing this for lack of support. Also see #16169 (comment). |
I often find myself in need of registering to multiple events using the same handler.
.on
currently only supports on event per call, so this most concise way to write it isIdeally, I think
.on
and possibly.once
should provide a way to add multiple listeners in one call, like for example using jQuery-style space-separated event names:Above would be a breaking change for event names containing spaces, so an alternative could be to take the event names from an array:
The text was updated successfully, but these errors were encountered: