From 96adb918c1e7740bb999924f1c8a3bdd29c77f1c Mon Sep 17 00:00:00 2001 From: Conor Fennell Date: Tue, 21 Jun 2016 10:48:05 +0100 Subject: [PATCH] Create a stop method #81 --- README.md | 6 ++++++ src/telegram.js | 12 ++++++++++++ src/telegramPolling.js | 6 ++++++ test/index.js | 27 ++++++++++++++++++++++++++- 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ace732f..336235c 100644 --- a/README.md +++ b/README.md @@ -489,4 +489,10 @@ Register a reply to wait for a message response. | messageId | Number | String | The message id to be replied. | | callback | function | Callback will be called with the reply message. | +### telegramBot.stopPolling() => Promise +Stops polling after the last polling request resolves. + +**Kind**: instance method of [TelegramBot](#TelegramBot) +**Returns**: Promise - The resolved promise for the last poll request + * * * diff --git a/src/telegram.js b/src/telegram.js index d033b11..7359306 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -67,6 +67,18 @@ class TelegramBot extends EventEmitter { this._polling = new TelegramBotPolling(this.token, this.options.polling, this.processUpdate.bind(this)); } + /** + * Stops polling after the last polling request resolves + * + * @return {Promise} promise Promise, of last polling request + */ + stopPolling() { + if (this._polling) { + return this._polling.stopPolling(); + } + return Promise.resolve(); + } + processUpdate(update) { debug('Process Update %j', update); const message = update.message; diff --git a/src/telegramPolling.js b/src/telegramPolling.js index f53874e..fa0bd44 100644 --- a/src/telegramPolling.js +++ b/src/telegramPolling.js @@ -28,6 +28,12 @@ class TelegramBotPolling { this._polling(); } + stopPolling() { + this.abort = true; + // wait until the last request is fulfilled + return this.lastRequest; + } + _polling() { this.lastRequest = this ._getUpdates() diff --git a/test/index.js b/test/index.js index 698f3b6..f7ebb01 100644 --- a/test/index.js +++ b/test/index.js @@ -546,11 +546,14 @@ describe('Telegram', function telegramSuite() { }); // End Telegram describe('#TelegramBotPolling', function TelegramBotPollingSuite() { + it('should call the callback on polling', function test(done) { const opts = { interval: 100, timeout: 1 }; const polling = new TelegramPolling(TOKEN, opts, (msg) => { if (msg.update_id === 10) { - done(); + polling.stopPolling().then(() => { + done(); + }); } }); // The second time _getUpdates is called it will return a message @@ -559,4 +562,26 @@ describe('#TelegramBotPolling', function TelegramBotPollingSuite() { return new Promise.resolve([{ update_id: 10, message: {} }]); }; }); + + describe('#stopPolling', function stopPollingSuite() { + it('should stop polling after last poll request', function test(done) { + const opts = { interval: 200, timeout: 0.5 }; + const polling = new TelegramPolling(TOKEN, opts, (msg) => { + // error if message received as only one poll will complete and there should be no more because of stopPolling + done(msg); + }); + polling.stopPolling() + .then(() => { + setInterval(() => { + done(); + }, 1000); + }).catch(done); + // The second time _getUpdates is called it will return a message + // Really dirty but it works + polling._getUpdates = () => { + return new Promise.resolve([{ update_id: 11, message: {} }]); + }; + }); + }); + });