Skip to content

Commit

Permalink
Add reverse lookup to dependency graph
Browse files Browse the repository at this point in the history
  • Loading branch information
jnordberg committed Mar 20, 2013
1 parent 5ccd1fc commit 2918c79
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/core/graph.coffee
Expand Up @@ -11,7 +11,8 @@ class Node
@edges = []

addEdge: (node) ->
@edges.push node
if node not in @edges
@edges.push node

class Graph

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