Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Only subscribe to updates when a handler is attached. #190

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
34 changes: 26 additions & 8 deletions src/rpc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,32 @@ class RPC {
this.isAuth = true;
this.sendWaitMessages();

// This request is necessary to ensure that you start interacting with the server. If we have not made any request, the server will not send us updates.
this.call('help.getConfig')
.then((result) => {
// TODO: Handle config
})
.catch((error) => {
this.debug(`error when calling the method help.getConfig:`, error);
});
const updatesEvents = [
'updatesTooLong',
'updateShortMessage',
'updateShortChatMessage',
'updateShort',
'updatesCombined',
'updates',
'updateShortSentMessage'
];

const newListenerHandler = async (event) => {
if (updatesEvents.includes(event)) {
this.context.updates.off('newListener', newListenerHandler);
try {
// This request will subscribe the connection to updates from telegram's server.
const state = await this.call('updates.getState');
this.context.updates.emit('updateState', state);
} catch (error) {
this.debug(`error when calling the method updates.getState:`, error);
// There was a problem getting state and subscribing to updates, so reattach the handler
this.context.updates.on('newListener', newListenerHandler);
}
}
}
this.context.updates.on('newListener', newListenerHandler);

} else {
this.nonce = this.crypto.getRandomBytes(16);
this.handleMessage = this.handlePQResponse;
Expand Down