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

Commit

Permalink
fix(core): don't update taskCounts of setInterval
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion committed Feb 9, 2018
1 parent 42b9edb commit 4192893
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
7 changes: 5 additions & 2 deletions lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -846,8 +846,9 @@ const Zone: ZoneType = (function(global: any) {
this._zoneDelegate.handleError(this, err);
throw err;
}
if ((task as any as ZoneTask<any>)._zoneDelegates === zoneDelegates) {
if ((task as any as ZoneTask<any>)._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<any>, 1);
}
if ((task as any as ZoneTask<any>).state == scheduling) {
Expand Down Expand Up @@ -891,7 +892,9 @@ const Zone: ZoneType = (function(global: any) {
this._zoneDelegate.handleError(this, err);
throw err;
}
this._updateTaskCount(task as ZoneTask<any>, -1);
if (!(task.data && task.data.isPeriodic)) {
this._updateTaskCount(task as ZoneTask<any>, -1);
}
(task as ZoneTask<any>)._transitionTo(notScheduled, canceling);
task.runCount = 0;
return task;
Expand Down
19 changes: 9 additions & 10 deletions test/common/setInterval.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
});
});
});

});
9 changes: 1 addition & 8 deletions test/common/zone.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]);
});

Expand Down

0 comments on commit 4192893

Please sign in to comment.