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

Commit c70f011

Browse files
juliemrmhevery
authored andcommittedApr 7, 2016
fix(xhr): XHR macrotasks allow abort after XHR has completed (#311)
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).
1 parent 1929c0d commit c70f011

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed
 

‎lib/browser/browser.ts

+4
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ function patchXHR(window: any) {
9595
var clearNative = patchMethod(window.XMLHttpRequest.prototype, 'abort', (delegate: Function) => function(self: any, args: any[]) {
9696
var task: Task = findPendingTask(self);
9797
if (task && typeof task.type == 'string') {
98+
// If the XHR has already completed, do nothing.
99+
if (task.cancelFn == null) {
100+
return;
101+
}
98102
task.zone.cancelTask(task);
99103
}
100104
// Otherwise, we are trying to abort an XHR which has not yet been sent, so there is no task to cancel. Do nothing.

‎test/browser/XMLHttpRequest.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,27 @@ describe('XMLHttpRequest', function () {
107107
done();
108108
}, 0);
109109
});
110+
111+
it('should allow aborting an XMLHttpRequest after its completed', function(done) {
112+
var req;
113+
114+
testZone.run(function() {
115+
req = new XMLHttpRequest();
116+
req.onreadystatechange = function() {
117+
if (req.readyState === XMLHttpRequest.DONE) {
118+
if (req.status !== 0) {
119+
setTimeout(function() {
120+
req.abort();
121+
done();
122+
}, 0);
123+
}
124+
}
125+
};
126+
req.open('get', '/', true);
127+
128+
req.send();
129+
});
130+
});
110131
}));
111132

112133
it('should preserve other setters', function () {

0 commit comments

Comments
 (0)
This repository has been archived.