From: Darren Date: Sun, 27 Jan 2013 15:45:42 +0000 (+0000) Subject: Irc bound events fix X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=d2c73ae6c3167c382a045e45269e8ae4a399272b;p=KiwiIRC.git Irc bound events fix --- 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