From add45567d8925968c4e9091fce82b29ca9e946f1 Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 27 Apr 2026 09:34:07 +0100 Subject: [PATCH] Enhance metadata caching in setLoadedImageIdsAndCacheMeta and add forced warm-up for visible stack viewports --- dicom-viewer/src/App.tsx | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/dicom-viewer/src/App.tsx b/dicom-viewer/src/App.tsx index fe576b3..449fac7 100644 --- a/dicom-viewer/src/App.tsx +++ b/dicom-viewer/src/App.tsx @@ -856,12 +856,13 @@ function App({ container_id, imageStacks, autoCacheStack, annotationJson, viewer }, [availableStacks, generateThumbnail, stackThumbnails]); // Wrap setLoadedImageIds to also cache DICOM metadata - const setLoadedImageIdsAndCacheMeta = async (imageIds: string[]) => { + const setLoadedImageIdsAndCacheMeta = async (imageIds: string[], options?: { force?: boolean }) => { console.log("Setting loaded imageIds:", imageIds); console.log("autoCacheStack:", autoCacheStack); const loadPromises: Promise[] = []; + const shouldCache = Boolean(autoCacheStack || options?.force); - if (autoCacheStack) { + if (shouldCache) { for (const imageId of imageIds) { if (imageId.startsWith("wadouri:")) { try { @@ -1171,6 +1172,19 @@ function App({ container_id, imageStacks, autoCacheStack, annotationJson, viewer const modifierActive = shiftPressed || (altEnablesReferenceCursors && altPressed); if (modifierActive) { + // Force metadata warm-up for visible stack viewports so nearest-slice lookup + // can consider the entire stack (including images not yet viewed/cached). + const visibleCount = viewportGrid.rows * viewportGrid.cols; + const visibleStackImageIds = viewportImageIds + .slice(0, visibleCount) + .filter((ids, idx) => viewportModes[idx] === 'stack' && Array.isArray(ids) && ids.length > 0); + + visibleStackImageIds.forEach((ids) => { + setLoadedImageIdsAndCacheMeta(ids, { force: true }).catch((err) => { + console.warn('Reference cursor metadata warm-up failed:', err); + }); + }); + if (!mainToolGroup.hasTool(ReferenceCursors.toolName)) { mainToolGroup.addTool(ReferenceCursors.toolName); } @@ -1181,7 +1195,16 @@ function App({ container_id, imageStacks, autoCacheStack, annotationJson, viewer mainToolGroup.setToolDisabled(ReferenceCursors.toolName); } } - }, [altPressed, shiftPressed, altEnablesReferenceCursors]); + }, [ + altPressed, + shiftPressed, + altEnablesReferenceCursors, + viewportGrid.rows, + viewportGrid.cols, + viewportImageIds, + viewportModes, + setLoadedImageIdsAndCacheMeta, + ]); // Helper to apply all mouse tool bindings (normal or ctrl) const applyMouseToolBindings = useCallback(() => {