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
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions src/telegramServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,29 +217,37 @@ export class TelegramServer extends EventEmitter {
}
}

/**
* @FIXME
* (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)
*/
async waitBotEdits() {
return new Promise<void>((resolve) => {
this.on('EditedMessageText', () => resolve());
const handler = () => {
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.

});
}

async waitBotMessage() {
return new Promise<void>((resolve) => {
this.on('AddedBotMessage', () => resolve());
const handler = () => {
this.off('AddedBotMessage', handler);
resolve();
};
this.on('AddedBotMessage', handler);
});
}

async waitUserMessage() {
return new Promise<void>((resolve) => {
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 :)

this.off('AddedUserMessage', messageHandler);
this.off('AddedUserCommand', messageHandler);
this.off('AddedUserCallbackQuery', messageHandler);
resolve();
};
this.on('AddedUserMessage', messageHandler);
this.on('AddedUserCommand', messageHandler);
this.on('AddedUserCallbackQuery', messageHandler);
});
}

Expand All @@ -254,7 +262,6 @@ export class TelegramServer extends EventEmitter {

async addUserCommand(message: CommandRequest) {
assert.ok(message.entities, 'Command should have entities');

await this.addUserUpdate({
...this.getCommonFields(message.botToken),
message,
Expand Down
9 changes: 9 additions & 0 deletions src/test/generic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ describe('Telegram Server', () => {
}).catch((err) => assert.fail(err));
});

it('waits user message', async () => {
const { server, client } = await getServerAndClient(token);
client.sendCommand(client.makeCommand('/start'));
await server.waitUserMessage();
const history = await client.getUpdatesHistory();
assert.equal(history.length, 1);
await server.stop();
});

it('should fully implement user-bot interaction', async () => {
const { server, client } = await getServerAndClient(token);
let message = client.makeMessage('/start');
Expand Down