From d718a0f965ef59b86c11807fa28ba2dee044290b Mon Sep 17 00:00:00 2001 From: Darren Date: Fri, 19 Sep 2014 12:02:35 +0100 Subject: [PATCH] WebsocketRpc: Use instance as a function instead of confusing .call function --- client/assets/libs/engine.io.tools.js | 46 ++++++++++++++++++--------- client/src/app.js | 6 +++- client/src/models/gateway.js | 11 +++++-- server/client.js | 4 +-- server/websocketrpc.js | 46 ++++++++++++++++++--------- 5 files changed, 77 insertions(+), 36 deletions(-) diff --git a/client/assets/libs/engine.io.tools.js b/client/assets/libs/engine.io.tools.js index e24ba24..8758d79 100644 --- a/client/assets/libs/engine.io.tools.js +++ b/client/assets/libs/engine.io.tools.js @@ -107,19 +107,35 @@ var EngineioTools = { Some way to expire unused callbacks? TTL? expireCallback() function? */ + /** + * Wrapper around creating a new WebsocketRpcCaller + * This lets us use the WebsocketRpc object as a function + */ function WebsocketRpc(eio_socket) { - var self = this; + var caller = new WebsocketRpcCaller(eio_socket); + var ret = function WebsocketRpcInstance() { + return ret.makeCall.apply(ret, arguments); + }; + + for(var prop in caller){ + ret[prop] = caller[prop]; + } + ret._mixinEmitter(); + ret._bindSocketListeners(); + + return ret; + } + + + function WebsocketRpcCaller(eio_socket) { this._next_id = 0; this._rpc_callbacks = {}; this._socket = eio_socket; - - this._mixinEmitter(); - this._bindSocketListeners(); } - WebsocketRpc.prototype._bindSocketListeners = function() { + WebsocketRpcCaller.prototype._bindSocketListeners = function() { var self = this; // Proxy the onMessage listener @@ -131,7 +147,7 @@ var EngineioTools = { - WebsocketRpc.prototype.dispose = function() { + WebsocketRpcCaller.prototype.dispose = function() { if (this._onMessageProxy) { this._socket.removeListener('message', this._onMessageProxy); delete this._onMessageProxy; @@ -146,7 +162,7 @@ var EngineioTools = { /** * The engine.io socket already has an emitter mixin so steal it from there */ - WebsocketRpc.prototype._mixinEmitter = function() { + WebsocketRpcCaller.prototype._mixinEmitter = function() { var funcs = ['on', 'once', 'off', 'removeListener', 'removeAllListeners', 'emit', 'listeners', 'hasListeners']; for (var i=0; i