Skip to content

Commit

Permalink
Merge pull request #105 from cornerstonejs/prefetchingFix
Browse files Browse the repository at this point in the history
perf: ⚡️ On scroll, reset the prefetch queue
  • Loading branch information
JamesAPetts authored Sep 24, 2020
2 parents d3093d0 + a3e851e commit 832a82c
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/CornerstoneViewport/CornerstoneViewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down

0 comments on commit 832a82c

Please sign in to comment.