No description, website, or topics provided.
Switch branches/tags
Nothing to show
Clone or download
Latest commit 09b2f97 Dec 4, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
test test reverse and max Sep 26, 2018
LICENSE initial Jul 23, 2018 document start, max, reverse Sep 26, 2018
index.js notify onReady after updating graph (not before) Dec 3, 2018
package.json 1.1.2 Dec 4, 2018


compose a graph out of multiple sublayers, and in particular, expose a dynamically updating shortest paths calculation.

later added layers override earlier layers.

api: LayeredGraph({start, max}) => layers

start is a node id that is the "root" of the graph. hops are calculated from this node. max is a float that is the maximum path length to include in the hops calculation.

layers.createLayer (name) => add(g) || add(from, to, value)

create a layer in this graph. returns an add function. The add function should be called with an initial graph, and then new edges. Each layer must be initialized. add({}) is a valid initialization, which is adding an empty graph. add(a, b, 1) would be adding a single edge with weight 1 between a and b.

layers.getGraph() => g

returns the current layered graph merged into one layer. the graph is just a two level js object {}, structure {<id_a>:{<id_b>: <weight>},...}

layers.getHops(opts?) => {: }

return a hops map, of each peer id, to their hop length from start (passed to constructor) If opts is provided, it accepts the following fields: reverse: return hops to start instead of from start. start: calculate hops from/to a different node. max: set a different max distance. If the max is smaller than the default passed to the constructor, the output will be fastest, because it will just copy the cached value, but skip nodes at a greater distance than max.

layers.hopStream() => Source

returns a pull-stream source, where each message is a hops object (as returned by getHops) the first item will be the current state, and any subsequent objects will be diffs to that object, created by edges being added or removed in some layer in real time.

layers.onReady (fn)

call fn back once all layers have been initialized, or immediately if they are already initialized.

layers.onEdge (fn(from, to, value))

call fn when an edge is added or removed from the graph.