Merge pull request #403 from M2Ys4U/nick_trunc_fix
[KiwiIRC.git] / client / assets / libs / engine.io.tools.js
index 9852e8a0f2aa7f8555ef5d21969aa3664658b2ae..e24ba2449d7ea42921a6b095e93e798cd805a180 100644 (file)
@@ -111,7 +111,7 @@ var EngineioTools = {
             var self = this;
 
             this._next_id = 0;
-            this._callbacks = {};
+            this._rpc_callbacks = {};
             this._socket = eio_socket;
 
             this._mixinEmitter();
@@ -203,7 +203,7 @@ var EngineioTools = {
                 packet.id = this._next_id;
 
                 this._next_id++;
-                this._callbacks[packet.id] = callback;
+                this._rpc_callbacks[packet.id] = callback;
             }
 
             this.send(packet);
@@ -225,7 +225,8 @@ var EngineioTools = {
         WebsocketRpc.prototype._onMessage = function(message_raw) {
             var self = this,
                 packet,
-                returnFn;
+                returnFn,
+                callback;
 
             try {
                 packet = JSON.parse(message_raw);
@@ -236,12 +237,15 @@ var EngineioTools = {
 
             if (this._isResponse(packet)) {
                 // If we have no callback waiting for this response, don't do anything
-                if (typeof this._callbacks[packet.id] !== 'function')
+                if (typeof this._rpc_callbacks[packet.id] !== 'function')
                     return;
 
-                // Call and delete this callback once finished with it
-                this._callbacks[packet.id].apply(this, packet.response);
-                delete this._callbacks[packet.id];
+                // Delete the callback before calling it. If any exceptions accur within the callback
+                // we don't have to worry about the delete not happening
+                callback = this._rpc_callbacks[packet.id];
+                delete this._rpc_callbacks[packet.id];
+
+                callback.apply(this, packet.response);
 
             } else if (this._isCall(packet)) {
                 // Calls with an ID may be responded to