Use Nodes Data
Nodes that compute from the data of the nodes connected to them, as React Flow's useNodesData and useNodeConnections do. The source holds a number in its Data; every other node works its value out from its incoming connections as it renders, so changing the number or rewiring an edge updates everything downstream.
Press Tab to move between the nodes and edges of the diagram.
Press Enter or Space to select it, Shift with Enter to add it to the selection, the arrow keys to move the selected nodes, Delete to remove them and Escape to clear the selection. Press C to start a connection from it.
Press Enter or Space to select it, Delete to remove it and Escape to clear the selection. Press C to move its end to another node.
bit BlowTry it
- Drag the slider in Source: Double, Square and Sum all recompute as it moves.
- Click the edge from Square to Sum and press Delete: Sum now adds up only what still reaches it. Delete the edge into Double and Double shows no input.
- Drag from a handle to rewire: connect Source straight to Sum, or Double into Square, and the values follow the new wiring.
- Drag a node anywhere: where it sits changes nothing, only what is connected to it.
How it works
- A node's
Dataholds whatever the application needs: aCounteron the source, and anOperationon each computing node. TheNodeTemplatedraws each by the type of its data. - A computing node's value is worked out as the template renders it:
BlowGraphUtils.GetIncomers(node, nodes, edges)returns the nodes with an edge into it - whatuseNodeConnectionsanswers - and their data gives its inputs - whatuseNodesDatareads. The operation is applied to the sum of the inputs; with none, there is no value. - Nothing computed is stored on a node, so nothing can go stale: the value is always that of the graph as it is drawn. A walk that comes back to a node it is already computing stops there, so wiring a loop shows no value rather than hanging.
- The slider carries the
nodragclass, the canvas's defaultNoDragClass, and stops its own pointer-down, so dragging it moves the value and not the node. - Moving the slider runs a handler of the page, which re-renders the page and with it the canvas and every template.
EdgesChangeddoes the same when an edge is connected or deleted on the canvas.