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

Ansynchronous HTTP 202 support + user cancellation #34

Open
wants to merge 3 commits into
base: terriajs
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Change Log
* Fixed a bug that could cause tiles to be missing from the globe surface, especially when starting with the camera zoomed close to the surface.
* Added support for refreshing expired tokens for `ArcGisMapServerImageryProvider` via callback registered with `options.requestNewToken` in constructor.
* Added support for `parameters` to `ArcGisMapServerImageryProvider`.
* Added Support for HTTP 202 headers, and cancelling them if the request object is marked as cancelled.

### 1.46 - 2018-06-01

Expand Down
30 changes: 28 additions & 2 deletions Source/Core/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -1260,10 +1260,11 @@ define([
var timeout = options.timeout;
var data = options.data;
var deferred = when.defer();
var xhr = Resource._Implementations.loadWithXhr(resource.url, responseType, method, data, headers, deferred, overrideMimeType, timeout);
var xhr = Resource._Implementations.loadWithXhr(resource.url, responseType, method, data, headers, deferred, overrideMimeType, timeout, request);
if (defined(xhr) && defined(xhr.abort)) {
request.cancelFunction = function() {
xhr.abort();
request.cancel();
};
}
return deferred.promise;
Expand Down Expand Up @@ -1893,7 +1894,7 @@ define([
}).end();
}

Resource._Implementations.loadWithXhr = function(url, responseType, method, data, headers, deferred, overrideMimeType, timeout) {
Resource._Implementations.loadWithXhr = function(url, responseType, method, data, headers, deferred, overrideMimeType, timeout, request) {
var dataUriRegexResult = dataUriRegex.exec(url);
if (dataUriRegexResult !== null) {
deferred.resolve(decodeDataUri(dataUriRegexResult, responseType));
Expand Down Expand Up @@ -1969,6 +1970,31 @@ define([
if (xhr.status === 204) {
// accept no content
deferred.resolve();
} else if (xhr.status === 202) {
//HTTP 202 is for a process that can take a while
//contains at minimum a location to get the
//completed resource from
//and the permission to read the location
var newURL = xhr.getResponseHeader('Location');
if (newURL === undefined || newURL === null || newURL === "") {
deferred.reject(new RuntimeError("HTTP 202 header with no Location.\n Contact the service owner directly about this misconfiguration.\n Using HTTP 202 responses requires 'Location' and 'Access-Control-Expose-Headers: Location' headers in the response"));
return;
}
//if request still valid change url and call this function
if (!request.cancelled) {
var newXHR;
setTimeout(function(){
if (!request.cancelled){
newXHR = Resource._Implementations.loadWithXhr.call(this,newURL, responseType, method, data, headers, deferred, overrideMimeType, timeout, request);
} else {
//user cancel request after waking from sleep
return;
}
},10000);
return newXHR
} else {
return;//request has been cancelled by user before sleep
}
} else if (defined(xhr.response) && (!defined(responseType) || (browserResponseType === responseType))) {
deferred.resolve(response);
} else if ((responseType === 'json') && typeof response === 'string') {
Expand Down