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

Commit

Permalink
fix(task): fix #778, sometimes task will run after being canceled (#780)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion authored and mhevery committed May 19, 2017
1 parent 245f8e9 commit b7238c8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,10 +759,22 @@ const Zone: ZoneType = (function(global: any) {


runTask(task: Task, applyThis?: any, applyArgs?: any): any {
if (task.zone != this)
if (task.zone != this) {
throw new Error(
'A task can only be run in the zone of creation! (Creation: ' +
(task.zone || NO_ZONE).name + '; Execution: ' + this.name + ')');
}
// https://github.com/angular/zone.js/issues/778, sometimes eventTask
// will run in notScheduled(canceled) state, we should not try to
// run such kind of task but just return

// we have to define an variable here, if not
// typescript compiler will complain below
const isNotScheduled = task.state === notScheduled;
if (isNotScheduled && task.type === eventTask) {
return;
}

const reEntryGuard = task.state != running;
reEntryGuard && (task as ZoneTask<any>)._transitionTo(running, scheduled);
task.runCount++;
Expand Down
20 changes: 20 additions & 0 deletions test/common/task.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,26 @@ describe('task lifecycle', () => {
{toState: 'notScheduled', fromState: 'running'}
]);
}));

it('task should not run if task transite to notScheduled state which was canceled',
testFnWithLoggedTransitionTo(() => {
let task: Task;
Zone.current.fork({name: 'testCancelZone'}).run(() => {
const task = Zone.current.scheduleEventTask('testEventTask', noop, null, noop, noop);
Zone.current.cancelTask(task);
task.invoke();
});
expect(log.map(item => {
return {toState: item.toState, fromState: item.fromState};
}))
.toEqual([
{toState: 'scheduling', fromState: 'notScheduled'},
{toState: 'scheduled', fromState: 'scheduling'},
{toState: 'canceling', fromState: 'scheduled'},
{toState: 'notScheduled', fromState: 'canceling'}
]);
}));

});

describe('reschedule zone', () => {
Expand Down

0 comments on commit b7238c8

Please sign in to comment.