From eb9250da94b29fc0502b0d7f12069e36adebfeac Mon Sep 17 00:00:00 2001 From: JiaLiPassion Date: Tue, 7 Mar 2017 09:14:55 +0900 Subject: [PATCH] fix(task): fix #638, eventTask/Periodical task should not be reset after cancel in running state (#642) --- lib/zone.ts | 6 +++++- test/common/zone.spec.ts | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/zone.ts b/lib/zone.ts index 82a38b07f..a5336e314 100644 --- a/lib/zone.ts +++ b/lib/zone.ts @@ -754,7 +754,11 @@ const Zone: ZoneType = (function(global: any) { } } finally { if (task.type == eventTask || (task.data && task.data.isPeriodic)) { - reEntryGuard && (task as ZoneTask)._transitionTo(scheduled, running, notScheduled); + // if the task's state is notScheduled, then it has already been cancelled + // we should not reset the state to scheduled + if (task.state !== notScheduled) { + reEntryGuard && (task as ZoneTask)._transitionTo(scheduled, running); + } } else { task.runCount = 0; this._updateTaskCount(task as ZoneTask, -1); diff --git a/test/common/zone.spec.ts b/test/common/zone.spec.ts index e5224081c..72e87f51c 100644 --- a/test/common/zone.spec.ts +++ b/test/common/zone.spec.ts @@ -299,6 +299,30 @@ describe('Zone', function() { ]); }); + it('period task should not transit to scheduled state after being cancelled in running state', + () => { + const zone = Zone.current.fork({name: 'testZone'}); + + const task = zone.scheduleMacroTask('testPeriodTask', () => { + zone.cancelTask(task); + }, {isPeriodic: true}, () => {}, () => {}); + + task.invoke(); + expect(task.state).toBe('notScheduled'); + }); + + it('event task should not transit to scheduled state after being cancelled in running state', + () => { + const zone = Zone.current.fork({name: 'testZone'}); + + const task = zone.scheduleEventTask('testEventTask', () => { + zone.cancelTask(task); + }, null, () => {}, () => {}); + + task.invoke(); + expect(task.state).toBe('notScheduled'); + }); + describe('assert ZoneAwarePromise', () => { it('should not throw when all is OK', () => { Zone.assertZonePatched();