From d6eedbfbc33c027012d9eba67a83ae4f204a5319 Mon Sep 17 00:00:00 2001 From: dextmorgn Date: Tue, 2 Dec 2025 11:57:31 +0100 Subject: [PATCH] feat(app): polling to check graph initialization --- .../graph/use-graph-initialization.ts | 23 +++++++++++++++---- flowsint-app/src/hooks/use-layout.ts | 3 --- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/flowsint-app/src/components/sketches/graph/use-graph-initialization.ts b/flowsint-app/src/components/sketches/graph/use-graph-initialization.ts index fbee7f3..7a93c13 100644 --- a/flowsint-app/src/components/sketches/graph/use-graph-initialization.ts +++ b/flowsint-app/src/components/sketches/graph/use-graph-initialization.ts @@ -20,7 +20,6 @@ export const useGraphInitialization = ({ const isGraphReadyRef = useRef(false) const regenerateLayoutRef = useRef(regenerateLayout) - // Keep ref updated useEffect(() => { regenerateLayoutRef.current = regenerateLayout }, [regenerateLayout]) @@ -40,10 +39,8 @@ export const useGraphInitialization = ({ }, 100) return } - if (isGraphReadyRef.current) return isGraphReadyRef.current = true - // Initial zoom to fit setTimeout(() => { if (graphRef.current && typeof graphRef.current.zoomToFit === 'function') { @@ -106,12 +103,28 @@ export const useGraphInitialization = ({ [setActions, onGraphRef, instanceId, selectedNodeIdsRef, graphRef] ) + // Poll for graphRef to be ready useEffect(() => { - if (graphRef.current) { - initializeGraph(graphRef.current) + const checkAndInitialize = () => { + if (graphRef.current && !isGraphReadyRef.current) { + initializeGraph(graphRef.current) + } } + // Check immediately + checkAndInitialize() + + // Then check every 100ms until initialized + const interval = setInterval(() => { + if (isGraphReadyRef.current) { + clearInterval(interval) + } else { + checkAndInitialize() + } + }, 100) + return () => { + clearInterval(interval) if (!instanceId && isGraphReadyRef.current) { setActions({ zoomIn: () => { }, diff --git a/flowsint-app/src/hooks/use-layout.ts b/flowsint-app/src/hooks/use-layout.ts index f446152..5621f75 100644 --- a/flowsint-app/src/hooks/use-layout.ts +++ b/flowsint-app/src/hooks/use-layout.ts @@ -36,17 +36,14 @@ export function useLayout({ const applyLayout = useCallback( async ({ nodes, edges, layoutType }: LayoutOptions) => { - console.log(nodes) if (!workerRef.current) { throw new Error('Layout worker not initialized') } - // Remove fx and fy from all nodes to allow repositioning nodes.forEach((node: any) => { delete node.fx delete node.fy }) - return new Promise((resolve, reject) => { const worker = workerRef.current!