3 Graph + Fn: Execute functions as a graph.
7 Grafn lets you execute sequences of async functions by defining them as
10 This allows you to easily define and manipulate complex dependencies in
15 In order to use it you must first create a graph.
18 const Grafn = require('grafn');
20 const graph = new Grafn();
23 Next you need to add some vertexes.
35 This is a vertex without dependencies, it means it can be executed without
36 waiting for anything else. The return value of this function will be stored
37 so it can be used by its dependents.
39 You can add dependencies by listing the names as an array in the vertex definition.
44 dependencies: ['root'],
47 return state.root + state.root;
52 Notice how the result of the `root` vertex is available in this action.
54 Vertexes will be executed as soon as all of their dependencies are met. For
55 example, consider the following graph
58 const Grafn = require('grafn');
60 const graph = new Grafn();
72 dependencies: ['root'],
75 return state.root + state.root;
81 dependencies: ['root'],
84 return state.root - state.root;
89 name: 'multiplication',
90 dependencies: ['root'],
93 return state.root * state.root;
99 dependencies: ['root'],
102 return state.root / state.root;
108 dependencies: ['addition', 'subtraction', 'multiplication', 'division'],
111 console.log('+', state.addition);
112 console.log('-', state.subtraction);
113 console.log('*', state.multiplication);
114 console.log('/', state.division);
119 The `addition`, `subtraction`, `multiplication`, and `division` vertices will
120 all run as soon as `roott` is executed. However, the final `tally` vertex won't
121 run until all other operations are complete.
123 You can visualize the graph by using the `graph.toString()` method. This
124 will output a graphviz digraph:
134 root -> multiplication
140 multiplication -> tally
145 ![execution graph, before execution][graph-pre-exec]
147 You can easily change the shape of the graph by changing the dependencies array.
149 In order to start execution of the graph you should select which node to start
156 After an execution, you can use `graph.toString()` to check the result of an
157 execution. It will highlight which nodes were executed successfully (green), and
158 which nodes threw an error (red).
160 ![execution graph, after execution][graph-post-exec]
164 This project implements the same ideas behind [fluorine][fluorine], a similar
165 graph based library. Grafn wouldn't exist without it.
167 [fluorine]: https://github.com/freshout-dev/fluorine
168 [graph-pre-exec]: doc/images/graph-pre-exec.svg
169 [graph-post-exec]: doc/images/graph-post-exec.svg