Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix(xhr): XHR macrotasks allow abort after XHR has completed (#311)
Browse files Browse the repository at this point in the history
Avoid throwing an error if an XHR is aborted after it has already

been sent and completed (this throws no error in the browser,

and zones should not introduce a new error).
  • Loading branch information
juliemr authored and mhevery committed Apr 7, 2016
1 parent 1929c0d commit c70f011
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/browser/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ function patchXHR(window: any) {
var clearNative = patchMethod(window.XMLHttpRequest.prototype, 'abort', (delegate: Function) => function(self: any, args: any[]) {
var task: Task = findPendingTask(self);
if (task && typeof task.type == 'string') {
// If the XHR has already completed, do nothing.
if (task.cancelFn == null) {
return;
}
task.zone.cancelTask(task);
}
// Otherwise, we are trying to abort an XHR which has not yet been sent, so there is no task to cancel. Do nothing.
Expand Down
21 changes: 21 additions & 0 deletions test/browser/XMLHttpRequest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,27 @@ describe('XMLHttpRequest', function () {
done();
}, 0);
});

it('should allow aborting an XMLHttpRequest after its completed', function(done) {
var req;

testZone.run(function() {
req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (req.readyState === XMLHttpRequest.DONE) {
if (req.status !== 0) {
setTimeout(function() {
req.abort();
done();
}, 0);
}
}
};
req.open('get', '/', true);

req.send();
});
});
}));

it('should preserve other setters', function () {
Expand Down

0 comments on commit c70f011

Please sign in to comment.