From d2c73ae6c3167c382a045e45269e8ae4a399272b Mon Sep 17 00:00:00 2001 From: Darren Date: Sun, 27 Jan 2013 15:45:42 +0000 Subject: [PATCH] Irc bound events fix --- server/irc/eventbinder.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/server/irc/eventbinder.js b/server/irc/eventbinder.js index f343913..e1d7791 100644 --- a/server/irc/eventbinder.js +++ b/server/irc/eventbinder.js @@ -2,26 +2,37 @@ var _ = require('lodash'); module.exports.bindIrcEvents = function (events_scope, event_map, context, irc_connection) { + // Make sure we have a holder for the bound events + if (!event_map._bound_events) + event_map._bound_events = {}; + _.each(event_map, function (fn, event_name) { + if (event_name[0] === '_') return; + // Bind the event to `context`, storing it with the event listing - if (!event_map[event_name].bound_fn) { - event_map[event_name].bound_fn = fn.bind(context); + if (!event_map._bound_events[event_name]) { + event_map._bound_events[event_name] = fn.bind(context); } // Add the listener to the IRC connection object - irc_connection.on(events_scope + ':' + event_name, event_map[event_name].bound_fn); + irc_connection.on(events_scope + ':' + event_name, event_map._bound_events[event_name]); }); }; module.exports.unbindIrcEvents = function (events_scope, event_map, irc_connection) { + // No bound events? Then we have nothing to do + if (!event_map._bound_events) return; + _.each(event_map, function(fn, event_name) { - if (event_map[event_name].bound_fn) { + if (event_name[0] === '_') return; + + if (event_map._bound_events[event_name]) { // Remove the listener from the IRC connection object - irc_connection.removeListener(events_scope + ':' + event_name, event_map[event_name].bound_fn); + irc_connection.removeListener(events_scope + ':' + event_name, event_map._bound_events[event_name]); // Remove the bound function as no longer needed - event_map[event_name].bound_fn = undefined; + event_map._bound_events[event_name] = undefined; } }); }; \ No newline at end of file -- 2.25.1