diff --git a/lib/zone.ts b/lib/zone.ts index 76efc031a..cf4cd79ba 100644 --- a/lib/zone.ts +++ b/lib/zone.ts @@ -846,8 +846,9 @@ const Zone: ZoneType = (function(global: any) { this._zoneDelegate.handleError(this, err); throw err; } - if ((task as any as ZoneTask)._zoneDelegates === zoneDelegates) { + if ((task as any as ZoneTask)._zoneDelegates === zoneDelegates && !(task.data && task.data.isPeriodic)) { // we have to check because internally the delegate can reschedule the task. + // and we don't need to updateTaskCount for periodic task such as setInterval this._updateTaskCount(task as any as ZoneTask, 1); } if ((task as any as ZoneTask).state == scheduling) { @@ -891,7 +892,9 @@ const Zone: ZoneType = (function(global: any) { this._zoneDelegate.handleError(this, err); throw err; } - this._updateTaskCount(task as ZoneTask, -1); + if (!(task.data && task.data.isPeriodic)) { + this._updateTaskCount(task as ZoneTask, -1); + } (task as ZoneTask)._transitionTo(notScheduled, canceling); task.runCount = 0; return task; diff --git a/test/common/setInterval.spec.ts b/test/common/setInterval.spec.ts index 42aaac7b0..c1e868bdc 100644 --- a/test/common/setInterval.spec.ts +++ b/test/common/setInterval.spec.ts @@ -60,10 +60,15 @@ describe('setInterval', function() { }, null, null, 'unit-test'); }); - it('should not cancel the task after invoke the setInterval callback', (done) => { + it('setInterval should not update task count to trigger onHasTask', (done: Function) => { const logs: HasTaskState[] = []; + let task: Task; const zone = Zone.current.fork({ name: 'interval', + onScheduleTask: (delegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, t: Task) => { + task = t; + return delegate.scheduleTask(targetZone, task); + }, onHasTask: (delegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, hasTask: HasTaskState) => { logs.push(hasTask); @@ -74,17 +79,11 @@ describe('setInterval', function() { zone.run(() => { const timerId = setInterval(() => {}, 100); (global as any)[Zone.__symbol__('setTimeout')](() => { - expect(logs.length > 0).toBeTruthy(); - expect(logs).toEqual( - [{microTask: false, macroTask: true, eventTask: false, change: 'macroTask'}]); + expect(logs).toEqual([]); clearInterval(timerId); - expect(logs).toEqual([ - {microTask: false, macroTask: true, eventTask: false, change: 'macroTask'}, - {microTask: false, macroTask: false, eventTask: false, change: 'macroTask'} - ]); + expect(logs).toEqual([]); done(); }, 300); - }); + }); }); - }); diff --git a/test/common/zone.spec.ts b/test/common/zone.spec.ts index f969df913..df40ae193 100644 --- a/test/common/zone.spec.ts +++ b/test/common/zone.spec.ts @@ -183,14 +183,7 @@ describe('Zone', function() { zone.cancelTask(task); }); expect(log).toEqual([ - {microTask: false, macroTask: true, eventTask: false, change: 'macroTask', zone: 'parent'}, - 'macroTask', 'macroTask', { - microTask: false, - macroTask: false, - eventTask: false, - change: 'macroTask', - zone: 'parent' - } + 'macroTask', 'macroTask' ]); });