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

Commit b7238c8

Browse files
JiaLiPassionmhevery
authored andcommitted
fix(task): fix #778, sometimes task will run after being canceled (#780)
1 parent 245f8e9 commit b7238c8

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

Diff for: lib/zone.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -759,10 +759,22 @@ const Zone: ZoneType = (function(global: any) {
759759

760760

761761
runTask(task: Task, applyThis?: any, applyArgs?: any): any {
762-
if (task.zone != this)
762+
if (task.zone != this) {
763763
throw new Error(
764764
'A task can only be run in the zone of creation! (Creation: ' +
765765
(task.zone || NO_ZONE).name + '; Execution: ' + this.name + ')');
766+
}
767+
// https://github.com/angular/zone.js/issues/778, sometimes eventTask
768+
// will run in notScheduled(canceled) state, we should not try to
769+
// run such kind of task but just return
770+
771+
// we have to define an variable here, if not
772+
// typescript compiler will complain below
773+
const isNotScheduled = task.state === notScheduled;
774+
if (isNotScheduled && task.type === eventTask) {
775+
return;
776+
}
777+
766778
const reEntryGuard = task.state != running;
767779
reEntryGuard && (task as ZoneTask<any>)._transitionTo(running, scheduled);
768780
task.runCount++;

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

+20
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,26 @@ describe('task lifecycle', () => {
873873
{toState: 'notScheduled', fromState: 'running'}
874874
]);
875875
}));
876+
877+
it('task should not run if task transite to notScheduled state which was canceled',
878+
testFnWithLoggedTransitionTo(() => {
879+
let task: Task;
880+
Zone.current.fork({name: 'testCancelZone'}).run(() => {
881+
const task = Zone.current.scheduleEventTask('testEventTask', noop, null, noop, noop);
882+
Zone.current.cancelTask(task);
883+
task.invoke();
884+
});
885+
expect(log.map(item => {
886+
return {toState: item.toState, fromState: item.fromState};
887+
}))
888+
.toEqual([
889+
{toState: 'scheduling', fromState: 'notScheduled'},
890+
{toState: 'scheduled', fromState: 'scheduling'},
891+
{toState: 'canceling', fromState: 'scheduled'},
892+
{toState: 'notScheduled', fromState: 'canceling'}
893+
]);
894+
}));
895+
876896
});
877897

878898
describe('reschedule zone', () => {

0 commit comments

Comments
 (0)