From 880e24c9e2fcf45af341cf3103e5f409988d4b65 Mon Sep 17 00:00:00 2001 From: Enrico Stara Date: Sun, 23 Aug 2015 10:55:46 +0200 Subject: [PATCH] Remove code duplication --- lib/telegram.link.js | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/telegram.link.js b/lib/telegram.link.js index fb71138..4fa4a35 100644 --- a/lib/telegram.link.js +++ b/lib/telegram.link.js @@ -316,13 +316,7 @@ Client.prototype.stopHttpPollLoop = function () { // The code: Client.prototype.registerOnUpdates = function (callback) { - var emitter = this._channel.getParser(); - emitter.on('api.type.UpdatesTooLong', callback); - emitter.on('api.type.UpdateShortMessage', callback); - emitter.on('api.type.UpdateShortChatMessage', callback); - emitter.on('api.type.UpdateShort', callback); - emitter.on('api.type.UpdatesCombined', callback); - emitter.on('api.type.Updates', callback); + manageUpdatesListener.call(this, 'on', callback); }; // *** @@ -332,15 +326,19 @@ Client.prototype.registerOnUpdates = function (callback) { // The code: Client.prototype.unregisterOnUpdates = function (callback) { - var emitter = this._channel.getParser(); - emitter.removeListener('api.type.UpdatesTooLong', callback); - emitter.removeListener('api.type.UpdateShortMessage', callback); - emitter.removeListener('api.type.UpdateShortChatMessage', callback); - emitter.removeListener('api.type.UpdateShort', callback); - emitter.removeListener('api.type.UpdatesCombined', callback); - emitter.removeListener('api.type.Updates', callback); + manageUpdatesListener.call(this, 'removeListener', callback); }; +function manageUpdatesListener (func, callback) { + var emitter = this._channel.getParser(); + emitter[func]('api.type.UpdatesTooLong', callback); + emitter[func]('api.type.UpdateShortMessage', callback); + emitter[func]('api.type.UpdateShortChatMessage', callback); + emitter[func]('api.type.UpdateShort', callback); + emitter[func]('api.type.UpdatesCombined', callback); + emitter[func]('api.type.Updates', callback); +} + // *** // client.**isReady()**