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

fix event emitter leak warning #59

Merged
merged 5 commits into from
Nov 30, 2021
Merged

Conversation

RB-Lab
Copy link
Contributor

@RB-Lab RB-Lab commented Nov 29, 2021

I have a really long integration test suite, that listens a lot on server edit messages. It was giving me these warnings:

 (node:103570) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11
 EditedMessageText listeners added to [TelegramServer]. Use emitter.setMaxListeners() to
 increase limit (Use `node --trace-warnings ...` to show where the warning was created)

@coveralls
Copy link

coveralls commented Nov 29, 2021

Coverage Status

Coverage increased (+1.3%) to 88.286% when pulling 6b5f7e1 on RB-Lab:rb/fix-event-emitter into b7c085e on jehy:master.

this.off('EditedMessageText', handler);
resolve();
};
this.on('EditedMessageText', handler);
Copy link
Owner

@jehy jehy Nov 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we can simply use once onstead of on - the we don't have to remove listener manually.

this.on('AddedUserMessage', () => resolve());
this.on('AddedUserCommand', () => resolve());
this.on('AddedUserCallbackQuery', () => resolve());
const messageHandler = () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't look cool but I can't think of a better way too :)

Copy link
Owner

@jehy jehy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it would be a little better to use once in cases when we wait for one event.

@RB-Lab
Copy link
Contributor Author

RB-Lab commented Nov 29, 2021

Good point!

this.on('AddedUserMessage', () => resolve());
this.on('AddedUserCommand', () => resolve());
this.on('AddedUserCallbackQuery', () => resolve());
this.once('AddedUserMessage', () => resolve());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything was right here in previous commit version. We wait for any signal and the turn off all emitters and resolve promise.

Now, one emitter will turn itself off but other two will stay and that's not what we expect :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, that's why you "can't think of a better way too"! =)) Indeed!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. Emitter is a great power which comes with great possibility of shooting your legs off :)

@jehy jehy merged commit 0ebcad6 into jehy:master Nov 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants