Removed irc_events from the prototype; Untied IrcEventBinder from other objects;...
authorDarren <darren@darrenwhitlen.com>
Fri, 25 Jan 2013 18:49:49 +0000 (18:49 +0000)
committerDarren <darren@darrenwhitlen.com>
Thu, 31 Jan 2013 14:58:43 +0000 (14:58 +0000)
server/irc/binder.js [deleted file]
server/irc/channel.js
server/irc/eventbinder.js [new file with mode: 0644]
server/irc/server.js
server/irc/user.js

diff --git a/server/irc/binder.js b/server/irc/binder.js
deleted file mode 100755 (executable)
index 7f3f0e5..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-var _ = require('lodash');
-
-var Binder = function () {};
-
-module.exports = Binder;
-
-Binder.prototype.bindEvents = function () {
-    var that = this;
-    _.each(this.irc_events, function (fn, event_name, irc_events) {
-        // Bind the event to `that` context, storing it with the event listing
-        if (!irc_events[event_name].bound_fn) {
-            irc_events[event_name].bound_fn = fn.bind(that);
-        }
-
-        that.irc_connection.on(that.scope + ':' + event_name, irc_events[event_name].bound_fn);
-    });
-};
-
-
-Binder.prototype.unbindEvents = function () {
-    var that = this;
-    _.each(this.irc_events, function(fn, event_name, irc_events) {
-        if (irc_events[event_name].bound_fn) {
-            that.irc_connection.removeListener(that.scope + ':' + event_name, irc_events[event_name].bound_fn);
-        }
-    });
-};
\ No newline at end of file
index 57d197c7d91642ccf9e6a5da47b267d3bf912446..e75bcf07e001b937036db7b388167e2841b15b91 100644 (file)
@@ -1,46 +1,44 @@
 var util    = require('util'),
-    Binder  = require('./binder.js');
+    EventBinder  = require('./eventbinder.js');
 
 function IrcChannel(irc_connection, name) {
     this.irc_connection = irc_connection;
     this.name = name;
 
-    this.scope = 'channel:' + name;
-    Binder.call(this);
-    
     this.members = [];
     this.ban_list_buffer = [];
+
+    // Listen for events on the IRC connection
+    this.irc_events = {
+        join:           onJoin,
+        part:           onPart,
+        kick:           onKick,
+        quit:           onQuit,
+        privmsg:        onMsg,
+        notice:         onNotice,
+        ctcp_request:   onCtcpRequest,
+        ctcp_response:  onCtcpResponse,
+        topic:          onTopic,
+        nicklist:       onNicklist,
+        nicklistEnd:    onNicklistEnd,
+        banlist:        onBanList,
+        banlist_end:    onBanListEnd,
+        topicsetby:     onTopicSetby,
+        mode:           onMode
+    };
+    EventBinder.bindIrcEvents('channel:' + this.name, this.irc_events, this, irc_connection);
 }
 
-util.inherits(IrcChannel, Binder);
 
 module.exports = IrcChannel;
 
 
 IrcChannel.prototype.dispose = function (){
-    this.unbindEvents();
+    EventBinder.unbindIrcEvents('channel:' + this.name, this.irc_events);
     this.irc_connection = undefined;
 };
 
 
-IrcChannel.prototype.irc_events = {
-    join:           onJoin,
-    part:           onPart,
-    kick:           onKick,
-    quit:           onQuit,
-    privmsg:        onMsg,
-    notice:         onNotice,
-    ctcp_request:   onCtcpRequest,
-    ctcp_response:  onCtcpResponse,
-    topic:          onTopic,
-    nicklist:       onNicklist,
-    nicklistEnd:    onNicklistEnd,
-    banlist:        onBanList,
-    banlist_end:    onBanListEnd,
-    topicsetby:     onTopicSetby,
-    mode:           onMode
-};
-
 
 function onJoin(event) {
     this.irc_connection.clientEvent('join', {
diff --git a/server/irc/eventbinder.js b/server/irc/eventbinder.js
new file mode 100644 (file)
index 0000000..0f4fe8d
--- /dev/null
@@ -0,0 +1,32 @@
+var _ = require('lodash');
+
+var IrcEventBinder = function () {};
+
+module.exports = IrcEvents;
+
+
+
+IrcEvents.prototype.bindIrcEvents = function (events_scope, event_map, context, irc_connection) {
+    _.each(event_map, function (fn, event_name) {
+        // 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);
+        }
+
+        // Add the listener to the IRC connection object
+        irc_connection.on(events_scope + ':' + event_name, event_map[event_name].bound_fn);
+    });
+};
+
+
+IrcEvents.prototype.unbindIrcEvents = function (events_scope, event_map, irc_connection) {
+    _.each(event_map, function(fn, event_name) {
+        if (event_map[event_name].bound_fn) {
+            // Remove the listener from the IRC connection object
+            irc_connection.removeListener(events_scope + ':' + event_name, event_map[event_name].bound_fn);
+
+            // Remove the bound function as no longer needed
+            event_map[event_name].bound_fn = undefined;
+        }
+    });
+};
\ No newline at end of file
index f52748dead12c3f36fce32efe8abf9e8d45bd864..afd4d68ffe85292abc31030101787d40351af4b2 100755 (executable)
@@ -1,47 +1,53 @@
 var util    = require('util'),
-    Binder  = require('./binder.js');
+    EventBinder  = require('./eventbinder.js');
 
 var IrcServer = function (irc_connection, host, port) {
     this.irc_connection = irc_connection;
     this.host = host;
     this.port = port;
-    
-    this.scope = 'server:' + host;
-    
-    Binder.call(this);
-    
+
     this.list_buffer = [];
     this.motd_buffer = '';
+    
+    this.irc_events = {
+        connect:                onConnect,
+        options:                onOptions,
+        list_start:             onListStart,
+        list_channel:           onListChannel,
+        list_end:               onListEnd,
+        motd_start:             onMotdStart,
+        motd:                   onMotd,
+        motd_end:               onMotdEnd,
+        error:                  onError,
+        channel_redirect:       onChannelRedirect,
+        no_such_nick:           onNoSuchNick,
+        cannot_send_to_channel: onCannotSendToChan,
+        too_many_channels:      onTooManyChannels,
+        user_not_in_channel:    onUserNotInChannel,
+        not_on_channel:         onNotOnChannel,
+        channel_is_full:        onChannelIsFull,
+        invite_only_channel:    onInviteOnlyChannel,
+        banned_from_channel:    onBannedFromChannel,
+        bad_channel_key:        onBadChannelKey,
+        chanop_privs_needed:    onChanopPrivsNeeded,
+        nickname_in_use:        onNicknameInUse
+    };
+    EventBinder.bindIrcEvents('server:' + this.host, this.irc_events, this, irc_connection);
+    
+
 };
 
-util.inherits(IrcServer, Binder);
 
 module.exports = IrcServer;
 
-IrcServer.prototype.irc_events = {
-    connect:                onConnect,
-    options:                onOptions,
-    list_start:             onListStart,
-    list_channel:           onListChannel,
-    list_end:               onListEnd,
-    motd_start:             onMotdStart,
-    motd:                   onMotd,
-    motd_end:               onMotdEnd,
-    error:                  onError,
-    channel_redirect:       onChannelRedirect,
-    no_such_nick:           onNoSuchNick,
-    cannot_send_to_channel: onCannotSendToChan,
-    too_many_channels:      onTooManyChannels,
-    user_not_in_channel:    onUserNotInChannel,
-    not_on_channel:         onNotOnChannel,
-    channel_is_full:        onChannelIsFull,
-    invite_only_channel:    onInviteOnlyChannel,
-    banned_from_channel:    onBannedFromChannel,
-    bad_channel_key:        onBadChannelKey,
-    chanop_privs_needed:    onChanopPrivsNeeded,
-    nickname_in_use:        onNicknameInUse
+
+IrcServer.prototype.dispose = function (){
+    EventBinder.unbindIrcEvents('server:' + this.host, this.irc_events);
+    this.irc_connection = undefined;
 };
 
+
+
 function onConnect(event) {
     this.irc_connection.clientEvent('connect', {
         nick: event.nick
index 2ca511710e170910e820d2145ac72165ded2f8b7..40fdf900227b8978b6371647030fe4275f7d863c 100755 (executable)
@@ -1,36 +1,40 @@
 var util    = require('util'),\r
-    Binder  = require('./binder.js');\r
+    EventBinder  = require('./eventbinder.js');\r
 \r
 var IrcUser = function (irc_connection, nick) {\r
     this.irc_connection = irc_connection;\r
     this.nick = nick;\r
     \r
-    this.scope = 'user:' + nick;\r
-    Binder.call(this);\r
+    this.irc_events = {\r
+        nick:           onNick,\r
+        away:           onAway,\r
+        quit:           onKick,\r
+        whoisuser:      onWhoisUser,\r
+        whoisoperator:  onWhoisOperator,\r
+        whoischannels:  onWhoisChannels,\r
+        whoismodes:     onWhoisModes,\r
+        whoisidle:      onWhoisIdle,\r
+        whoisregnick:   onRegNick,\r
+        endofwhois:     onEhoisEnd,\r
+        notice:         onNotice,\r
+        ctcp_response:  onCtcpResponse,\r
+        privmsg:        onPrivmsg,\r
+        ctcp_request:   onCtcpRequest,\r
+        mode:           onMode\r
+    };\r
+    EventBinder.bindIrcEvents('user:' + this.nick, this.irc_events, this, irc_connection);\r
 };\r
 \r
-util.inherits(IrcUser, Binder);\r
 \r
 module.exports = IrcUser;\r
 \r
-IrcUser.prototype.irc_events = {\r
-    nick:           onNick,\r
-    away:           onAway,\r
-    quit:           onKick,\r
-    whoisuser:      onWhoisUser,\r
-    whoisoperator:  onWhoisOperator,\r
-    whoischannels:  onWhoisChannels,\r
-    whoismodes:     onWhoisModes,\r
-    whoisidle:      onWhoisIdle,\r
-    whoisregnick:   onRegNick,\r
-    endofwhois:     onEhoisEnd,\r
-    notice:         onNotice,\r
-    ctcp_response:  onCtcpResponse,\r
-    privmsg:        onPrivmsg,\r
-    ctcp_request:   onCtcpRequest,\r
-    mode:           onMode\r
+\r
+IrcUser.prototype.dispose = function (){\r
+    EventBinder.unbindIrcEvents('user:' + this.nick, this.irc_events);\r
+    this.irc_connection = undefined;\r
 };\r
 \r
+\r
 function onNick(event) {\r
     this.irc_connection.clientEvent('nick', {\r
         nick: event.nick,\r