Permalink
Browse files

Dependency not interfered by others (issue #6)

  • Loading branch information...
1 parent a91ba25 commit 48f81f1ec3b852b92d13b2bf451fceb412a97fa9 Yuest Wang committed May 25, 2012
Showing with 29 additions and 19 deletions.
  1. +27 −16 lib/dep-graph.js
  2. +2 −3 src/dep-graph.coffee
View
43 lib/dep-graph.js
@@ -1,16 +1,16 @@
+// Generated by CoffeeScript 1.3.3
(function() {
- var DepGraph, _;
- var __indexOf = Array.prototype.indexOf || function(item) {
- for (var i = 0, l = this.length; i < l; i++) {
- if (this[i] === item) return i;
- }
- return -1;
- }, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
+ var DepGraph, _,
+ __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
+
_ = require('underscore');
+
DepGraph = (function() {
+
function DepGraph() {
this.map = {};
}
+
DepGraph.prototype.add = function(id, depId) {
var _base, _ref;
if ((_ref = (_base = this.map)[id]) == null) {
@@ -22,31 +22,36 @@
this.map[id].push(depId);
return this.map[id];
};
+
DepGraph.prototype.getChain = function(id) {
- var chain, deps, leafNode, visit, visited, _i, _len, _ref;
+ var chain, deps, leafNode, visit, visited, _i, _len, _ref,
+ _this = this;
deps = this.descendantsOf(id);
chain = [];
visited = {};
- visit = __bind(function(node) {
+ visit = function(node) {
var parent, _i, _len, _ref;
if (visited[node] || node === id) {
return;
}
visited[node] = true;
- _ref = this.parentsOf(node);
+ _ref = _this.parentsOf(node);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
parent = _ref[_i];
- visit(parent);
+ if (__indexOf.call(deps, parent) >= 0) {
+ visit(parent);
+ }
}
return chain.unshift(node);
- }, this);
+ };
_ref = _.intersection(deps, this.leafNodes()).reverse();
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
leafNode = _ref[_i];
visit(leafNode);
}
return chain;
};
+
DepGraph.prototype.leafNodes = function() {
var allNodes, node, _i, _len, _ref, _results;
allNodes = _.uniq(_.flatten(_.values(this.map)));
@@ -59,6 +64,7 @@
}
return _results;
};
+
DepGraph.prototype.parentsOf = function(child) {
var node, _i, _len, _ref, _results;
_ref = _.keys(this.map);
@@ -71,8 +77,9 @@
}
return _results;
};
+
DepGraph.prototype.descendantsOf = function(parent, descendants, branch) {
- var child, _i, _len, _ref, _ref2;
+ var child, _i, _len, _ref, _ref1;
if (descendants == null) {
descendants = [];
}
@@ -81,9 +88,9 @@
}
descendants.push(parent);
branch.push(parent);
- _ref2 = (_ref = this.map[parent]) != null ? _ref : [];
- for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
- child = _ref2[_i];
+ _ref1 = (_ref = this.map[parent]) != null ? _ref : [];
+ for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
+ child = _ref1[_i];
if (__indexOf.call(branch, child) >= 0) {
throw new Error("Cyclic dependency from " + parent + " to " + child);
}
@@ -94,11 +101,15 @@
}
return descendants.slice(1);
};
+
return DepGraph;
+
})();
+
if ((typeof module !== "undefined" && module !== null ? module.exports : void 0) != null) {
module.exports = DepGraph;
} else {
this.DepGraph = DepGraph;
}
+
}).call(this);
View
5 src/dep-graph.coffee
@@ -27,8 +27,7 @@ class DepGraph
visit = (node) =>
return if visited[node] or node is id
visited[node] = true
- for parent in @parentsOf(node)
- visit parent
+ visit parent for parent in @parentsOf(node) when parent in deps
chain.unshift node
for leafNode in _.intersection(deps, @leafNodes()).reverse()
@@ -57,4 +56,4 @@ class DepGraph
if module?.exports?
module.exports = DepGraph
else
- @DepGraph = DepGraph
+ @DepGraph = DepGraph

0 comments on commit 48f81f1

Please sign in to comment.