diff --git a/src/index.js b/src/index.js index f865097dc..6e8b961b0 100644 --- a/src/index.js +++ b/src/index.js @@ -203,8 +203,6 @@ import { default as stackImageIndexSynchronizer } from './synchronization/stackI import { default as panZoomSynchronizer } from './synchronization/panZoomSynchronizer.js'; // ~~~~~~ REQUEST POOL MANAGER ~~~~~ // -import { default as requestPoolManager } from './requestPool/requestPoolManager.js'; - import { default as external } from './externalModules.js'; import { default as EVENTS } from './events.js'; import { default as version } from './version.js'; @@ -313,7 +311,6 @@ const cornerstoneTools = { stackImagePositionOffsetSynchronizer, stackImageIndexSynchronizer, panZoomSynchronizer, - requestPoolManager, external, EVENTS, version, @@ -420,7 +417,6 @@ export { stackImagePositionOffsetSynchronizer, stackImageIndexSynchronizer, panZoomSynchronizer, - requestPoolManager, importInternal, external, EVENTS, diff --git a/src/lib.js b/src/lib.js index 3ee421867..a5ca68a45 100644 --- a/src/lib.js +++ b/src/lib.js @@ -75,8 +75,6 @@ import pointInsideBoundingBox from './util/pointInsideBoundingBox.js'; import makeUnselectable from './util/makeUnselectable.js'; import getRGBPixels from './util/getRGBPixels.js'; import { - getDefaultSimultaneousRequests, - getMaxSimultaneousRequests, getBrowserInfo, isMobileDevice, } from './util/getMaxSimultaneousRequests.js'; @@ -147,8 +145,6 @@ export const lib = { 'util/pointInsideBoundingBox': pointInsideBoundingBox, 'util/makeUnselectable': makeUnselectable, 'util/getRGBPixels': getRGBPixels, - 'util/getDefaultSimultaneousRequests': getDefaultSimultaneousRequests, - 'util/getMaxSimultaneousRequests': getMaxSimultaneousRequests, 'util/getBrowserInfo': getBrowserInfo, 'util/isMobileDevice': isMobileDevice, 'util/angleBetweenPoints': angleBetweenPoints, diff --git a/src/requestPool/requestPoolManager.js b/src/requestPool/requestPoolManager.js deleted file mode 100644 index 5323ea5a9..000000000 --- a/src/requestPool/requestPoolManager.js +++ /dev/null @@ -1,250 +0,0 @@ -import external from '../externalModules.js'; -import { getMaxSimultaneousRequests } from '../util/getMaxSimultaneousRequests.js'; - -const requestPool = { - interaction: [], - thumbnail: [], - prefetch: [], -}; - -const numRequests = { - interaction: 0, - thumbnail: 0, - prefetch: 0, -}; - -let maxNumRequests = { - interaction: 6, - thumbnail: 6, - prefetch: 5, -}; - -let awake = false; -const grabDelay = 20; - -function addRequest( - element, - imageId, - type, - preventCache, - doneCallback, - failCallback, - addToBeginning, - options = {} -) { - if (!requestPool.hasOwnProperty(type)) { - throw new Error( - 'Request type must be one of interaction, thumbnail, or prefetch' - ); - } - - if (!element || !imageId) { - return; - } - - // Describe the request - const requestDetails = { - type, - imageId, - preventCache, - doneCallback, - failCallback, - options, - }; - - // If this imageId is in the cache, resolve it immediately - const imageLoadObject = external.cornerstone.imageCache.getImageLoadObject( - imageId - ); - - if (imageLoadObject) { - imageLoadObject.promise.then( - function(image) { - doneCallback(image); - }, - function(error) { - failCallback(error); - } - ); - - return; - } - - if (addToBeginning) { - // Add it to the beginning of the stack - requestPool[type].unshift(requestDetails); - } else { - // Add it to the end of the stack - requestPool[type].push(requestDetails); - } - - // Wake up - awake = true; -} - -function clearRequestStack(type) { - // Console.log('clearRequestStack'); - if (!requestPool.hasOwnProperty(type)) { - throw new Error( - 'Request type must be one of interaction, thumbnail, or prefetch' - ); - } - - requestPool[type] = []; -} - -function startAgain() { - if (!awake) { - return; - } - - setTimeout(function() { - startGrabbing(); - }, grabDelay); -} - -function sendRequest(requestDetails) { - const cornerstone = external.cornerstone; - // Increment the number of current requests of this type - const type = requestDetails.type; - - numRequests[type]++; - - awake = true; - const imageId = requestDetails.imageId; - const doneCallback = requestDetails.doneCallback; - const failCallback = requestDetails.failCallback; - - // Check if we already have this image promise in the cache - const imageLoadObject = cornerstone.imageCache.getImageLoadObject(imageId); - - if (imageLoadObject) { - // If we do, remove from list (when resolved, as we could have - // Pending prefetch requests) and stop processing this iteration - imageLoadObject.promise.then( - function(image) { - numRequests[type]--; - // Console.log(numRequests); - - doneCallback(image); - startAgain(); - }, - function(error) { - numRequests[type]--; - // Console.log(numRequests); - failCallback(error); - startAgain(); - } - ); - - return; - } - - function requestTypeToLoadPriority(requestDetails) { - if (requestDetails.type === 'prefetch') { - return -5; - } else if (requestDetails.type === 'interactive') { - return 0; - } else if (requestDetails.type === 'thumbnail') { - return 5; - } - } - - const priority = requestTypeToLoadPriority(requestDetails); - - const options = Object.assign({}, requestDetails.options, { - priority, - type: requestDetails.type, - }); - - let loader; - - if (requestDetails.preventCache === true) { - loader = cornerstone.loadImage(imageId, options); - } else { - loader = cornerstone.loadAndCacheImage(imageId, options); - } - - // Load and cache the image - loader.then( - function(image) { - numRequests[type]--; - // Console.log(numRequests); - doneCallback(image); - startAgain(); - }, - function(error) { - numRequests[type]--; - // Console.log(numRequests); - failCallback(error); - startAgain(); - } - ); -} - -function startGrabbing() { - // Begin by grabbing X images - const maxSimultaneousRequests = getMaxSimultaneousRequests(); - - maxNumRequests = { - interaction: Math.max(maxSimultaneousRequests, 1), - thumbnail: Math.max(maxSimultaneousRequests - 2, 1), - prefetch: Math.max(maxSimultaneousRequests - 1, 1), - }; - - const currentRequests = - numRequests.interaction + numRequests.thumbnail + numRequests.prefetch; - const requestsToSend = maxSimultaneousRequests - currentRequests; - - for (let i = 0; i < requestsToSend; i++) { - const requestDetails = getNextRequest(); - - if (requestDetails) { - sendRequest(requestDetails); - } - } -} - -function getNextRequest() { - if ( - requestPool.interaction.length && - numRequests.interaction < maxNumRequests.interaction - ) { - return requestPool.interaction.shift(); - } - - if ( - requestPool.thumbnail.length && - numRequests.thumbnail < maxNumRequests.thumbnail - ) { - return requestPool.thumbnail.shift(); - } - - if ( - requestPool.prefetch.length && - numRequests.prefetch < maxNumRequests.prefetch - ) { - return requestPool.prefetch.shift(); - } - - if ( - !requestPool.interaction.length && - !requestPool.thumbnail.length && - !requestPool.prefetch.length - ) { - awake = false; - } - - return false; -} - -function getRequestPool() { - return requestPool; -} - -export default { - addRequest, - clearRequestStack, - startGrabbing, - getRequestPool, -}; diff --git a/src/requestPool/requestPoolManager.test.js b/src/requestPool/requestPoolManager.test.js deleted file mode 100644 index 9c4ccb9ad..000000000 --- a/src/requestPool/requestPoolManager.test.js +++ /dev/null @@ -1,50 +0,0 @@ -// SUT -import requestPoolManager from './requestPoolManager.js'; - -jest.mock('./../externalModules.js'); - -describe('clearRequestStack with an invalid type', function() { - it('should throw an error when attempting to clear the request stack with an invalid type', () => { - const nonExistantType = 'NotAnExistingType'; - - expect(() => requestPoolManager.clearRequestStack(nonExistantType)).toThrow( - 'Request type must be one of interaction, thumbnail, or prefetch' - ); - }); -}); - -describe('addRequest', function() { - it('should add the requests to the interaction stack, in the correct order', function() { - requestPoolManager.addRequest( - {}, - 'request1', - 'interaction', - false, - () => {}, // DoneCallback - () => {} // FailCallback - ); - requestPoolManager.addRequest( - {}, - 'request2', - 'interaction', - false, - () => {}, // DoneCallback - () => {} // FailCallback - ); - requestPoolManager.addRequest( - {}, - 'request3', - 'interaction', - false, - () => {}, // DoneCallback - () => {}, // FailCallback - true - ); - - const interactionStack = requestPoolManager.getRequestPool().interaction; - - expect(interactionStack[0].imageId).toEqual('request3'); - expect(interactionStack[1].imageId).toEqual('request1'); - expect(interactionStack[2].imageId).toEqual('request2'); - }); -}); diff --git a/src/stackTools/stackPrefetch.js b/src/stackTools/stackPrefetch.js index c74a91acc..f20736590 100755 --- a/src/stackTools/stackPrefetch.js +++ b/src/stackTools/stackPrefetch.js @@ -1,8 +1,6 @@ import external from './../externalModules.js'; -import requestPoolManager from '../requestPool/requestPoolManager.js'; import loadHandlerManager from '../stateManagement/loadHandlerManager.js'; import { addToolState, getToolState } from '../stateManagement/toolState.js'; -import { setMaxSimultaneousRequests } from '../util/getMaxSimultaneousRequests.js'; import { getLogger } from '../util/logger.js'; import triggerEvent from '../util/triggerEvent'; import EVENTS from '../events.js'; @@ -140,7 +138,7 @@ function prefetch(element) { // Clear the requestPool of prefetch requests, if needed. if (!configuration.preserveExistingPool) { - requestPoolManager.clearRequestStack(requestType); + external.cornerstone.imageLoadPoolManager.clearRequestStack(requestType); } // Identify the nearest imageIdIndex to the currentImageIdIndex @@ -233,20 +231,16 @@ function prefetch(element) { } // 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 - ); + if (preventCache) { + external.cornerstone + .loadImage(imageToLoad, { priority: 0, requestType }) + .then(doneCallback, failCallback); + } else { + external.cornerstone + .loadAndCacheImage(imageToLoad, { priority: 0, requestType }) + .then(doneCallback, failCallback); + } } - - // Try to start the requestPool's grabbing procedure - // In case it isn't already running - requestPoolManager.startGrabbing(); } function getPromiseRemovedHandler(element) { @@ -393,7 +387,7 @@ function disable(element) { stackPrefetchData.data[0].enabled = false; // Clear current prefetch requests from the requestPool - requestPoolManager.clearRequestStack(requestType); + external.cornerstone.imageLoadPoolManager.clearRequestStack(requestType); } } @@ -403,10 +397,6 @@ function getConfiguration() { function setConfiguration(config) { configuration = config; - - if (config.maxSimultaneousRequests) { - setMaxSimultaneousRequests(config.maxSimultaneousRequests); - } } // Module/private exports diff --git a/src/util/scrollToIndex.js b/src/util/scrollToIndex.js index 44286fcff..170b321bd 100644 --- a/src/util/scrollToIndex.js +++ b/src/util/scrollToIndex.js @@ -1,7 +1,6 @@ import EVENTS from '../events.js'; import external from '../externalModules.js'; import { getToolState } from '../stateManagement/toolState.js'; -import requestPoolManager from '../requestPool/requestPoolManager.js'; import loadHandlerManager from '../stateManagement/loadHandlerManager.js'; import triggerEvent from '../util/triggerEvent.js'; @@ -126,8 +125,6 @@ export default function(element, newImageIdIndex) { } imagePromise.then(doneCallback, failCallback); - // Make sure we kick off any changed download request pools - requestPoolManager.startGrabbing(); triggerEvent(element, EVENTS.STACK_SCROLL, eventData); }