|
| 1 | +/** |
| 2 | + * Hook for using graphology's built-in ForceAtlas2 web worker. |
| 3 | + * This uses the production-ready worker implementation from graphology-layout-forceatlas2. |
| 4 | + */ |
| 5 | + |
| 6 | +import { useCallback, useEffect, useRef } from 'react'; |
| 7 | +import type Graph from 'graphology'; |
| 8 | +import random from 'graphology-layout/random.js'; |
| 9 | +import FA2Layout from 'graphology-layout-forceatlas2/worker'; |
| 10 | +import type { ForceAtlas2LayoutInputs } from './forceatlas2'; |
| 11 | + |
| 12 | +export interface FA2WorkerResult { |
| 13 | + /** Whether the layout completed successfully */ |
| 14 | + success: boolean; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Hook that provides access to graphology's ForceAtlas2 web worker. |
| 19 | + * The FA2Layout worker runs the layout algorithm off the main thread, |
| 20 | + * updating node positions directly on the graph object. |
| 21 | + */ |
| 22 | +export function useForceAtlas2Worker() { |
| 23 | + const layoutRef = useRef<any | null>(null); |
| 24 | + const isRunningRef = useRef(false); |
| 25 | + |
| 26 | + // Cleanup on unmount |
| 27 | + useEffect(() => { |
| 28 | + return () => { |
| 29 | + if (layoutRef.current) { |
| 30 | + layoutRef.current.kill(); |
| 31 | + layoutRef.current = null; |
| 32 | + } |
| 33 | + }; |
| 34 | + }, []); |
| 35 | + |
| 36 | + /** |
| 37 | + * Run ForceAtlas2 layout using web worker. |
| 38 | + * Returns a promise that resolves when layout runs for sufficient time. |
| 39 | + */ |
| 40 | + const runLayout = useCallback( |
| 41 | + async ( |
| 42 | + graph: Graph, |
| 43 | + options: Omit<ForceAtlas2LayoutInputs, 'graph' | 'drags'> |
| 44 | + ): Promise<FA2WorkerResult> => { |
| 45 | + // Kill any existing layout |
| 46 | + if (layoutRef.current) { |
| 47 | + layoutRef.current.kill(); |
| 48 | + layoutRef.current = null; |
| 49 | + } |
| 50 | + |
| 51 | + return new Promise((resolve) => { |
| 52 | + const { iterations = 50, ...settings } = options; |
| 53 | + |
| 54 | + // FA2 requires nodes to have initial x,y positions |
| 55 | + // Assign random positions if not present |
| 56 | + random.assign(graph); |
| 57 | + |
| 58 | + // Create the FA2Layout worker |
| 59 | + const layout = new FA2Layout(graph, { |
| 60 | + settings |
| 61 | + }); |
| 62 | + |
| 63 | + layoutRef.current = layout; |
| 64 | + isRunningRef.current = true; |
| 65 | + |
| 66 | + // FA2Layout worker runs continuously, we need to stop it after sufficient iterations |
| 67 | + // Each requestAnimationFrame is roughly one "tick" - we'll run for iterations * 16ms |
| 68 | + const runTimeMs = Math.max(500, iterations * 10); // At least 500ms, or 10ms per iteration |
| 69 | + |
| 70 | + // Start the layout |
| 71 | + layout.start(); |
| 72 | + |
| 73 | + // Stop after the calculated run time |
| 74 | + setTimeout(() => { |
| 75 | + if (layoutRef.current && isRunningRef.current) { |
| 76 | + layout.stop(); |
| 77 | + isRunningRef.current = false; |
| 78 | + resolve({ success: true }); |
| 79 | + } else { |
| 80 | + resolve({ success: false }); |
| 81 | + } |
| 82 | + }, runTimeMs); |
| 83 | + }); |
| 84 | + }, |
| 85 | + [] |
| 86 | + ); |
| 87 | + |
| 88 | + /** |
| 89 | + * Stop the current layout calculation |
| 90 | + */ |
| 91 | + const stopLayout = useCallback(() => { |
| 92 | + if (layoutRef.current) { |
| 93 | + layoutRef.current.stop(); |
| 94 | + isRunningRef.current = false; |
| 95 | + } |
| 96 | + }, []); |
| 97 | + |
| 98 | + /** |
| 99 | + * Kill the layout and release resources |
| 100 | + */ |
| 101 | + const killLayout = useCallback(() => { |
| 102 | + if (layoutRef.current) { |
| 103 | + layoutRef.current.kill(); |
| 104 | + layoutRef.current = null; |
| 105 | + isRunningRef.current = false; |
| 106 | + } |
| 107 | + }, []); |
| 108 | + |
| 109 | + /** |
| 110 | + * Check if layout is currently running |
| 111 | + */ |
| 112 | + const isRunning = useCallback(() => { |
| 113 | + return isRunningRef.current && layoutRef.current?.isRunning(); |
| 114 | + }, []); |
| 115 | + |
| 116 | + return { |
| 117 | + runLayout, |
| 118 | + stopLayout, |
| 119 | + killLayout, |
| 120 | + isRunning |
| 121 | + }; |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * Check if ForceAtlas2 worker is supported |
| 126 | + */ |
| 127 | +export function supportsFA2Worker(): boolean { |
| 128 | + return typeof Worker !== 'undefined'; |
| 129 | +} |
0 commit comments