Skip to content
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

waitFor does not clear listener once resolved #262

Closed
kelvinhammond opened this issue Jun 14, 2020 · 1 comment · Fixed by #263
Closed

waitFor does not clear listener once resolved #262

kelvinhammond opened this issue Jun 14, 2020 · 1 comment · Fixed by #263
Assignees
Labels

Comments

@kelvinhammond
Copy link

kelvinhammond commented Jun 14, 2020

Thank you for this excellent library.
There is a bug, I think the library is still good.
It does not clear the listener when waitFor has been resolved.
Here is an example.

const EventEmitter2 = require('eventemitter2');
const emitter = new EventEmitter2();

console.log(emitter.listeners())
# > []

const promise = emitter.waitFor('event').then(() => { console.log('here'); });
console.log(emitter.listeners());
# > [ [Function: listener] ]

emitter.emit('event');
# > here

console.log(emitter.listeners());
# > [ [Function: listener] ]

At the end it should be # > [] but there still remains on listener bound which later causes...

MaxListenersExceededWarning: (node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.

A good solution / workaround that I use:

return new Promise((resolve, reject) => {
  const listener = this.on('event', (...args) => {
    if(filter(...args)) {
      listener.off();
      resolve(...args);
    }
  }, {objectify: true});
});
@kelvinhammond kelvinhammond changed the title waitFor does not clear listener waitFor does not clear listener once resolved Jun 14, 2020
@DigitalBrainJS
Copy link
Collaborator

@kelvinhammond Thanks for reporting! Fixed. Will be released in 6.4.3 patch.

@DigitalBrainJS DigitalBrainJS self-assigned this Jun 14, 2020
@DigitalBrainJS DigitalBrainJS linked a pull request Jun 14, 2020 that will close this issue
RangerMauve pushed a commit that referenced this issue Jun 22, 2020
* Fixes #259 unsafe-eval

* Fixed coverage badge;
Updated CHANGELOG;

* Refactored variable;

* Fixes #262 waitFor listeners leakage

* Fixed coveralls badge link;

* Spell fix;

* Fixes #265 Objectify option is ignored in wildcard mode;

* updated CHANGELOG.md;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants