From 062fc23ebb1a36015a7457d6355effda3bd72b06 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sun, 26 Jul 2015 01:59:02 -0600 Subject: [PATCH] prepwork for handling off (issue #1) --- client.js | 22 ++++++++++++++++------ server.js | 25 ++++++++++++++++--------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/client.js b/client.js index afbd018..cd84eb3 100644 --- a/client.js +++ b/client.js @@ -112,11 +112,6 @@ function create(opts) { promise = getConnection(opts); } - /* - if (opts.connect) { - } - */ - // TODO maybe use HTTP POST instead? return promise.then(function (ws) { if (ws.masterClient) { @@ -130,6 +125,7 @@ function create(opts) { var cb; if ('function' === typeof args[args.length - 1]) { + // TODO if off, search for cb and derive id from previous onMessage id = Math.random(); cb = args.pop(); } @@ -138,6 +134,7 @@ function create(opts) { type: 'rpc' , func: fname , args: args + , hasCallback: !!cb , filename: opts.filename , id: id })); @@ -165,7 +162,16 @@ function create(opts) { return; } - if ('on' !== fname) { + /* + // TODO not sure how to handle 'emit' or 'off'... + // it'll just be broken for now + if ('off' === fname || 'remove.*Listener'.test(fname)) { + var index = ws.___listeners.indexOf(onMessage); + ws.___listeners.splice(index, 1); + } + */ + + if ('on' !== fname && ! /add.*Listener/.test(fname)) { var index = ws.___listeners.indexOf(onMessage); ws.___listeners.splice(index, 1); } @@ -173,6 +179,10 @@ function create(opts) { cb.apply(cmd.this, cmd.args); } + // TODO search index by cb for 'off' + // and pass it along to the rpc with the original id + onMessage.__cb = cb; + onMessage.__id = id; ws.___listeners.push(onMessage); } diff --git a/server.js b/server.js index 7d60f65..be089ac 100644 --- a/server.js +++ b/server.js @@ -44,7 +44,9 @@ function createApp(server, options) { return [ 'set', 'get', 'touch', 'destroy' , 'all', 'length', 'clear' - , 'on', 'off', 'removeEventListener', 'addEventListener' + , 'emit', 'on', 'off', 'once' + , 'removeListener', 'addListener' + , 'removeEventListener', 'addEventListener' ].filter(function (key) { if ('function' === typeof db[key]) { return true; @@ -83,15 +85,20 @@ function createApp(server, options) { break; case 'rpc': - cmd.args.push(function () { - var args = Array.prototype.slice.call(arguments); + if (cmd.hasCallback) { + cmd.args.push(function () { + var args = Array.prototype.slice.call(arguments); - ws.send(JSON.stringify({ - this: this - , args: args - , id: cmd.id - })); - }); + ws.send(JSON.stringify({ + this: this + , args: args + , id: cmd.id + })); + }); + + // TODO handle 'off' by id + cmd.args[cmd.args.length - 1].__id = cmd.id; + } db[cmd.func].apply(db, cmd.args); break;