Correctly disposing IrcConnection
authorDarren <darren@darrenwhitlen.com>
Sun, 28 Jul 2013 20:43:46 +0000 (21:43 +0100)
committerDarren <darren@darrenwhitlen.com>
Sun, 28 Jul 2013 20:43:46 +0000 (21:43 +0100)
server/irc/connection.js
server/irc/state.js

index 2f07ba81704897015b583dded3053734bda296a8..780158769730e9b062bcf3b7396654e0dbadee2c 100644 (file)
@@ -330,6 +330,18 @@ IrcConnection.prototype.end = function (data, callback) {
 IrcConnection.prototype.dispose = function () {
     var that = this;
 
+    // If we're still connected, wait until the socket is closed before disposing
+    // so that all the events are still correctly triggered
+    if (this.socket && this.connected) {
+        this.socket.end();
+        return;
+    }
+
+    if (this.socket) {
+        this.disposeSocket();
+        this.removeAllListeners();
+    }
+
     _.each(this.irc_users, function (user) {
         user.dispose();
     });
@@ -342,22 +354,10 @@ IrcConnection.prototype.dispose = function () {
     this.server.dispose();
     this.server = undefined;
 
-    EventBinder.unbindIrcEvents('', this.irc_events, this);
+    this.irc_commands = undefined;
 
-    // If we're still connected, wait until the socket is closed before disposing
-    // so that all the events are still correctly triggered
-    if (this.socket && this.connected) {
-        this.socket.once('close', function() {
-            that.disposeSocket();
-            that.removeAllListeners();
-        });
-
-        this.socket.end();
+    EventBinder.unbindIrcEvents('', this.irc_events, this);
 
-    } else {
-        this.disposeSocket();
-        this.removeAllListeners();
-    }
 };
 
 
index e63451aa1232d52b449bd238233b1ad6bc7164e4..67d8351beb4e290a8311b5c7d04d07c7e341fa1a 100755 (executable)
@@ -18,7 +18,6 @@ var State = function (client, save_state) {
             _.each(that.irc_connections, function (irc_connection, i, cons) {
                 if (irc_connection) {
                     irc_connection.end('QUIT :' + (global.config.quit_message || ''));
-                    irc_connection.dispose();
                     global.servers.removeConnection(irc_connection);
                     cons[i] = null;
                 }