Skip to content

Commit

Permalink
U: update circles check
Browse files Browse the repository at this point in the history
  • Loading branch information
leoner committed Aug 12, 2013
1 parent d6d1770 commit 049715d
Showing 1 changed file with 173 additions and 3 deletions.
176 changes: 173 additions & 3 deletions src/seajs-health.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.

Copy link
@afc163

afc163 Aug 12, 2013

Member

源码中还是不好出现中文吧。

} 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.

Copy link
@afc163

afc163 Aug 12, 2013

Member

包括注释。

This comment has been minimized.

Copy link
@popomore

popomore Aug 12, 2013

Member

注释用中文应该没啥问题吧,有些表达用英文可能看不懂。

This comment has been minimized.

Copy link
@lifesinger

lifesinger via email Aug 13, 2013

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", [], {})
Expand Down

3 comments on commit 049715d

@leoner
Copy link
Member Author

@leoner leoner commented on 049715d Aug 13, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好, 收到!

@hotoo
Copy link
Member

@hotoo hotoo commented on 049715d Aug 13, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最好可以本地化。

@leoner
Copy link
Member Author

@leoner leoner commented on 049715d Aug 13, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hotoo 有啥需求可以 #1 在这个地方记录下呀.

Please sign in to comment.