Skip to content

Commit 832a82c

Browse files
authored
Merge pull request #105 from cornerstonejs/prefetchingFix
perf: ⚡️ On scroll, reset the prefetch queue
2 parents d3093d0 + a3e851e commit 832a82c

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

src/CornerstoneViewport/CornerstoneViewport.js

+79
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,85 @@ class CornerstoneViewport extends Component {
714714
this.setViewportActive();
715715

716716
scrollToIndex(this.element, value);
717+
718+
this.resetPrefetching(value);
719+
};
720+
721+
resetPrefetching = imageIdIndex => {
722+
cornerstoneTools.stackPrefetch.setConfiguration({
723+
maxImagesToPrefetch: Infinity,
724+
preserveExistingPool: false,
725+
maxSimultaneousRequests: 6,
726+
});
727+
728+
const { imageIds } = this.props;
729+
730+
const minImageIdIndex = 0;
731+
const maxImageIdIndex = imageIds.length - 1;
732+
733+
let lowerImageIdIndex = imageIdIndex;
734+
let upperImageIdIndex = imageIdIndex;
735+
736+
// Build up an array of images to prefetch, starting with the current image.
737+
let imageIdsToPrefetch = [imageIds[imageIdIndex]];
738+
739+
// 0: From current stack position down to minimum.
740+
// 1: From current stack position up to maximum.
741+
742+
const prefetchQueuedFilled = [false, false];
743+
744+
// Check if on edges and some criteria is already fulfilled
745+
746+
if (imageIdIndex === minImageIdIndex) {
747+
prefetchQueuedFilled[0] = true;
748+
} else if (imageIdIndex === maxImageIdIndex) {
749+
prefetchQueuedFilled[1] = true;
750+
}
751+
752+
while (
753+
prefetchQueuedFilled[0] === false ||
754+
prefetchQueuedFilled[1] === false
755+
) {
756+
if (prefetchQueuedFilled[0] === false) {
757+
// Add imageId bellow
758+
lowerImageIdIndex--;
759+
imageIdsToPrefetch.push(imageIds[lowerImageIdIndex]);
760+
761+
if (lowerImageIdIndex === minImageIdIndex) {
762+
prefetchQueuedFilled[0] = true;
763+
}
764+
}
765+
766+
if (prefetchQueuedFilled[1] === false) {
767+
// Add imageId above
768+
upperImageIdIndex++;
769+
imageIdsToPrefetch.push(imageIds[upperImageIdIndex]);
770+
771+
if (upperImageIdIndex === maxImageIdIndex) {
772+
prefetchQueuedFilled[1] = true;
773+
}
774+
}
775+
}
776+
777+
const requestPoolManager = cornerstoneTools.requestPoolManager;
778+
const requestType = 'prefetch';
779+
const preventCache = false;
780+
const noop = () => {};
781+
782+
requestPoolManager.clearRequestStack('prefetch');
783+
784+
imageIdsToPrefetch.forEach(imageId => {
785+
requestPoolManager.addRequest(
786+
{},
787+
imageId,
788+
requestType,
789+
preventCache,
790+
noop,
791+
noop
792+
);
793+
});
794+
795+
requestPoolManager.startGrabbing();
717796
};
718797

719798
setViewportActive = () => {

0 commit comments

Comments
 (0)