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

Commit eb9250d

Browse files
JiaLiPassionmhevery
authored andcommitted
fix(task): fix #638, eventTask/Periodical task should not be reset after cancel in running state (#642)
1 parent 0534b19 commit eb9250d

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

Diff for: lib/zone.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,11 @@ const Zone: ZoneType = (function(global: any) {
754754
}
755755
} finally {
756756
if (task.type == eventTask || (task.data && task.data.isPeriodic)) {
757-
reEntryGuard && (task as ZoneTask)._transitionTo(scheduled, running, notScheduled);
757+
// if the task's state is notScheduled, then it has already been cancelled
758+
// we should not reset the state to scheduled
759+
if (task.state !== notScheduled) {
760+
reEntryGuard && (task as ZoneTask)._transitionTo(scheduled, running);
761+
}
758762
} else {
759763
task.runCount = 0;
760764
this._updateTaskCount(task as ZoneTask, -1);

Diff for: test/common/zone.spec.ts

+24
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,30 @@ describe('Zone', function() {
299299
]);
300300
});
301301

302+
it('period task should not transit to scheduled state after being cancelled in running state',
303+
() => {
304+
const zone = Zone.current.fork({name: 'testZone'});
305+
306+
const task = zone.scheduleMacroTask('testPeriodTask', () => {
307+
zone.cancelTask(task);
308+
}, {isPeriodic: true}, () => {}, () => {});
309+
310+
task.invoke();
311+
expect(task.state).toBe('notScheduled');
312+
});
313+
314+
it('event task should not transit to scheduled state after being cancelled in running state',
315+
() => {
316+
const zone = Zone.current.fork({name: 'testZone'});
317+
318+
const task = zone.scheduleEventTask('testEventTask', () => {
319+
zone.cancelTask(task);
320+
}, null, () => {}, () => {});
321+
322+
task.invoke();
323+
expect(task.state).toBe('notScheduled');
324+
});
325+
302326
describe('assert ZoneAwarePromise', () => {
303327
it('should not throw when all is OK', () => {
304328
Zone.assertZonePatched();

0 commit comments

Comments
 (0)