Permalink
Browse files

removed all the {local,remote}Store code, just pass in a ref yourself

  • Loading branch information...
1 parent d9ac377 commit 6762af36ac3802eebe2e273d3d6ea9cca19a3079 @substack committed Jun 20, 2012
Showing with 39 additions and 168 deletions.
  1. +24 −24 index.js
  2. +15 −41 lib/scrub.js
  3. +0 −56 lib/store.js
  4. +0 −47 test/store.js
View
@@ -1,6 +1,5 @@
var EventEmitter = require('events').EventEmitter;
var Scrubber = require('./lib/scrub');
-var Store = require('./lib/store');
var objectKeys = require('./lib/keys');
var forEach = require('./lib/foreach');
var isEnumerable = require('./lib/is_enum');
@@ -15,23 +14,17 @@ module.exports = function (cons) {
}
})();
-function Proto (cons) {
+function Proto (cons, localRef) {
var self = this;
EventEmitter.call(self);
self.remote = {};
- self.localStore = new Store;
- self.remoteStore = new Store;
+ self.callbacks = {
+ local : localRef || [],
+ remote : []
+ };
- self.scrubber = new Scrubber(self.localStore);
-
- self.localStore.on('cull', function (id) {
- self.emit('request', {
- method : 'cull',
- arguments : [id],
- callbacks : {}
- });
- });
+ self.scrubber = new Scrubber(self.callbacks.local);
if (typeof cons === 'function') {
self.instance = new cons(self.remote, self);
@@ -43,6 +36,13 @@ Proto.prototype.start = function () {
this.request('methods', [ this.instance ]);
};
+Proto.prototype.cull = function (id) {
+ this.emit('request', {
+ method : 'cull',
+ arguments : [ id ]
+ });
+};
+
Proto.prototype.request = function (method, args) {
var scrub = this.scrubber.scrub(args);
@@ -57,27 +57,27 @@ Proto.prototype.request = function (method, args) {
Proto.prototype.handle = function (req) {
var self = this;
var args = self.scrubber.unscrub(req, function (id) {
- if (!self.remoteStore.has(id)) {
+ if (self.callbacks.remote[id] === undefined) {
// create a new function only if one hasn't already been created
// for a particular id
- self.remoteStore.add(function () {
+ self.callbacks.remote[id] = function () {
self.request(id, [].slice.apply(arguments));
- }, id);
+ };
}
- return self.remoteStore.get(id);
+ return self.callbacks.remote[id];
});
if (req.method === 'methods') {
self.handleMethods(args[0]);
}
else if (req.method === 'cull') {
forEach(args, function (id) {
- self.remoteStore.cull(args);
+ delete self.callbacks.remote[id];
});
}
else if (typeof req.method === 'string') {
if (isEnumerable(self.instance, req.method)) {
- self.apply(self.instance[req.method], self.instance, args);
+ self.apply(self.instance[req.method], args);
}
else {
self.emit('fail', new Error(
@@ -86,11 +86,11 @@ Proto.prototype.handle = function (req) {
}
}
else if (typeof req.method == 'number') {
- var fn = self.localStore.get(req.method);
- if (!fn) {
+ var fn = self.callbacks.local[req.method];
+ if (typeof fn !== 'function') {
self.emit('fail', new Error('no such method'));
}
- else self.apply(fn, self.instance, args);
+ else self.apply(fn, args);
}
};
@@ -113,7 +113,7 @@ Proto.prototype.handleMethods = function (methods) {
self.emit('ready');
};
-Proto.prototype.apply = function (f, obj, args) {
- try { f.apply(obj, args) }
+Proto.prototype.apply = function (f, args) {
+ try { f.apply(undefined, args) }
catch (err) { this.emit('error', err) }
};
View
@@ -1,30 +1,34 @@
-var Store = require('./store');
var traverse = require('traverse');
var objectKeys = require('./keys');
var forEach = require('./foreach');
+function indexOf (xs, x) {
+ if (xs.indexOf) return xs.indexOf(x);
+ for (var i = 0; i < xs.length; i++) if (xs[i] === x) return i;
+ return -1;
+}
+
// scrub callbacks out of requests in order to call them again later
-module.exports = function (store) {
+module.exports = function (callbacks) {
var self = {};
- store = store || new Store;
- self.callbacks = store.items;
+ self.callbacks = callbacks || [];
// Take the functions out and note them for future use
self.scrub = function (obj) {
var paths = {};
var links = [];
var args = traverse(obj).map(function (node) {
- if (typeof(node) == 'function') {
- var i = store.indexOf(node);
+ if (typeof node === 'function') {
+ var i = indexOf(self.callbacks, node);
if (i >= 0 && !(i in paths)) {
// Keep previous function IDs only for the first function
// found. This is somewhat suboptimal but the alternatives
// are worse.
paths[i] = this.path;
}
else {
- var id = store.add(node);
+ var id = self.callbacks.push(node) - 1;
paths[id] = this.path;
}
@@ -48,48 +52,18 @@ module.exports = function (store) {
self.unscrub = function (msg, f) {
var args = msg.arguments || [];
forEach(objectKeys(msg.callbacks || {}), function (strId) {
- var id = parseInt(strId,10);
+ var id = parseInt(strId, 10);
var path = msg.callbacks[id];
- args = setAt(args, path, f(id));
+ traverse.set(args, path, f(id));
});
forEach(msg.links || [], function (link) {
- var value = getAt(args, link.from);
- args = setAt(args, link.to, value);
+ var value = traverse.get(args, link.from);
+ traverse.set(args, link.to, value);
});
return args;
};
- function setAt (ref, path, value) {
- var node = ref;
- for (var i = 0; i < path.length - 1; i++) {
- var key = path[i];
- if (Object.propertyIsEnumerable.call(node, key)) {
- node = node[key];
- }
- else return undefined;
- };
- var last = path.slice(-1)[0];
- if (last === undefined) {
- return value;
- }
- else {
- node[last] = value;
- return ref;
- }
- }
-
- function getAt (node, path) {
- for (var i = 0; i < path.length; i++) {
- var key = path[i];
- if (Object.propertyIsEnumerable.call(node, key)) {
- node = node[key];
- }
- else return undefined;
- }
- return node;
- }
-
return self;
}
View
@@ -1,56 +0,0 @@
-var EventEmitter = require('events').EventEmitter;
-
-module.exports = function () {
- var self = new EventEmitter;
- var items = self.items = [];
-
- self.has = function (id) {
- return items[id] != undefined;
- };
-
- self.get = function (id) {
- if (!self.has(id)) return null;
- return wrap(items[id]);
- };
-
- self.add = function (fn, id) {
- if (id == undefined) id = items.length;
- items[id] = fn;
- return id;
- };
-
- self.cull = function (arg) {
- if (typeof arg == 'function') {
- arg = items.indexOf(arg);
- }
- delete items[arg];
- return arg;
- };
-
- self.indexOf = function (fn) {
- if (items.indexOf) return items.indexOf(fn);
- for (var i = 0; i < items.length; i++) {
- if (items[i] === fn) return i;
- }
- return -1;
- };
-
- function wrap (fn) {
- return function() {
- fn.apply(this, arguments);
- autoCull(fn);
- };
- }
-
- function autoCull (fn) {
- if (typeof fn.times == 'number') {
- fn.times--;
- if (fn.times == 0) {
- var id = self.cull(fn);
- self.emit('cull', id);
- }
- }
- }
-
- return self;
-};
View
@@ -1,47 +0,0 @@
-var test;
-try { test = require('tap').test; }
-catch (e) { test = require('testling') }
-var Store = require('../lib/store');
-
-test('store', function (t) {
- var s = new Store;
-
- t.deepEqual(s.items.length, 0);
-
- var i = 0;
- var cb = function() {
- i++;
- };
-
- var fn1 = function () {
- cb();
- };
- s.add(fn1);
- t.deepEqual(s.items.length, 1);
- s.get(0)();
- fn1();
- t.deepEqual(i, 2);
- t.deepEqual(s.items[0].times, undefined);
-
- var fn2 = function() {
- cb();
- };
- fn2.times = 2;
- s.add(fn2);
- t.deepEqual(s.items.length, 2);
- s.get(1)();
- fn2();
- t.deepEqual(i, 4);
- t.deepEqual(s.items[1].times, 1);
- s.get(1)();
- t.deepEqual(s.items[1], undefined);
-
- var fn3 = function() {
- cb2();
- };
- s.add(fn3);
- t.deepEqual(s.items.length, 3);
- t.end();
-});
-
-

0 comments on commit 6762af3

Please sign in to comment.