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

Commit f6d442f

Browse files
committed
fix(timer): setInterval should not auto cancel after callback invoked
1 parent e5fa562 commit f6d442f

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lib/common/timers.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ export function patchTimer(window: any, setName: string, cancelName: string, nam
3939
try {
4040
task.invoke.apply(this, arguments);
4141
} finally {
42+
if (task.data && task.data.isPeriodic) {
43+
// issue-934, task will be cancelled
44+
// even it is a periodic task such as
45+
// setInterval
46+
return;
47+
}
4248
if (typeof data.handleId === NUMBER) {
4349
// in non-nodejs env, we remove timerId
4450
// from local cache

test/common/setInterval.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,35 @@ describe('setInterval', function() {
6060
}, null, null, 'unit-test');
6161
});
6262

63+
it('should not cancel the task after invoke the setInterval callback', (done) => {
64+
const logs: string[] = [];
65+
const hasTaskSpy = jasmine.createSpy('hasTask');
66+
const zone = Zone.current.fork({
67+
name: 'interval',
68+
onHasTask:
69+
(delegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, hasTask: HasTaskState) => {
70+
hasTaskSpy(hasTask);
71+
return delegate.hasTask(targetZone, hasTask);
72+
}
73+
});
74+
75+
zone.run(() => {
76+
const timerId = setInterval(() => {
77+
logs.push('interval invoked');
78+
}, 100);
79+
(global as any)[Zone.__symbol__('setTimeout')](() => {
80+
expect(logs.length > 0).toBeTruthy();
81+
expect(hasTaskSpy)
82+
.toHaveBeenCalledWith(
83+
{microTask: false, macroTask: true, eventTask: false, change: 'macroTask'});
84+
clearInterval(timerId);
85+
expect(hasTaskSpy.calls.allArgs()).toEqual([
86+
[{microTask: false, macroTask: true, eventTask: false, change: 'macroTask'}],
87+
[{microTask: false, macroTask: false, eventTask: false, change: 'macroTask'}]
88+
]);
89+
done();
90+
}, 300);
91+
});
92+
});
93+
6394
});

0 commit comments

Comments
 (0)