Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix images not being aborted when dequeued #7655

Merged
merged 2 commits into from
Dec 14, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/util/ajax.js
Original file line number Diff line number Diff line change
@@ -190,9 +190,14 @@ resetImageRequestQueue();
export const getImage = function(requestParameters: RequestParameters, callback: Callback<HTMLImageElement>): Cancelable {
// limit concurrent image loads to help with raster sources performance on big screens
if (numImageRequests >= config.MAX_PARALLEL_IMAGE_REQUESTS) {
const queued = {requestParameters, callback, cancelled: false};
const queued = {
requestParameters,
callback,
cancelled: false,
cancel() { this.cancelled = true; }
};
imageQueue.push(queued);
return { cancel() { queued.cancelled = true; } };
return queued;
}
numImageRequests++;

@@ -203,9 +208,10 @@ export const getImage = function(requestParameters: RequestParameters, callback:
numImageRequests--;
assert(numImageRequests >= 0);
while (imageQueue.length && numImageRequests < config.MAX_PARALLEL_IMAGE_REQUESTS) { // eslint-disable-line
const {requestParameters, callback, cancelled} = imageQueue.shift();
const request = imageQueue.shift();
const {requestParameters, callback, cancelled} = request;
if (!cancelled) {
getImage(requestParameters, callback);
request.cancel = getImage(requestParameters, callback).cancel;
}
}
};
33 changes: 33 additions & 0 deletions test/unit/util/ajax.test.js
Original file line number Diff line number Diff line change
@@ -152,5 +152,38 @@ test('ajax', (t) => {
t.end();
});

t.test('getImage requests that were once queued are still abortable', (t) => {
resetImageRequestQueue();

const maxRequests = config.MAX_PARALLEL_IMAGE_REQUESTS;

const requests = [];
for (let i = 0; i < maxRequests; i++) {
requests.push(getImage({url: ''}, () => {}));
}

// the limit of allowed requests is reached
t.equals(window.server.requests.length, maxRequests);

const queuedURL = 'this-is-the-queued-request';
const queued = getImage({ url: queuedURL }, () => t.fail());

// the new requests is queued because the limit is reached
t.equals(window.server.requests.length, maxRequests);

// cancel the first request to let the queued request start
requests[0].cancel();
t.equals(window.server.requests.length, maxRequests + 1);

// abort the previously queued request and confirm that it is aborted
const queuedRequest = window.server.requests[window.server.requests.length - 1];
t.equals(queuedRequest.url, queuedURL);
t.equals(queuedRequest.aborted, undefined);
queued.cancel();
t.equals(queuedRequest.aborted, true);

t.end();
});

t.end();
});