Updates the ajax concurrency limit so that it's defined globally using a new OpenSeadragon.settings object.

This commit is contained in:
Chili Johnson 2023-03-30 11:36:04 -07:00
parent 537c2b21ad
commit 3b0857b545
2 changed files with 47 additions and 26 deletions

View File

@ -312,10 +312,6 @@
* it is set to 0 allowing the browser to make the maximum number of * it is set to 0 allowing the browser to make the maximum number of
* image requests in parallel as allowed by the browsers policy. * image requests in parallel as allowed by the browsers policy.
* *
* @property {Number} [ajaxLoaderLimit=0]
* The maximum number of tile source requests to make concurrently. By default
* it is set to 0 allowing an unlimited number of concurrent requests.
*
* @property {Number} [clickTimeThreshold=300] * @property {Number} [clickTimeThreshold=300]
* The number of milliseconds within which a pointer down-up event combination * The number of milliseconds within which a pointer down-up event combination
* will be treated as a click gesture. * will be treated as a click gesture.
@ -792,13 +788,6 @@ function OpenSeadragon( options ){
(function( $ ){ (function( $ ){
$.ajaxQueue = {
requestFuncs: [],
numRequests: 0,
numActiveRequests: 0,
maxConcurrency: 0,
};
/** /**
* The OpenSeadragon version. * The OpenSeadragon version.
* *
@ -1343,7 +1332,6 @@ function OpenSeadragon( options ){
//PERFORMANCE SETTINGS //PERFORMANCE SETTINGS
imageLoaderLimit: 0, imageLoaderLimit: 0,
ajaxLoaderLimit: 0,
maxImageCacheCount: 200, maxImageCacheCount: 200,
timeout: 30000, timeout: 30000,
useCanvas: true, // Use canvas element for drawing if available useCanvas: true, // Use canvas element for drawing if available
@ -1480,6 +1468,24 @@ function OpenSeadragon( options ){
ALWAYS: 2 ALWAYS: 2
}, },
/**
* Global settings, shared across all viewers.
* @static
* @type {Object}
* @property {Number} ajaxLoaderLimit The maximum number of ajax requests to make concurrently. By default it is set to 0 allowing an unlimited number of concurrent requests.
*/
settings: {
ajaxLoaderLimit: 0
},
setAjaxLoaderLimit: function ( maxNumAjaxRequests ) {
if( maxNumAjaxRequests < 0) {
throw new Error("The ajax loader limit must be >= 0");
}
$.settings.ajaxLoaderLimit = maxNumAjaxRequests;
},
/** /**
* Keep track of which {@link Viewer}s have been created. * Keep track of which {@link Viewer}s have been created.
* - Key: {@link Element} to which a Viewer is attached. * - Key: {@link Element} to which a Viewer is attached.
@ -2343,23 +2349,42 @@ function OpenSeadragon( options ){
return $.createAjaxRequest( local ); return $.createAjaxRequest( local );
}, },
/**
* An object holding the AJAX queue state.
* @static
* @type {Object}
* @property {Array} requestFuncs Array of functions representing queued AJAX requests
* @property {Array} numIncompleteRequests The number of incomplete AJAX requests, including queued and active (but not completed) requests
* @property {Array} numActiveRequests The number of AJAX requests currently in-flight
*/
AjaxQueue: {
requestFuncs: [],
numIncompleteRequests: 0,
numActiveRequests: 0,
},
/**
* Queues an AJAX request.
* @param {XMLHttpRequest} request The request to be queued
* @param {Function} sendRequestFunc A function which, when called, will send the request
*/
queueAjaxRequest: function (request, sendRequestFunc) { queueAjaxRequest: function (request, sendRequestFunc) {
if(!$.ajaxQueue.maxConcurrency) { if(!$.settings.ajaxLoaderLimit) {
sendRequestFunc(); sendRequestFunc();
} }
var oldOnStateChange = request.onreadystatechange; var oldOnStateChange = request.onreadystatechange;
var onCompleteRequest = function() { var onCompleteRequest = function() {
$.ajaxQueue.numRequests--; $.AjaxQueue.numIncompleteRequests--;
if ( if (
$.ajaxQueue.numActiveRequests === $.ajaxQueue.maxConcurrency && $.AjaxQueue.numActiveRequests === $.settings.ajaxLoaderLimit &&
$.ajaxQueue.requestFuncs.length > 0 $.AjaxQueue.requestFuncs.length > 0
) { ) {
$.ajaxQueue.requestFuncs.shift()(); $.AjaxQueue.requestFuncs.shift()();
} else { } else {
$.ajaxQueue.numActiveRequests--; $.AjaxQueue.numActiveRequests--;
} }
}; };
@ -2370,12 +2395,12 @@ function OpenSeadragon( options ){
} }
}; };
$.ajaxQueue.numRequests++; $.AjaxQueue.numIncompleteRequests++;
if ($.ajaxQueue.numActiveRequests === $.ajaxQueue.maxConcurrency) { if ($.AjaxQueue.numActiveRequests === $.settings.ajaxLoaderLimit) {
$.ajaxQueue.requestFuncs.push(sendRequestFunc); $.AjaxQueue.requestFuncs.push(sendRequestFunc);
} else { } else {
$.ajaxQueue.numActiveRequests++; $.AjaxQueue.numActiveRequests++;
sendRequestFunc(); sendRequestFunc();
} }
}, },

View File

@ -394,10 +394,6 @@ $.Viewer = function( options ) {
timeout: options.timeout timeout: options.timeout
}); });
// TODO: Instantiating a viewer shouldn't have
// a side effect on the global queue
$.ajaxQueue.ajaxLoaderLimit = options.ajaxLoaderLimit;
// Create the tile cache // Create the tile cache
this.tileCache = new $.TileCache({ this.tileCache = new $.TileCache({
maxImageCacheCount: this.maxImageCacheCount maxImageCacheCount: this.maxImageCacheCount