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 (angular#45391)

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

PR Close angular#45391
  • Loading branch information
Platonn authored and josmar-crwdstffng committed Apr 8, 2022
1 parent 221ae8a commit 7c7ee37
Show file tree
Hide file tree
Showing 2 changed files with 22 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
21 changes: 21 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,25 @@ describe('TaskTrackingZone', function() {
expect((taskTrackingZoneSpec!.macroTasks[0] as any)['creationLocation']).toBeTruthy();
});
});

it('should track periodic task until it is canceled', (done) => {
taskTrackingZone.run(() => {
const intervalCallback = jasmine.createSpy('intervalCallback');
const interval = setInterval(intervalCallback, 1);

expect(intervalCallback).not.toHaveBeenCalled();
expect(taskTrackingZoneSpec!.macroTasks.length).toBe(1);
expect(taskTrackingZoneSpec!.macroTasks[0].source).toBe('setInterval');

setTimeout(() => {
expect(intervalCallback).toHaveBeenCalled();
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 7c7ee37

Please sign in to comment.