Skip to content

Commit

Permalink
fix(zone.js): in TaskTrackingZoneSpec track a periodic task until it …
Browse files Browse the repository at this point in the history
…is cancelled

Before this change, the macrotask for `setInterval(callback, ms)` was no
longer tracked by `TaskTrackingZoneSpec` after the `callback` was
invoked for the first time. Now the periodic macrotask is tracked until
it is cancelled, e.g. `clearInterval(id)`.

BREAKING CHANGE: in TaskTrackingZoneSpec track a periodic task until it is cancelled

The breaking change is scoped only to the plugin
`zone.js/plugins/task-tracking`. If you used `TaskTrackingZoneSpec` and
checked the pending macroTasks e.g. using `(this.ngZone as any)._inner
._parent._properties.TaskTrackingZone.getTasksFor('macroTask')`, then
its behavior slightly changed for periodic macrotasks. For example,
previously the `setInterval` macrotask was no longer tracked after its
callback was executed for the first time. Now it's tracked until
the task is explicitly cancelled, e.g  with `clearInterval(id)`.

fixes 45350
  • Loading branch information
Platonn committed Mar 19, 2022
1 parent a049840 commit 6766dd1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/zone.js/lib/zone-spec/task-tracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class TaskTrackingZoneSpec implements ZoneSpec {
onInvokeTask(
parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task,
applyThis: any, applyArgs: any): any {
if (task.type === 'eventTask')
if (task.type === 'eventTask' || task.data?.isPeriodic)
return parentZoneDelegate.invokeTask(targetZone, task, applyThis, applyArgs);
const tasks = this.getTasksFor(task.type);
for (let i = 0; i < tasks.length; i++) {
Expand Down
17 changes: 17 additions & 0 deletions packages/zone.js/test/zone-spec/task-tracking.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,21 @@ describe('TaskTrackingZone', function() {
expect((taskTrackingZoneSpec!.macroTasks[0] as any)['creationLocation']).toBeTruthy();
});
});

it('should track periodic task until it is canceled', (done) => {
taskTrackingZone.run(() => {
const interval = setInterval(() => {}, 1);
expect(taskTrackingZoneSpec!.macroTasks.length).toBe(1);
expect(taskTrackingZoneSpec!.macroTasks[0].source).toBe('setInterval');

setTimeout(() => {
expect(taskTrackingZoneSpec!.macroTasks.length).toBe(1);
expect(taskTrackingZoneSpec!.macroTasks[0].source).toBe('setInterval');

clearInterval(interval);
expect(taskTrackingZoneSpec!.macroTasks.length).toBe(0);
done();
}, 2);
});
});
});

0 comments on commit 6766dd1

Please sign in to comment.