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);
}
/*
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);
}

View File

@ -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;