Permalink
Browse files

[api] Added support for actually resolving circular deps based on delay

  • Loading branch information...
1 parent 0ca59cd commit 27902e07d01dad8728982159f86c6fcbe40e2b98 @Marak Marak committed Dec 30, 2011
Showing with 20 additions and 7 deletions.
  1. +1 −2 example/mutli-top-level-nodes.js
  2. +19 −5 lib/hnet.js
@@ -16,8 +16,7 @@ hnet.on('level::*', function(nodes, options){
//
hnet.load(['http://hnet.iriscouch.com/public/0',
'https://raw.github.com/gist/01889d7e9e8cc35375c8/9e26dfeeb6e641a33dae4961196235bdb965b21b/h',
- 'https://raw.github.com/gist/697ffae7ed038c2f12f9/7d93ea8a75af4b167641ae2db8a7567f23dfd546/h0',
+ 'https://raw.github.com/gist/697ffae7ed038c2f12f9/2617d48408d6e30f5c4b0316d9892e564dd0c951/h0',
'http://hnet.iriscouch.com/public/1',
'http://hnet.iriscouch.com/public/2',
]);
-
View
@@ -37,6 +37,9 @@ var Hnet = exports.Hnet = function (options) {
// Default node depth that hnet will jump
self.maxDepth = options.maxDepth || 100;
+ // Default to not resolviing circular data sets
+ self.circularDelay = options.circularDelay || -1;
+
//
// Load engines and cyphers
//
@@ -64,7 +67,7 @@ util.inherits(Hnet, EventEmitter);
//
// Parses a JSON document for possible instructions
//
-Hnet.prototype.parseDocument = function (engine, doc, options) {
+Hnet.prototype.parseDocument = function (node, doc, options) {
var self = this;
try {
doc = JSON.parse(doc);
@@ -105,7 +108,15 @@ Hnet.prototype.parseDocument = function (engine, doc, options) {
//
// Attempt to load another resource
//
- self.load(d.params, { currentDepth: options.currentDepth + 1, maxDepth: self.maxDepth });
+ if (self.circularDelay && options.currentDepth > 0 && self.nodes[node.uri]) {
+ setTimeout(function(){
+ self.load(d.params, { currentDepth: options.currentDepth + 1, maxDepth: self.maxDepth });
+ }, self.circularDelay)
+ } else {
+ self.load(d.params, { currentDepth: options.currentDepth + 1, maxDepth: self.maxDepth });
+ }
+
+
}
}
@@ -162,7 +173,6 @@ Hnet.prototype.load = function(nodes, options, cb){
uri: node
};
}
-
request({ uri: node.uri }, function(err, response, body) {
//
@@ -179,11 +189,11 @@ Hnet.prototype.load = function(nodes, options, cb){
});
}
- if(self.nodes[node.uri]){
+ if(self.nodes[node.uri] && self.circularDelay < 0){
return cb(new Error('Already loaded ' + node.uri + ' Will not re-load circular dependency'));
}
- if(self.engines[node.type].load) {
+ function loadSet () {
self.engines[node.type].load(node, function(err, response, doc){
doc = self.parseDocument(node, body, options);
self.nodes[node.uri] = node;
@@ -192,6 +202,10 @@ Hnet.prototype.load = function(nodes, options, cb){
});
}
+ if(self.engines[node.type].load) {
+ loadSet();
+ }
+
});
}, function(err){

0 comments on commit 27902e0

Please sign in to comment.