Skip to content

Commit

Permalink
feat(stackPrefetch): Added a configuration to allow for prefetching t…
Browse files Browse the repository at this point in the history
…o keep the existing pool (cornerstonejs#542)

* Added a configuration to allow for prefetching to keep the existing pool.

* Added the "preserveExistingPool" setting to the changelog.
  • Loading branch information
Saucistophe authored and swederik committed Sep 10, 2018
1 parent 747e5c2 commit 892efaa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Added a configuration setting on the stackPrefetch tool, called "preserveExistingPool", that prevents resetting the prefetch pool each time a new stack viewer is activated.

## [2.3.9] - 2018-07-24
### Changed
Expand Down
19 changes: 14 additions & 5 deletions src/stackTools/stackPrefetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const toolType = 'stackPrefetch';
const requestType = 'prefetch';

let configuration = {
maxImagesToPrefetch: Infinity
maxImagesToPrefetch: Infinity,
preserveExistingPool: false
};

let resetPrefetchTimeout;
Expand Down Expand Up @@ -127,8 +128,10 @@ function prefetch (element) {
return;
}

// Clear the requestPool of prefetch requests
requestPoolManager.clearRequestStack(requestType);
// Clear the requestPool of prefetch requests, if needed.
if (!configuration.preserveExistingPool) {
requestPoolManager.clearRequestStack(requestType);
}

// Identify the nearest imageIdIndex to the currentImageIdIndex
const nearest = nearestIndex(stackPrefetch.indicesToRequest, stack.currentImageIdIndex);
Expand Down Expand Up @@ -157,6 +160,7 @@ function prefetch (element) {
// Prefetch images around the current image (before and after)
let lowerIndex = nearest.low;
let higherIndex = nearest.high;
const imageIdsToPrefetch = [];

while (lowerIndex >= 0 || higherIndex < stackPrefetch.indicesToRequest.length) {
const currentIndex = stack.currentImageIdIndex;
Expand All @@ -173,17 +177,22 @@ function prefetch (element) {
if (shouldLoadLower) {
nextImageIdIndex = stackPrefetch.indicesToRequest[lowerIndex--];
imageId = stack.imageIds[nextImageIdIndex];
requestPoolManager.addRequest(element, imageId, requestType, preventCache, doneCallback, failCallback);
imageIdsToPrefetch.push(imageId);
}

if (shouldLoadHigher) {
nextImageIdIndex = stackPrefetch.indicesToRequest[higherIndex++];
imageId = stack.imageIds[nextImageIdIndex];
requestPoolManager.addRequest(element, imageId, requestType, preventCache, doneCallback, failCallback);
imageIdsToPrefetch.push(imageId);
}

}

// Load images in reverse order, by adding them at the beginning of the pool.
for(const imageToLoad of imageIdsToPrefetch.reverse()) {
requestPoolManager.addRequest(element, imageToLoad, requestType, preventCache, doneCallback, failCallback, true);
}

// Try to start the requestPool's grabbing procedure
// In case it isn't already running
requestPoolManager.startGrabbing();
Expand Down

0 comments on commit 892efaa

Please sign in to comment.