Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Dnode related fix #7

Open
wants to merge 2 commits into from

1 participant

@Marsup

Hello substack,

After the update to 0.1.0, dnode failed on some tests, this pull request fixes this.
There are also some code changes because I found it more readable, though you may not agree with me :)

Hope that'll help a bit to upgrade dnode :p

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 25, 2011
  1. @Marsup
  2. @Marsup

    Bump version

    Marsup authored
This page is out of date. Refresh to see the latest.
Showing with 37 additions and 30 deletions.
  1. +3 −0  .gitignore
  2. +32 −28 index.js
  3. +1 −1  package.json
  4. +1 −1  test/store.js
View
3  .gitignore
@@ -0,0 +1,3 @@
+node_modules/
+npm-debug.log
+
View
60 index.js
@@ -34,7 +34,7 @@ var Session = exports.Session = function (id, wrapper) {
self.remote = {};
var instance = self.instance =
- typeof(wrapper) == 'function'
+ typeof(wrapper) === 'function'
? new wrapper(self.remote, self)
: wrapper || {}
;
@@ -69,7 +69,7 @@ var Session = exports.Session = function (id, wrapper) {
self.parse = function (line) {
var msg = null;
- try { msg = JSON.parse(line) }
+ try { msg = JSON.parse(line); }
catch (err) {
self.emit('error', new SyntaxError(
'Error parsing JSON message: ' + JSON.stringify(line))
@@ -77,8 +77,8 @@ var Session = exports.Session = function (id, wrapper) {
return;
}
- try { self.handle(msg) }
- catch (err) { self.emit('error', err) }
+ try { self.handle(msg); }
+ catch (err) { self.emit('error', err); }
};
self.handle = function (req) {
@@ -101,9 +101,7 @@ var Session = exports.Session = function (id, wrapper) {
self.emit('remoteError', methods);
}
else if (req.method === 'cull') {
- args.forEach(function (id) {
- self.remoteStore.cull(args);
- });
+ self.remoteStore.cull(args);
}
else if (typeof req.method === 'string') {
if (self.instance.propertyIsEnumerable(req.method)) {
@@ -115,13 +113,13 @@ var Session = exports.Session = function (id, wrapper) {
));
}
}
- else if (typeof req.method == 'number') {
+ else if (typeof req.method === 'number') {
apply(self.localStore.get(req.method), self.instance, args);
}
- }
+ };
function handleMethods (methods) {
- if (typeof methods != 'object') {
+ if (typeof methods !== 'object') {
methods = {};
}
@@ -139,8 +137,8 @@ var Session = exports.Session = function (id, wrapper) {
}
function apply(f, obj, args) {
- try { f.apply(obj, args) }
- catch (err) { self.emit('error', err) }
+ try { f.apply(obj, args); }
+ catch (err) { self.emit('error', err); }
}
return self;
@@ -158,7 +156,7 @@ var Scrubber = exports.Scrubber = function (store) {
var links = [];
var args = Traverse(obj).map(function (node) {
- if (typeof(node) == 'function') {
+ if (typeof(node) === 'function') {
var i = store.indexOf(node);
if (i >= 0 && !(i in paths)) {
// Keep previous function IDs only for the first function
@@ -211,8 +209,8 @@ var Scrubber = exports.Scrubber = function (store) {
if (Object.propertyIsEnumerable.call(node, key)) {
node = node[key];
}
- else return undefined;
- };
+ else return;
+ }
var last = path.slice(-1)[0];
if (last === undefined) {
return value;
@@ -229,38 +227,47 @@ var Scrubber = exports.Scrubber = function (store) {
if (Object.propertyIsEnumerable.call(node, key)) {
node = node[key];
}
- else return undefined;
+ else return;
}
return node;
}
return self;
-}
+};
var Store = exports.Store = function() {
var self = new EventEmitter;
var items = self.items = [];
+ var wrapped = [];
self.has = function (id) {
- return items[id] != undefined;
+ return items[id] !== undefined;
};
self.get = function (id) {
if (!self.has(id)) return null;
- return wrap(items[id]);
+ return wrapped[id];
};
self.add = function (fn, id) {
- if (id == undefined) id = items.length;
+ if (typeof id === 'undefined') {
+ id = items.length;
+ }
items[id] = fn;
+ wrapped[id] = wrap(fn);
return id;
};
self.cull = function (arg) {
- if (typeof arg == 'function') {
+ if (typeof arg === 'function') {
arg = items.indexOf(arg);
+ } else if(arg instanceof Array) {
+ return arg.forEach(function(id) {
+ self.cull(id);
+ });
}
delete items[arg];
+ delete wrapped[arg];
return arg;
};
@@ -276,13 +283,10 @@ var Store = exports.Store = function() {
}
function autoCull (fn) {
- if (typeof fn.times == 'number') {
- fn.times--;
- if (fn.times == 0) {
- var id = self.cull(fn);
- self.emit('cull', id);
- }
- }
+ if (typeof fn.times !== 'number') return;
+ if(--fn.times) return;
+ var id = self.cull(fn);
+ self.emit('cull', id);
}
return self;
View
2  package.json
@@ -1,7 +1,7 @@
{
"name": "dnode-protocol",
"description": "Implements the dnode protocol abstractly",
- "version": "0.1.0",
+ "version": "0.1.1",
"repository": "https://github.com/substack/dnode-protocol.git",
"author": {
"name" : "James Halliday",
View
2  test/store.js
@@ -1,5 +1,5 @@
var assert = require('assert');
-var Store = require('dnode-protocol').Store;
+var Store = require('../').Store;
exports.store = function () {
var s = new Store;
Something went wrong with that request. Please try again.