From fdadaae946e5a9fa3b30eebdea42d3398427ad0f Mon Sep 17 00:00:00 2001 From: Raevel Date: Sat, 16 Oct 2010 22:57:12 +0200 Subject: [PATCH] * Added a throw to the catch in parsing since catching all errors thrown means that a lot of other errors get hard to debug. * Fixed a bug where two listeners with the same function body could not be added to the same event. This was due to the fact that they were stored under listener_cache[event][hollaback], hashes in js can only have string properties so what was actually defined was listener_cache[event][hollaback.toString()], which makes the event handling unreliable. --- lib/irc.js | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/lib/irc.js b/lib/irc.js index e215a78..2aa3489 100644 --- a/lib/irc.js +++ b/lib/irc.js @@ -123,6 +123,7 @@ function IRC (options) { catch (e) { if (DEBUG) sys.puts("[ERROR] Parsing '" + line + "'") + throw e; } } @@ -246,20 +247,14 @@ function IRC (options) { * irc_instance.addListener('PING', pingListener); // Listens for the PING message from server **/ this.addListener = function (event, hollaback) { - var bound; - - if (internal.listener_cache[event] && - internal.listener_cache[event][hollaback]) { - emitter.removeListener(event, internal.listener_cache[event][hollaback]) - } - - bound = hollaback.bind(this) + console.log("IRC:addListener: ", event); + var bound = hollaback.bind(this) if (!internal.listener_cache[event]) { - internal.listener_cache[event] = {} + internal.listener_cache[event] = [] } - internal.listener_cache[event][hollaback] = bound + internal.listener_cache[event].push(bound); emitter.addListener(event, bound) return this } @@ -276,14 +271,23 @@ function IRC (options) { * irc_instance.removeListener('PING', pingListener); // Stops listening for the PING message from server **/ this.removeListener = function (event, hollaback) { - if (internal.listener_cache[event] && - internal.listener_cache[event][hollaback]) { - emitter.removeListener(event, internal.listener_cache[event][hollaback]) - delete internal.listener_cache[event][hollaback] + if (internal.listener_cache[event]) { + var eventCache = internal.listener_cache[event] + for (var i = 0; i < eventCache.length; i++) { + if (eventCache[i] === hollaback) { + // If last element. + if (i+1 === eventCache.length) { + eventCache.pop(); + } else { + // Replace index with last element. + eventCache[i] = eventCache.pop(); + } + emitter.removeListener(event, eventCache[i]); + } + } } - - return this - } + return this; + }; /** * IRC#listenOnce(event, listener) -> null