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

fix(task): fix #778, sometimes task will run after being canceled #780

Merged
merged 1 commit into from
May 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,10 +756,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