1 var _
= require('lodash');
4 module
.exports
.bindIrcEvents = function (events_scope
, event_map
, context
, irc_connection
) {
5 var namespace_prefix
= events_scope
?
9 // Make sure we have a holder for the bound events
10 if (!event_map
._bound_events
)
11 event_map
._bound_events
= {};
13 _
.each(event_map
, function (fn
, event_name
) {
14 if (event_name
[0] === '_') return;
16 // Bind the event to `context`, storing it with the event listing
17 if (!event_map
._bound_events
[event_name
]) {
18 event_map
._bound_events
[event_name
] = _
.bind(fn
, context
);
21 // Add the listener to the IRC connection object
22 irc_connection
.on(namespace_prefix
+ event_name
, event_map
._bound_events
[event_name
]);
27 module
.exports
.unbindIrcEvents = function (events_scope
, event_map
, irc_connection
) {
28 var namespace_prefix
= events_scope
?
32 // No bound events? Then we have nothing to do
33 if (!event_map
._bound_events
) return;
35 _
.each(event_map
, function(fn
, event_name
) {
36 if (event_name
[0] === '_') return;
38 if (event_map
._bound_events
[event_name
]) {
39 // Remove the listener from the IRC connection object
40 irc_connection
.removeListener(namespace_prefix
+ event_name
, event_map
._bound_events
[event_name
]);
42 // Remove the bound function as no longer needed
43 delete event_map
._bound_events
[event_name
];
47 delete event_map
._bound_events
;