diff --git a/lib/browser/browser.ts b/lib/browser/browser.ts index 39013f28b..6a24bc120 100644 --- a/lib/browser/browser.ts +++ b/lib/browser/browser.ts @@ -86,7 +86,10 @@ function patchTimer( var clearNative = patchMethod(window, cancelName, (delegate: Function) => function(self: any, args: any[]) { var task: Task = args[0]; if (task && typeof task.type == 'string') { - task.zone.cancelTask(task); + if (task.cancelFn) { + // Do not cancel already canceled functions + task.zone.cancelTask(task); + } } else { // cause an error by calling it directly. delegate.apply(window, args); diff --git a/lib/zone.ts b/lib/zone.ts index 76090c21d..2fce60274 100644 --- a/lib/zone.ts +++ b/lib/zone.ts @@ -581,11 +581,11 @@ var Zone: ZoneType = (function(global) { } } } finally { + if (task.type == 'macroTask' && task.data && !task.data.isPeriodic) { + task.cancelFn = null; + } _currentZone = oldZone; _currentTask = previousTask; - if (task.type == 'microTask') { - - } } } @@ -761,7 +761,6 @@ var Zone: ZoneType = (function(global) { var prev = counts[type]; var next = counts[type] = prev + count; if (next < 0) { - debugger; throw new Error('More tasks executed then were scheduled.'); } if (prev == 0 || next == 0) { diff --git a/test/browser/setTimeout.spec.ts b/test/browser/setTimeout.spec.ts index e40910b54..be5a494cc 100644 --- a/test/browser/setTimeout.spec.ts +++ b/test/browser/setTimeout.spec.ts @@ -40,6 +40,18 @@ describe('setTimeout', function () { }); }); + it('should allow double cancelation of fns registered with setTimeout', function (done) { + var spy = jasmine.createSpy('spy'); + var cancelId = setTimeout(spy, 0); + setTimeout(function () { + expect(spy).toHaveBeenCalled(); + setTimeout(function () { + clearTimeout(cancelId); + done(); + }); + }); + }); + it('should pass invalid values through', function () { clearTimeout(null); clearTimeout({});