MCPcopy Create free account
hub / github.com/thetarnav/solid-devtools / calculateNodeOrder

Function calculateNodeOrder

packages/frontend/src/dgraph.tsx:52–128  ·  view source on GitHub ↗
(
    graph: debug.SerializedDGraph.Graph,
    inspectedId: debug.NodeID,
    inspectedNode: debug.SerializedDGraph.Node,
)

Source from the content-addressed store, hash-verified

50}
51
52function calculateNodeOrder(
53 graph: debug.SerializedDGraph.Graph,
54 inspectedId: debug.NodeID,
55 inspectedNode: debug.SerializedDGraph.Node,
56): NodeOrder {
57 const depthGroups = new Map<number, debug.NodeID[]>([[inspectedNode.depth, [inspectedId]]])
58
59 /*
60 breadth-first traversal
61 direct observers will be included closest to the inspected node
62 and further observers will be included further away
63
64 observers and sources are checked in parallel
65 by one list of ids per cycle
66 and the cycle is repeated until all ids are checked
67
68 so that the closer the node is to the inspected node in the d-graph
69 (no matter if it's an observer or a source)
70 the closer it will be to the inspected node in the list
71 and more likely to be on the correct side of the inspected node
72 (sources are added to the top of the list, observers to the bottom)
73 */
74
75 const flowOrder = [inspectedId]
76 const checkedObservers = new Set(flowOrder)
77 const checkedSources = new Set(flowOrder)
78
79 const toCheck: [sources: (readonly debug.NodeID[])[], observers: (readonly debug.NodeID[])[]] = [
80 inspectedNode.sources ? [inspectedNode.sources] : [],
81 inspectedNode.observers ? [inspectedNode.observers] : [],
82 ]
83 let index = 0
84 let checkingObservers: 0 | 1 = 0
85 let ids = toCheck[checkingObservers][0] ?? toCheck[(checkingObservers = 1)][index]
86 while (ids) {
87 for (const id of ids) {
88 const node = graph[id]
89 if (!node) continue
90
91 const inSources = checkedSources.has(id)
92 const inObservers = checkedObservers.has(id)
93
94 if (!inSources && node.sources && node.sources.length)
95 toCheck[0].push(node.sources)
96 if (!inObservers && node.observers && node.observers.length)
97 toCheck[1].push(node.observers)
98
99 if (!inSources && !inObservers) {
100 flowOrder[checkingObservers ? 'push' : 'unshift'](id)
101 const depthList = depthGroups.get(node.depth)
102 if (depthList) depthList.push(id)
103 else depthGroups.set(node.depth, [id])
104 }
105
106 checkingObservers
107 ? inObservers || checkedObservers.add(id)
108 : inSources || checkedSources.add(id)
109 }

Callers 1

Dgraph_ViewFunction · 0.85

Calls 1

getMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…