Permalink
Browse files

Add reverse lookup to dependency graph

  • Loading branch information...
jnordberg committed Mar 20, 2013
1 parent 5ccd1fc commit 2918c7968363b38a4be9b8d6872d53f5b745c8d5
Showing with 20 additions and 4 deletions.
  1. +20 −4 src/core/graph.coffee
View
@@ -11,7 +11,8 @@ class Node
@edges = []
addEdge: (node) ->
- @edges.push node
+ if node not in @edges
+ @edges.push node
class Graph
@@ -32,12 +33,17 @@ class Graph
node.addEdge @nodeFor dependency
dependenciesFor: (item) ->
- ### return an array with all depdenencies for *item* ###
+ ### return a list of items that *item* depends on ###
node = @nodeFor item
resolved = @resolveNode node
resolved.splice resolved.indexOf(node), 1
return resolved.map (node) -> node.item
+ dependsOn: (item) ->
+ ### return a list of items depending on *item* ###
+ target = @nodeFor item
+ return @reverseLookup(target).map (node) -> node.item
+
### private ###
nodeFor: (item) ->
@@ -51,6 +57,14 @@ class Graph
resolved.push node
return resolved
+ reverseLookup: (target) ->
+ rv = []
+ for id, node of @nodes
+ continue if node is target
+ if target in @resolveNode node
+ rv.push node
+ return rv
+
class GraphHandler
constructor: (@target, @fn) ->
@@ -133,7 +147,7 @@ buildGraph = (env, contents, templates, locals, callback) ->
proxy = GraphHandler.proxy contents, (dep) ->
graph.addDependency current, dep
- locals.contents = proxy
+ locals.contents = proxy # TODO: don't modify locals
async.eachSeries items, (item, callback) ->
current = item
@@ -142,4 +156,6 @@ buildGraph = (env, contents, templates, locals, callback) ->
, (error) ->
callback error, graph
-exports.buildGraph = buildGraph
+### Exports ###
+
+module.exports = {Graph, GraphHandler, buildGraph}

0 comments on commit 2918c79

Please sign in to comment.