diff --git a/lib/zone.ts b/lib/zone.ts index 9af7ed520..2af01a261 100644 --- a/lib/zone.ts +++ b/lib/zone.ts @@ -579,6 +579,9 @@ const Zone: ZoneType = (function(global) { const oldZone = _currentZone; _currentZone = this; try { + if (task.type == 'macroTask' && task.data && !task.data.isPeriodic) { + task.cancelFn = null; + } try { return this._zoneDelegate.invokeTask(this, task, applyThis, applyArgs); } catch (error) { @@ -587,9 +590,6 @@ const Zone: ZoneType = (function(global) { } } } finally { - if (task.type == 'macroTask' && task.data && !task.data.isPeriodic) { - task.cancelFn = null; - } _currentZone = oldZone; _currentTask = previousTask; } diff --git a/test/browser/setTimeout.spec.ts b/test/browser/setTimeout.spec.ts index 38e0a7cea..1d6e20c9a 100644 --- a/test/browser/setTimeout.spec.ts +++ b/test/browser/setTimeout.spec.ts @@ -31,23 +31,29 @@ describe('setTimeout', function () { }); it('should allow canceling of fns registered with setTimeout', function (done) { - var spy = jasmine.createSpy('spy'); - var cancelId = setTimeout(spy, 0); - clearTimeout(cancelId); - setTimeout(function () { - expect(spy).not.toHaveBeenCalled(); - done(); + var testZone = Zone.current.fork(Zone['wtfZoneSpec']).fork({ name: 'TestZone' }); + testZone.run(() => { + var spy = jasmine.createSpy('spy'); + var cancelId = setTimeout(spy, 0); + clearTimeout(cancelId); + setTimeout(function () { + expect(spy).not.toHaveBeenCalled(); + done(); + }); }); }); - 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(); + it('should allow cancelation of fns registered with setTimeout after invocation', function (done) { + var testZone = Zone.current.fork(Zone['wtfZoneSpec']).fork({ name: 'TestZone' }); + testZone.run(() => { + var spy = jasmine.createSpy('spy'); + var cancelId = setTimeout(spy, 0); setTimeout(function () { - clearTimeout(cancelId); - done(); + expect(spy).toHaveBeenCalled(); + setTimeout(function () { + clearTimeout(cancelId); + done(); + }); }); }); }); @@ -59,6 +65,16 @@ describe('setTimeout', function () { }, 0); }); + it('should allow cancelation of fns registered with setTimeout during invocation', function (done) { + var testZone = Zone.current.fork(Zone['wtfZoneSpec']).fork({ name: 'TestZone' }); + testZone.run(() => { + var cancelId = setTimeout(function() { + clearTimeout(cancelId); + done(); + }, 0); + }); + }); + it('should pass invalid values through', function () { clearTimeout(null); clearTimeout({});