From d768749e8fade0892c55c39f89e63865eebec05d Mon Sep 17 00:00:00 2001 From: GochoMugo Date: Mon, 2 Jan 2017 14:19:52 +0300 Subject: [PATCH] [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! --- src/telegramPolling.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/telegramPolling.js b/src/telegramPolling.js index fd679f5..70b2516 100644 --- a/src/telegramPolling.js +++ b/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); } }); }