var self = this;
this._next_id = 0;
- this._callbacks = {};
+ this._rpc_callbacks = {};
this._socket = eio_socket;
this._mixinEmitter();
packet.id = this._next_id;
this._next_id++;
- this._callbacks[packet.id] = callback;
+ this._rpc_callbacks[packet.id] = callback;
}
this.send(packet);
WebsocketRpc.prototype._onMessage = function(message_raw) {
var self = this,
packet,
- returnFn;
+ returnFn,
+ callback;
try {
packet = JSON.parse(message_raw);
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