From ccd097cb27435332a56f3fe385a5220ac2c34a58 Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Wed, 21 Mar 2018 16:01:30 +0330 Subject: [PATCH 1/4] chore: bump to 3.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ee51d69..2ae337f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "telegram-api", - "version": "3.0.0", + "version": "3.1.0", "description": "Control Telegram bots easily using the new Telegram API", "main": "build/index.js", "scripts": { -- 2.34.1 From 120698fcc05dccfbfd5ce9a388ec59e4e77be905 Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Wed, 21 Mar 2018 16:02:23 +0330 Subject: [PATCH 2/4] chore: bump to 3.1.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2ae337f..d76630d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "telegram-api", - "version": "3.1.0", + "version": "3.1.1", "description": "Control Telegram bots easily using the new Telegram API", "main": "build/index.js", "scripts": { -- 2.34.1 From 8d280e157c97958e0ce26572f0185a7a163b2c67 Mon Sep 17 00:00:00 2001 From: Laurynas Karvelis Date: Wed, 21 Mar 2018 14:39:46 +0200 Subject: [PATCH 3/4] Introduce internal API call queueing mechanism to send messages sequentially (no more random order) --- src/functions/api.js | 57 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/src/functions/api.js b/src/functions/api.js index b505f81..e56a7d4 100644 --- a/src/functions/api.js +++ b/src/functions/api.js @@ -1,6 +1,17 @@ // API methods import fetch from './fetch'; +/** + * Simple replacement for Bluebird's Promise.mapSeries() implementation + * @param {Array} tasks to run serially + * @param {Function} task function to execute + * @return {Promise} + */ +function sequence(tasks, fn) { + return tasks.reduce((promise, task) => promise.then(() => fn(task)), Promise.resolve()); +} + + /** * API class, has a function for each method of the Telegram API which take * an object argument, and send request to the API server @@ -8,6 +19,7 @@ import fetch from './fetch'; * Methods: getMe, sendMessage, forwardMessage, sendPhoto, sendAudio, * sendDocument, sendSticker, sendVideo, sendLocation, sendChatAction, * getUserProfilePhotos, getUpdates + * */ export default class API { /** @@ -16,6 +28,29 @@ export default class API { */ constructor(token) { this.token = token; + this._queue = []; + this._inUseQueue = []; + } + + /** + * Run Telegram API calls serially using internal queueing mechanism + * @private + */ + _runQueue() { + // implementation taken from https://github.com/yagop/node-telegram-bot-api/issues/192#issuecomment-249488807 + if (this._inUseQueue.length || !this._queue.length) return; + + this._inUseQueue = this._queue; + this._queue = []; + + sequence(this._inUseQueue, request => { //eslint-disable-line + return this.request(request.method, request.data) + .then(request.resolve) + .catch(request.reject); + }).then(() => { + this._inUseQueue = []; + this._runQueue(); + }); } } @@ -24,12 +59,24 @@ API.prototype.request = function request(method, data) { }; const methods = ['getMe', 'sendMessage', 'forwardMessage', 'sendPhoto', -'sendAudio', 'sendDocument', 'sendSticker', 'sendVideo', -'sendLocation', 'sendChatAction', 'getUserProfilePhotos', -'getUpdates', 'setWebhook', 'deleteMessage']; + 'sendAudio', 'sendDocument', 'sendSticker', 'sendVideo', + 'sendLocation', 'sendChatAction', 'getUserProfilePhotos', + 'getUpdates', 'setWebhook', 'deleteMessage']; methods.forEach(method => { - API.prototype[method] = function(data) { //eslint-disable-line - return this.request(method, data); + API.prototype[method] = function (data) { //eslint-disable-line + // implementation taken from https://github.com/yagop/node-telegram-bot-api/issues/192#issuecomment-249488807 + let resolve; + let reject; + + const promise = new Promise((_resolve, _reject) => { + resolve = _resolve; + reject = _reject; + }); + + this._queue.push({ method, data, resolve, reject }); + process.nextTick(this._runQueue.bind(this)); + + return promise; }; }); -- 2.34.1 From 465eca0c5ee1b53c1fda71ccdecc9f5227343e87 Mon Sep 17 00:00:00 2001 From: Laurynas Karvelis Date: Wed, 21 Mar 2018 16:39:59 +0200 Subject: [PATCH 4/4] Reduce code complexity --- src/functions/api.js | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/functions/api.js b/src/functions/api.js index e56a7d4..9ee952d 100644 --- a/src/functions/api.js +++ b/src/functions/api.js @@ -66,17 +66,9 @@ const methods = ['getMe', 'sendMessage', 'forwardMessage', 'sendPhoto', methods.forEach(method => { API.prototype[method] = function (data) { //eslint-disable-line // implementation taken from https://github.com/yagop/node-telegram-bot-api/issues/192#issuecomment-249488807 - let resolve; - let reject; - - const promise = new Promise((_resolve, _reject) => { - resolve = _resolve; - reject = _reject; + return new Promise((resolve, reject) => { + this._queue.push({ method, data, resolve, reject }); + process.nextTick(this._runQueue.bind(this)); }); - - this._queue.push({ method, data, resolve, reject }); - process.nextTick(this._runQueue.bind(this)); - - return promise; }; }); -- 2.34.1