From 2dd3dd2e8098eb194705593b50a46f0aa69c1b2d Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Wed, 15 Jul 2015 21:11:33 +0430 Subject: [PATCH] Avoid Listener leaks by setting a max answer threshold --- build/functions/fetch.js | 2 +- build/types/Base.js | 9 +++++++++ lib/types/Base.js | 9 +++++++++ package.json | 2 +- types/Base.js | 9 +++++++++ 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/build/functions/fetch.js b/build/functions/fetch.js index 32830b3..26e4fc1 100644 --- a/build/functions/fetch.js +++ b/build/functions/fetch.js @@ -47,7 +47,7 @@ function fetch(path) { } _unirest2['default'].post('https://api.telegram.org/bot' + path).field(data).attach(files).end(function (response) { - if (response.statusType === 4 || response.statusType === 5) { + if (response.statusType === 4 || response.statusType === 5 || !response.body.ok) { reject(response); } else { resolve(response.body); diff --git a/build/types/Base.js b/build/types/Base.js index fc2cdfb..fb2dc96 100644 --- a/build/types/Base.js +++ b/build/types/Base.js @@ -14,6 +14,8 @@ function _inherits(subClass, superClass) { if (typeof superClass !== 'function' var _events = require('events'); +var ANSWER_THRESHOLD = 10; + /** * Base class of all classes */ @@ -67,7 +69,10 @@ var Base = (function (_EventEmitter) { } var chat = _this.properties.chat_id; + var answers = 0; bot.on('update', function listener(result) { + answers += result.length; + var update = result.find(function (_ref) { var message = _ref.message; @@ -86,6 +91,10 @@ var Base = (function (_EventEmitter) { bot.removeListener('update', listener); } + + if (answers >= ANSWER_THRESHOLD) { + bot.removeListener('update', listener); + } }); }); } diff --git a/lib/types/Base.js b/lib/types/Base.js index e777c4e..82ffde6 100644 --- a/lib/types/Base.js +++ b/lib/types/Base.js @@ -1,5 +1,7 @@ import {EventEmitter} from 'events'; +const ANSWER_THRESHOLD = 10; + /** * Base class of all classes */ @@ -43,7 +45,10 @@ export default class Base extends EventEmitter { } const chat = this.properties.chat_id; + let answers = 0; bot.on('update', function listener(result) { + answers += result.length; + const update = result.find(({message}) => { // if in a group, there will be a reply to this message if (chat < 0) { @@ -62,6 +67,10 @@ export default class Base extends EventEmitter { bot.removeListener('update', listener); } + + if (answers >= ANSWER_THRESHOLD) { + bot.removeListener('update', listener); + } }); }); } diff --git a/package.json b/package.json index 9e9acd2..fb30024 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "telegram-api", - "version": "0.5.12", + "version": "0.5.20", "description": "Control Telegram bots easily using the new Telegram API", "main": "index.js", "scripts": { diff --git a/types/Base.js b/types/Base.js index fc2cdfb..fb2dc96 100644 --- a/types/Base.js +++ b/types/Base.js @@ -14,6 +14,8 @@ function _inherits(subClass, superClass) { if (typeof superClass !== 'function' var _events = require('events'); +var ANSWER_THRESHOLD = 10; + /** * Base class of all classes */ @@ -67,7 +69,10 @@ var Base = (function (_EventEmitter) { } var chat = _this.properties.chat_id; + var answers = 0; bot.on('update', function listener(result) { + answers += result.length; + var update = result.find(function (_ref) { var message = _ref.message; @@ -86,6 +91,10 @@ var Base = (function (_EventEmitter) { bot.removeListener('update', listener); } + + if (answers >= ANSWER_THRESHOLD) { + bot.removeListener('update', listener); + } }); }); }