prepwork for handling off (issue #1)

This commit is contained in:
AJ ONeal 2015-07-26 01:59:02 -06:00
parent 9113b7d46e
commit 062fc23ebb
2 changed files with 32 additions and 15 deletions

View File

@ -112,11 +112,6 @@ function create(opts) {
promise = getConnection(opts); promise = getConnection(opts);
} }
/*
if (opts.connect) {
}
*/
// TODO maybe use HTTP POST instead? // TODO maybe use HTTP POST instead?
return promise.then(function (ws) { return promise.then(function (ws) {
if (ws.masterClient) { if (ws.masterClient) {
@ -130,6 +125,7 @@ function create(opts) {
var cb; var cb;
if ('function' === typeof args[args.length - 1]) { if ('function' === typeof args[args.length - 1]) {
// TODO if off, search for cb and derive id from previous onMessage
id = Math.random(); id = Math.random();
cb = args.pop(); cb = args.pop();
} }
@ -138,6 +134,7 @@ function create(opts) {
type: 'rpc' type: 'rpc'
, func: fname , func: fname
, args: args , args: args
, hasCallback: !!cb
, filename: opts.filename , filename: opts.filename
, id: id , id: id
})); }));
@ -165,7 +162,16 @@ function create(opts) {
return; 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); var index = ws.___listeners.indexOf(onMessage);
ws.___listeners.splice(index, 1); ws.___listeners.splice(index, 1);
} }
@ -173,6 +179,10 @@ function create(opts) {
cb.apply(cmd.this, cmd.args); 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); ws.___listeners.push(onMessage);
} }

View File

@ -44,7 +44,9 @@ function createApp(server, options) {
return [ return [
'set', 'get', 'touch', 'destroy' 'set', 'get', 'touch', 'destroy'
, 'all', 'length', 'clear' , 'all', 'length', 'clear'
, 'on', 'off', 'removeEventListener', 'addEventListener' , 'emit', 'on', 'off', 'once'
, 'removeListener', 'addListener'
, 'removeEventListener', 'addEventListener'
].filter(function (key) { ].filter(function (key) {
if ('function' === typeof db[key]) { if ('function' === typeof db[key]) {
return true; return true;
@ -83,15 +85,20 @@ function createApp(server, options) {
break; break;
case 'rpc': case 'rpc':
cmd.args.push(function () { if (cmd.hasCallback) {
var args = Array.prototype.slice.call(arguments); cmd.args.push(function () {
var args = Array.prototype.slice.call(arguments);
ws.send(JSON.stringify({ ws.send(JSON.stringify({
this: this this: this
, args: args , args: args
, id: cmd.id , id: cmd.id
})); }));
}); });
// TODO handle 'off' by id
cmd.args[cmd.args.length - 1].__id = cmd.id;
}
db[cmd.func].apply(db, cmd.args); db[cmd.func].apply(db, cmd.args);
break; break;