Adds a hacky AJAX concurrency limiter when initially loading tile sources.

Based on this gist: https://gist.github.com/Terrance/158a4c436baa64c4324803467844b00f
This commit is contained in:
Chili Johnson 2022-11-22 13:28:28 -08:00
parent ed66c2d151
commit bd51c161c2

View File

@ -788,6 +788,12 @@ function OpenSeadragon( options ){
(function( $ ){ (function( $ ){
$.ajaxQueue = {
requestFuncs: [],
numRequests: 0,
numActiveRequests: 0,
maxConcurrency: 50,
};
/** /**
* The OpenSeadragon version. * The OpenSeadragon version.
@ -2332,6 +2338,70 @@ function OpenSeadragon( options ){
return $.createAjaxRequest( local ); return $.createAjaxRequest( local );
}, },
queueAjaxRequest: function (
request,
method,
url,
headers,
withCredentials,
responseType,
postData
) {
var oldOnStateChange = request.onreadystatechange;
var sendRequest = function () {
request.open( method, url, true );
if (responseType) {
request.responseType = responseType;
}
if (headers) {
for (var headerName in headers) {
if (Object.prototype.hasOwnProperty.call(headers, headerName) && headers[headerName]) {
request.setRequestHeader(headerName, headers[headerName]);
}
}
}
if (withCredentials) {
request.withCredentials = true;
}
request.send(postData);
};
var callback = function() {
$.ajaxQueue.numRequests--;
if (
$.ajaxQueue.numActiveRequests === $.ajaxQueue.maxConcurrency &&
$.ajaxQueue.requestFuncs.length > 0
) {
$.ajaxQueue.requestFuncs.shift()();
} else {
$.ajaxQueue.numActiveRequests--;
}
};
request.onreadystatechange = function() {
if ( request.readyState === 4 ) {
callback();
oldOnStateChange();
}
};
$.ajaxQueue.numRequests++;
if ($.ajaxQueue.numActiveRequests === $.ajaxQueue.maxConcurrency) {
$.ajaxQueue.requestFuncs.push(sendRequest);
} else {
$.ajaxQueue.numActiveRequests++;
sendRequest();
}
},
/** /**
* Makes an AJAX request. * Makes an AJAX request.
* @param {Object} options * @param {Object} options
@ -2395,25 +2465,15 @@ function OpenSeadragon( options ){
var method = postData ? "POST" : "GET"; var method = postData ? "POST" : "GET";
try { try {
request.open( method, url, true ); $.queueAjaxRequest(
request,
if (responseType) { method,
request.responseType = responseType; url,
} headers,
withCredentials,
if (headers) { responseType,
for (var headerName in headers) { postData
if (Object.prototype.hasOwnProperty.call(headers, headerName) && headers[headerName]) { );
request.setRequestHeader(headerName, headers[headerName]);
}
}
}
if (withCredentials) {
request.withCredentials = true;
}
request.send(postData);
} catch (e) { } catch (e) {
$.console.error( "%s while making AJAX request: %s", e.name, e.message ); $.console.error( "%s while making AJAX request: %s", e.name, e.message );