question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Refactor the library

See original GitHub issue

The 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:open
  • Created 3 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
DigitalBrainJScommented, May 6, 2020

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…

1reaction
RangerMauvecommented, May 6, 2020

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. 😁

Read more comments on GitHub >

github_iconTop Results From Across the Web

Incomplete Library Class - Refactoring.Guru
The only solution to the problem—changing the library—is often impossible since the library is read-only. Reasons for the Problem. The author of the...
Read more >
The New Refactoring Library 🏛️ - by Luca Rossi
The New Refactoring Library 🏛️. Discover the best collection of online resources to level up your engineering game. Luca Rossi.
Read more >
Angular Libraries : Refactor Existing Components into a ...
When we read Angular's documentation on refactoring components into libraries, we find that it's short on theory and long on warnings.
Read more >
How I Refactored a JavaScript Library to TypeScript
In this story, I will refactor one of my libraries (nric-validator). The scenario is that the code is written in JavaScript + Ava...
Read more >
python-rope/rope: a python refactoring library - GitHub
a python refactoring library. Contribute to python-rope/rope development by creating an account on GitHub.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found