From b42bd8f3ff4c42533a501d859dd86f9af7e36820 Mon Sep 17 00:00:00 2001 From: Darren Date: Sun, 1 Sep 2013 14:03:30 +0100 Subject: [PATCH] RPC callbacks refactor --- client/assets/libs/engine.io.tools.js | 265 ++++++++++++++------------ server/websocketrpc.js | 48 +++-- 2 files changed, 176 insertions(+), 137 deletions(-) diff --git a/client/assets/libs/engine.io.tools.js b/client/assets/libs/engine.io.tools.js index 47e1a15..9852e8a 100644 --- a/client/assets/libs/engine.io.tools.js +++ b/client/assets/libs/engine.io.tools.js @@ -1,5 +1,5 @@ var EngineioTools = { - ReconnectingSocket: function ReconnectingSocket(server_uri, socket_options) { + ReconnectingSocket: function ReconnectingSocket(server_uri, socket_options) { var connected = false; var is_reconnecting = false; @@ -101,166 +101,185 @@ var EngineioTools = { Rpc: (function(){ - /* - Create a document explaining the protocol - */ + /* + TODO: + Create a document explaining the protocol + Some way to expire unused callbacks? TTL? expireCallback() function? + */ - function WebsocketRpc(eio_socket) { - var self = this; + function WebsocketRpc(eio_socket) { + var self = this; - this._next_id = 0; - this._callbacks = {}; - this._socket = eio_socket; + this._next_id = 0; + this._callbacks = {}; + this._socket = eio_socket; - this._mixinEmitter(); - this._bindSocketListeners(); - } + this._mixinEmitter(); + this._bindSocketListeners(); + } - WebsocketRpc.prototype._bindSocketListeners = function() { - var self = this; + WebsocketRpc.prototype._bindSocketListeners = function() { + var self = this; - // Proxy the onMessage listener - this._onMessageProxy = function rpcOnMessageBoundFunction(){ - self._onMessage.apply(self, arguments); - }; - this._socket.on('message', this._onMessageProxy); - }; + // Proxy the onMessage listener + this._onMessageProxy = function rpcOnMessageBoundFunction(){ + self._onMessage.apply(self, arguments); + }; + this._socket.on('message', this._onMessageProxy); + }; - WebsocketRpc.prototype.dispose = function() { - if (this._onMessageProxy) { - this._socket.removeListener('message', this._onMessageProxy); - delete this._onMessageProxy; - } + WebsocketRpc.prototype.dispose = function() { + if (this._onMessageProxy) { + this._socket.removeListener('message', this._onMessageProxy); + delete this._onMessageProxy; + } - this.removeAllListeners(); - }; + this.removeAllListeners(); + }; - /** - * The engine.io socket already has an emitter mixin so steal it from there - */ - WebsocketRpc.prototype._mixinEmitter = function() { - var funcs = ['on', 'once', 'off', 'removeListener', 'removeAllListeners', 'emit', 'listeners', 'hasListeners']; + /** + * The engine.io socket already has an emitter mixin so steal it from there + */ + WebsocketRpc.prototype._mixinEmitter = function() { + var funcs = ['on', 'once', 'off', 'removeListener', 'removeAllListeners', 'emit', 'listeners', 'hasListeners']; - for (var i=0; i