[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!
experimental
GochoMugo 9 years ago
parent 6b4ff126f4
commit d768749e8f
No known key found for this signature in database
GPG Key ID: 7B6A01CB57AA39E4
  1. 4
      src/telegramPolling.js

@ -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);
}
});
}

Loading…
Cancel
Save