-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
173 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,10 +43,17 @@ | |
} | ||
|
||
|
||
function getCircles() { | ||
return "NOT Available" | ||
} | ||
function addDep(node, mod) { | ||
if (!mod) return | ||
|
||
forEach(mod.dependencies, function(subId) { | ||
var subNode = graph.add(subId) | ||
node.addEdge(subNode) | ||
addDep(subNode, cachedMods[mod.resolve(subId)]) | ||
}) | ||
|
||
window.graph = graph | ||
} | ||
|
||
// Helpers | ||
|
||
|
@@ -63,6 +70,169 @@ | |
return -1 | ||
} | ||
|
||
function forEach(arrs, cb) { | ||
for (var i = 0, len = arrs.length; i < len; i++) { | ||
cb(arrs[i], i) | ||
} | ||
} | ||
|
||
function Graph() { | ||
this.nodes = [] | ||
} | ||
|
||
Graph.prototype = { | ||
add: function(name, data) { | ||
return this.getNode(name, data) | ||
}, | ||
|
||
getNode: function(name, data) { | ||
var ns = this.nodes | ||
var temp = { | ||
name: name | ||
} | ||
|
||
for (var i = 0, len = ns.length; i < len; i++) { | ||
if (ns[i].equals(temp)) { | ||
return ns[i] | ||
} | ||
} | ||
|
||
var node = new Node(name) | ||
node.data = data | ||
this.nodes.push(node) | ||
return node | ||
}, | ||
|
||
// 获取排序好的模块. | ||
sort: function() { | ||
var L = [] | ||
var S = [] | ||
forEach(this.nodes, function(node) { | ||
if (node.inEdges.length == 0) { | ||
S.push(node) | ||
} | ||
}) | ||
|
||
while(S.length) { | ||
var node = S.shift() | ||
L.push(node) | ||
|
||
while(node.outEdges.length) { | ||
var e = node.outEdges.shift() | ||
var m = e.to | ||
e.remove() | ||
|
||
if (m.inEdges.length == 0) { | ||
S.push(m) | ||
} | ||
} | ||
} | ||
|
||
var cycleNodes = this.nodes.filter(function(node) { | ||
return node.inEdges.length != 0 | ||
}) | ||
|
||
if (cycleNodes.length) { | ||
printCycleNode(cycleNodes) | ||
throw new Error('发现模块的循环依赖') | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} else { | ||
return L.reverse() | ||
} | ||
}, | ||
|
||
clone: function() { | ||
var g = new Graph() | ||
|
||
forEach(this.nodes, function(node) { | ||
var n = g.add(node.name, node.data) | ||
|
||
forEach(node.outEdges, function(e) { | ||
var to = e.to | ||
n.addEdge(g.add(to.name, to.data)) | ||
}) | ||
}) | ||
return g | ||
} | ||
} | ||
|
||
function Node(name) { | ||
this.name = name | ||
this.inEdges = [] | ||
this.outEdges = [] | ||
this.depth = 0 | ||
} | ||
|
||
Node.prototype = { | ||
addEdge: function(node) { | ||
var e = new Edge(this, node) | ||
this.outEdges.push(e) | ||
node.inEdges.push(e) | ||
// 增加边的时候,深度加1 | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
lifesinger
via email
Member
|
||
node.setDepth(this.depth + 1) | ||
return this | ||
}, | ||
|
||
setDepth: function(depth) { | ||
if (depth < this.depth) return | ||
this.depth = depth | ||
}, | ||
|
||
equals: function(node) { | ||
return node.name == this.name | ||
} | ||
} | ||
|
||
function Edge(from, to) { | ||
this.from = from | ||
this.to = to | ||
} | ||
|
||
Edge.prototype = { | ||
equals: function(edge) { | ||
return edge.from == this.from && edge.to == this.to | ||
}, | ||
|
||
// 入边删除 | ||
remove: function() { | ||
var toInEdges = this.to.inEdges.slice(0) | ||
for (var i = 0, len = toInEdges.length; i < len; i++) { | ||
if (toInEdges[i].equals(this)) { | ||
remove(this.to.inEdges, toInEdges[i]) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
function remove(arr, item) { | ||
arr.splice(arr.indexOf(item), 1) | ||
} | ||
|
||
function printCycleNode(nodes) { | ||
console.info(nodes.map(function(node) { | ||
return node.name | ||
})) | ||
} | ||
|
||
function isRoot(id) { | ||
return /_use_\d+$/.test(id) | ||
} | ||
|
||
function getCircles() { | ||
var graph = new Graph() | ||
var roots = [] | ||
|
||
for (var key in cachedMods) { | ||
if (isRoot(key)) { | ||
var mod = cachedMods[key] | ||
var node = graph.add(key) | ||
addDep(node, mod) | ||
} | ||
} | ||
|
||
return "NOT Available" | ||
} | ||
|
||
|
||
// Register as module | ||
define("seajs-health", [], {}) | ||
|
3 comments
on commit 049715d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好, 收到!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
最好可以本地化。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
源码中还是不好出现中文吧。