feat(app): polling to check graph initialization

This commit is contained in:
dextmorgn
2025-12-02 11:57:31 +01:00
parent b57df20b0b
commit d6eedbfbc3
2 changed files with 18 additions and 8 deletions

View File

@@ -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: () => { },

View File

@@ -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<GraphNode[]>((resolve, reject) => {
const worker = workerRef.current!