Skip to content

Commit

Permalink
fix(task): fix angular#638, eventTask/Periodical task should not be r…
Browse files Browse the repository at this point in the history
…eset after cancel in running state
  • Loading branch information
JiaLiPassion committed Feb 21, 2017
1 parent 76c6ebf commit 8d27605
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions test/common/zone.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,30 @@ describe('Zone', function() {
]);
});

it('period task should not transite to schedued state after being cancelled in a 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 transite to schedued state after being cancelled in a 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();
Expand Down

0 comments on commit 8d27605

Please sign in to comment.