2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-08-28 21:07:39 +00:00

[polling] Fix stopping polling

Bug:

  TelegramBot#stopPolling() fails to clear the timeout
  that is waiting to make the next polling request.
  Thus, if the method is invoked after the timeout has
  already been set up, once the timeout fn is executed,
  another request is made, before polling is stopped in the
  '.finally()' handler of the request!
This commit is contained in:
GochoMugo 2017-01-02 14:19:52 +03:00
parent 6b4ff126f4
commit d768749e8f
No known key found for this signature in database
GPG Key ID: 7B6A01CB57AA39E4

View File

@ -32,6 +32,7 @@ class TelegramBotPolling {
this._lastUpdate = 0;
this._lastRequest = null;
this._abort = false;
this._pollingTimeout = null;
this._polling();
}
@ -43,6 +44,7 @@ class TelegramBotPolling {
*/
stopPolling(options = {}) {
this._abort = true;
clearTimeout(this._pollingTimeout);
if (options.cancel) {
const reason = options.reason || 'Polling stop';
return this._lastRequest.cancel(reason);
@ -76,7 +78,7 @@ class TelegramBotPolling {
debug('Polling is aborted!');
} else {
debug('setTimeout for %s miliseconds', this.options.interval);
setTimeout(() => this._polling(), this.options.interval);
this._pollingTimeout = setTimeout(() => this._polling(), this.options.interval);
}
});
}