WebsocketRpc: Cleanup callbacks before exceptions occur
authorDarren <darren@darrenwhitlen.com>
Sun, 1 Sep 2013 13:43:25 +0000 (14:43 +0100)
committerDarren <darren@darrenwhitlen.com>
Sun, 1 Sep 2013 13:43:25 +0000 (14:43 +0100)
client/assets/libs/engine.io.tools.js
server/websocketrpc.js

index 8e7c26498104a9efb981bf29ed0e02de815a1ffd..e24ba2449d7ea42921a6b095e93e798cd805a180 100644 (file)
@@ -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);
@@ -239,10 +240,13 @@ var EngineioTools = {
                 if (typeof this._rpc_callbacks[packet.id] !== 'function')
                     return;
 
-                // Call and delete this callback once finished with it
-                this._rpc_callbacks[packet.id].apply(this, packet.response);
+                // 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
                 if (typeof packet.id !== 'undefined') {
index 974301824015d30398e85736cf9c4e8b5bc1c190..1fa035a4140986ac5935f48d6e9b14543e8cbd05 100644 (file)
@@ -122,7 +122,8 @@ WebsocketRpc.prototype.send = function(packet) {
 WebsocketRpc.prototype._onMessage = function(message_raw) {
     var self = this,
         packet,
-        returnFn;
+        returnFn,
+        callback;
 
     try {
         packet = JSON.parse(message_raw);
@@ -136,10 +137,13 @@ WebsocketRpc.prototype._onMessage = function(message_raw) {
         if (typeof this._rpc_callbacks[packet.id] !== 'function')
             return;
 
-        // Call and delete this callback once finished with it
-        this._rpc_callbacks[packet.id].apply(this, packet.response);
+        // 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
         if (typeof packet.id !== 'undefined') {