Refactor the library
See original GitHub issueThe library has strange, almost dead conditional branches with checking _events
and _all
props, since _events is always truly, moreover it is initialized with object even in wildcard mode, but never used.
https://github.com/EventEmitter2/EventEmitter2/blob/9f8a8371df3088469d02992aba244bcfc1eeb832/lib/eventemitter2.js#L222
The _all array stay initialized for always after being first time used.
So in fact these branches are dead (never executed at all):
https://github.com/EventEmitter2/EventEmitter2/blob/9f8a8371df3088469d02992aba244bcfc1eeb832/lib/eventemitter2.js#L459
https://github.com/EventEmitter2/EventEmitter2/blob/9f8a8371df3088469d02992aba244bcfc1eeb832/lib/eventemitter2.js#L567
These checks are useless:
https://github.com/EventEmitter2/EventEmitter2/blob/9f8a8371df3088469d02992aba244bcfc1eeb832/lib/eventemitter2.js#L712
https://github.com/EventEmitter2/EventEmitter2/blob/9f8a8371df3088469d02992aba244bcfc1eeb832/lib/eventemitter2.js#L463
https://github.com/EventEmitter2/EventEmitter2/blob/9f8a8371df3088469d02992aba244bcfc1eeb832/lib/eventemitter2.js#L876
https://github.com/EventEmitter2/EventEmitter2/blob/9f8a8371df3088469d02992aba244bcfc1eeb832/lib/eventemitter2.js#L904
And so on.
It seems we should set _events and _all to null if there are no any listeners, or use internal flag to determine if there are any listeners to avoid object reinitiazlization.
emitter.emitAsync
has bad logic and returns very strange values.
If _all and _events was not initialized yet it returns false (but fortunately is never executed) not even Promise.resolve(false).
For newListener
event and if the vent has no listeners it returns [false]
If _all and _events is initialized and there are no any listeners - []
Moreover it’s resolves promises from all
listeners array and array of specific event type so there is no ability to determine is value returned from all
listener or from a listener of specific event type.
I bellive this should be refactored in major version:
- The method should return resolved Promise with an empty array or false if there are no methods listening the specific event.
- The method should not collect the values from
all
listeners, just wait while they will be resolved.
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (3 by maintainers)
Top GitHub Comments
Yeap. This lib has a bit more issues that just emitAsync and currently i have not an idea how to fix that and keep 100% compability. Some internal props isn’t marked as private for ex. this.listenerTree, this.delimiter, this.wildcard so i can’t refactor that and be sure nobody uses them. this.listenerTree should be refactored to this._events to simplify code logic this.delimiter should be refactored to this._delimiter this.wildcard should be refactored to this._wildcard and so on. In the future their keys could be replaced with private or symbol properties. this.emit always returns true, even if all the listeners have been removed…
Yeah, I don’t think I was around when that was introduced. I think emitAsync was a PR which I didn’t look too deeply into.
I’m down for a major bump. 😁