From c5a58da6a365537ca03c5a5b81b6b1b08311ac74 Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 23 Mar 2026 11:08:21 +0000 Subject: [PATCH] Refactor annotation import handling to improve race condition management and type safety --- dicom-viewer/src/App.tsx | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/dicom-viewer/src/App.tsx b/dicom-viewer/src/App.tsx index 51dae38..d4ccc0d 100644 --- a/dicom-viewer/src/App.tsx +++ b/dicom-viewer/src/App.tsx @@ -2547,18 +2547,16 @@ function App({ container_id, imageStacks, autoCacheStack, annotationJson, viewer // Import and restore all annotations using cornerstoneTools.annotation.state.restoreAnnotations() // Add defensive guards and queueing so callers can call importAnnotations before the // viewer/annotation state is fully initialized in rare race conditions. - // @ts-expect-error window augmentation - window.__pendingImportAnnotations = window.__pendingImportAnnotations || {}; + (window as any).__pendingImportAnnotations = (window as any).__pendingImportAnnotations || {}; const flushPending = (key: string) => { try { - // @ts-expect-error global - const pending = window.__pendingImportAnnotations && window.__pendingImportAnnotations[key]; + const pending = (window as any).__pendingImportAnnotations && (window as any).__pendingImportAnnotations[key]; if (!pending || !pending.length) return; const annotationState = cornerstoneTools?.annotation?.state; if (!annotationState || typeof annotationState.addAnnotation !== 'function') return; // Drain the queue - while ((window.__pendingImportAnnotations[key] || []).length) { - const annotationData = (window.__pendingImportAnnotations[key] || []).shift(); + while (((window as any).__pendingImportAnnotations[key] || []).length) { + const annotationData = ((window as any).__pendingImportAnnotations[key] || []).shift(); if (!annotationData) continue; if (Array.isArray(annotationData)) { annotationData.forEach(ann => { @@ -2572,16 +2570,14 @@ function App({ container_id, imageStacks, autoCacheStack, annotationJson, viewer } }; - // @ts-expect-error window augmentation window[`importAnnotations_${apiKey}`] = (json: string) => { try { const annotationData = typeof json === "string" ? JSON.parse(json) : json; const annotationState = cornerstoneTools?.annotation?.state; if (!annotationState || typeof annotationState.addAnnotation !== 'function') { // Queue the annotations until the annotation state is ready - // @ts-expect-error window augmentation - window.__pendingImportAnnotations[apiKey] = window.__pendingImportAnnotations[apiKey] || []; - window.__pendingImportAnnotations[apiKey].push(annotationData); + (window as any).__pendingImportAnnotations[apiKey] = (window as any).__pendingImportAnnotations[apiKey] || []; + (window as any).__pendingImportAnnotations[apiKey].push(annotationData); console.warn('importAnnotations: annotation state not ready, queued import', apiKey); // schedule a flush attempt setTimeout(() => flushPending(apiKey), 500);