|
1 | 1 | import { |
| 2 | + DEFAULT_NODE_WIDTH, |
2 | 3 | type EdgeId, |
3 | | - type LayoutedGraph, |
4 | 4 | type LineageEdge, |
5 | 5 | type LineageEdgeData, |
6 | 6 | type LineageNodeData, |
7 | 7 | type LineageNodesMap, |
8 | 8 | type NodeId, |
9 | 9 | type PortId, |
10 | 10 | } from '../utils' |
| 11 | +import dagre from 'dagre' |
11 | 12 |
|
12 | | -const DEFAULT_TIMEOUT = 1000 * 60 // 1 minute |
13 | | - |
14 | | -let workerInstance: Worker | null = null |
15 | | - |
16 | | -function getWorker(): Worker { |
17 | | - if (workerInstance) return workerInstance |
18 | | - |
19 | | - workerInstance = new Worker( |
20 | | - new URL('./dagreLayout.worker.ts', import.meta.url), |
21 | | - { type: 'module' }, |
22 | | - ) |
23 | | - |
24 | | - return workerInstance |
25 | | -} |
26 | | - |
27 | | -export async function getLayoutedGraph< |
| 13 | +export function buildLayout< |
28 | 14 | TNodeData extends LineageNodeData = LineageNodeData, |
29 | 15 | TEdgeData extends LineageEdgeData = LineageEdgeData, |
30 | 16 | TNodeID extends string = NodeId, |
31 | 17 | TEdgeID extends string = EdgeId, |
32 | 18 | TPortID extends string = PortId, |
33 | | ->( |
34 | | - edges: LineageEdge<TEdgeData, TNodeID, TEdgeID, TPortID>[], |
35 | | - nodesMap: LineageNodesMap<TNodeData>, |
36 | | -): Promise<LayoutedGraph<TNodeData, TEdgeData, TNodeID, TEdgeID, TPortID>> { |
37 | | - let timeoutId: NodeJS.Timeout | null = null |
38 | | - |
39 | | - return new Promise((resolve, reject) => { |
40 | | - const nodes = Object.values(nodesMap) |
41 | | - |
42 | | - if (nodes.length === 0) return resolve({ edges: [], nodesMap: {} }) |
43 | | - |
44 | | - const worker = getWorker() |
45 | | - |
46 | | - if (worker == null) |
47 | | - return errorHandler(new ErrorEvent('Failed to create worker')) |
48 | | - |
49 | | - timeoutId = setTimeout( |
50 | | - () => errorHandler(new ErrorEvent('Layout calculation timed out')), |
51 | | - DEFAULT_TIMEOUT, |
52 | | - ) |
53 | | - |
54 | | - worker.addEventListener('message', handler) |
55 | | - worker.addEventListener('error', errorHandler) |
56 | | - |
57 | | - try { |
58 | | - worker.postMessage({ edges, nodesMap } as LayoutedGraph< |
59 | | - TNodeData, |
60 | | - TEdgeData, |
61 | | - TNodeID, |
62 | | - TEdgeID, |
63 | | - TPortID |
64 | | - >) |
65 | | - } catch (postError) { |
66 | | - errorHandler(postError as ErrorEvent) |
| 19 | +>({ |
| 20 | + edges, |
| 21 | + nodesMap, |
| 22 | +}: { |
| 23 | + edges: LineageEdge<TEdgeData, TNodeID, TEdgeID, TPortID>[] |
| 24 | + nodesMap: LineageNodesMap<TNodeData> |
| 25 | +}) { |
| 26 | + const nodes = Object.values(nodesMap) |
| 27 | + const nodeCount = nodes.length |
| 28 | + const edgeCount = edges.length |
| 29 | + |
| 30 | + if (nodeCount === 0) |
| 31 | + return { |
| 32 | + edges: [], |
| 33 | + nodesMap: {}, |
67 | 34 | } |
68 | 35 |
|
69 | | - function handler( |
70 | | - event: MessageEvent< |
71 | | - LayoutedGraph<TNodeData, TEdgeData, TNodeID, TEdgeID, TPortID> & { |
72 | | - error: ErrorEvent |
73 | | - } |
74 | | - >, |
75 | | - ) { |
76 | | - cleanup() |
77 | | - |
78 | | - if (event.data.error) return errorHandler(event.data.error) |
79 | | - |
80 | | - resolve(event.data) |
81 | | - } |
| 36 | + const g = new dagre.graphlib.Graph({ |
| 37 | + compound: true, |
| 38 | + multigraph: true, |
| 39 | + directed: true, |
| 40 | + }) |
82 | 41 |
|
83 | | - function errorHandler(error: ErrorEvent) { |
84 | | - cleanup() |
85 | | - reject(error) |
86 | | - } |
| 42 | + g.setGraph({ |
| 43 | + rankdir: 'LR', |
| 44 | + nodesep: 0, |
| 45 | + ranksep: 48, |
| 46 | + edgesep: 0, |
| 47 | + ranker: 'longest-path', |
| 48 | + }) |
87 | 49 |
|
88 | | - function cleanup() { |
89 | | - if (timeoutId) { |
90 | | - clearTimeout(timeoutId) |
91 | | - timeoutId = null |
92 | | - } |
93 | | - worker?.removeEventListener('message', handler) |
94 | | - worker?.removeEventListener('error', errorHandler) |
| 50 | + g.setDefaultEdgeLabel(() => ({})) |
| 51 | + |
| 52 | + // Building layout already heavy operation, so trying to optimize it a bit |
| 53 | + for (let i = 0; i < edgeCount; i++) { |
| 54 | + g.setEdge(edges[i].source, edges[i].target) |
| 55 | + } |
| 56 | + |
| 57 | + for (let i = 0; i < nodeCount; i++) { |
| 58 | + const node = nodes[i] |
| 59 | + g.setNode(node.id, { |
| 60 | + width: node.width || DEFAULT_NODE_WIDTH, |
| 61 | + height: node.height || 0, |
| 62 | + }) |
| 63 | + } |
| 64 | + |
| 65 | + dagre.layout(g) |
| 66 | + |
| 67 | + // Building layout already heavy operation, so trying to optimize it a bit |
| 68 | + for (let i = 0; i < nodeCount; i++) { |
| 69 | + const node = nodes[i] |
| 70 | + const width = node.width || DEFAULT_NODE_WIDTH |
| 71 | + const height = node.height || 0 |
| 72 | + const nodeId = node.id as NodeId |
| 73 | + const nodeWithPosition = g.node(nodeId) |
| 74 | + const halfWidth = width / 2 |
| 75 | + const halfHeight = height / 2 |
| 76 | + |
| 77 | + nodesMap[nodeId] = { |
| 78 | + ...node, |
| 79 | + position: { |
| 80 | + x: nodeWithPosition.x - halfWidth, |
| 81 | + y: nodeWithPosition.y - halfHeight, |
| 82 | + }, |
95 | 83 | } |
96 | | - }) |
97 | | -} |
| 84 | + } |
98 | 85 |
|
99 | | -export function cleanupLayoutWorker(): void { |
100 | | - workerInstance?.terminate() |
101 | | - workerInstance = null |
| 86 | + return { |
| 87 | + edges, |
| 88 | + nodesMap, |
| 89 | + } |
102 | 90 | } |
0 commit comments