diff --git a/src/CornerstoneViewport/CornerstoneViewport.js b/src/CornerstoneViewport/CornerstoneViewport.js index d6c8897..0027527 100644 --- a/src/CornerstoneViewport/CornerstoneViewport.js +++ b/src/CornerstoneViewport/CornerstoneViewport.js @@ -714,6 +714,85 @@ class CornerstoneViewport extends Component { this.setViewportActive(); scrollToIndex(this.element, value); + + this.resetPrefetching(value); + }; + + resetPrefetching = imageIdIndex => { + cornerstoneTools.stackPrefetch.setConfiguration({ + maxImagesToPrefetch: Infinity, + preserveExistingPool: false, + maxSimultaneousRequests: 6, + }); + + const { imageIds } = this.props; + + const minImageIdIndex = 0; + const maxImageIdIndex = imageIds.length - 1; + + let lowerImageIdIndex = imageIdIndex; + let upperImageIdIndex = imageIdIndex; + + // Build up an array of images to prefetch, starting with the current image. + let imageIdsToPrefetch = [imageIds[imageIdIndex]]; + + // 0: From current stack position down to minimum. + // 1: From current stack position up to maximum. + + const prefetchQueuedFilled = [false, false]; + + // Check if on edges and some criteria is already fulfilled + + if (imageIdIndex === minImageIdIndex) { + prefetchQueuedFilled[0] = true; + } else if (imageIdIndex === maxImageIdIndex) { + prefetchQueuedFilled[1] = true; + } + + while ( + prefetchQueuedFilled[0] === false || + prefetchQueuedFilled[1] === false + ) { + if (prefetchQueuedFilled[0] === false) { + // Add imageId bellow + lowerImageIdIndex--; + imageIdsToPrefetch.push(imageIds[lowerImageIdIndex]); + + if (lowerImageIdIndex === minImageIdIndex) { + prefetchQueuedFilled[0] = true; + } + } + + if (prefetchQueuedFilled[1] === false) { + // Add imageId above + upperImageIdIndex++; + imageIdsToPrefetch.push(imageIds[upperImageIdIndex]); + + if (upperImageIdIndex === maxImageIdIndex) { + prefetchQueuedFilled[1] = true; + } + } + } + + const requestPoolManager = cornerstoneTools.requestPoolManager; + const requestType = 'prefetch'; + const preventCache = false; + const noop = () => {}; + + requestPoolManager.clearRequestStack('prefetch'); + + imageIdsToPrefetch.forEach(imageId => { + requestPoolManager.addRequest( + {}, + imageId, + requestType, + preventCache, + noop, + noop + ); + }); + + requestPoolManager.startGrabbing(); }; setViewportActive = () => {