From 7eb10fda82b532dd3cc7186e6ec799d0730b2724 Mon Sep 17 00:00:00 2001 From: "JiaLi.Passion" Date: Fri, 30 Dec 2016 01:38:31 +0900 Subject: [PATCH] fix #569, request will cause updateTaskCount failed if we call abort multipletimes the fix is check whether the xhr has been aborted before abort the request --- lib/browser/browser.ts | 5 ++++- test/browser/XMLHttpRequest.spec.ts | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/browser/browser.ts b/lib/browser/browser.ts index a45db6bde..8c92a4aac 100644 --- a/lib/browser/browser.ts +++ b/lib/browser/browser.ts @@ -125,7 +125,10 @@ function patchXHR(window: any) { const task: Task = findPendingTask(self); if (task && typeof task.type == 'string') { // If the XHR has already completed, do nothing. - if (task.cancelFn == null) { + // If the XHR has already been aborted, do nothing. + // Fix #569, call abort multiple times before done will cause + // macroTask task count be negative number + if (task.cancelFn == null || (task.data && (task.data).aborted)) { return; } task.zone.cancelTask(task); diff --git a/test/browser/XMLHttpRequest.spec.ts b/test/browser/XMLHttpRequest.spec.ts index 2de91e99c..6a9743b0d 100644 --- a/test/browser/XMLHttpRequest.spec.ts +++ b/test/browser/XMLHttpRequest.spec.ts @@ -195,4 +195,18 @@ describe('XMLHttpRequest', function() { }; }); }); + + it('should keep taskcount correctly when abort was called multiple times before request is done', + function() { + testZone.run(function() { + const req = new XMLHttpRequest(); + req.open('get', '/', true); + req.send(); + req.addEventListener('readystatechange', function(ev) { + if (req.readyState >= 2) { + req.abort(); + } + }); + }); + }); });