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

Commit 4772348

Browse files
committed
fix(event): fix #836, handle event callback call removeEventListener case
1 parent 3a4bfbd commit 4772348

File tree

2 files changed

+414
-4
lines changed

2 files changed

+414
-4
lines changed

lib/common/events.ts

+29-4
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,20 @@ export function patchEventTarget(
6262
const tasks = target[zoneSymbolEventNames[event.type][FALSE_STR]];
6363
if (tasks) {
6464
// invoke all tasks which attached to current target with given event.type and capture = false
65-
for (let i = 0; i < tasks.length; i++) {
66-
invokeTask(tasks[i], target, event);
65+
if (tasks.length === 1) {
66+
invokeTask(tasks[0], target, event);
67+
} else {
68+
// https://github.com/angular/zone.js/issues/836
69+
// copy the tasks array before invoke, to avoid
70+
// the callback will remove itself or other listener
71+
const copyTasks = tasks.slice();
72+
for (let i = 0; i < copyTasks.length; i++) {
73+
const task = copyTasks[i];
74+
if (tasks.indexOf(task) === -1) {
75+
continue;
76+
}
77+
invokeTask(task, target, event);
78+
}
6779
}
6880
}
6981
};
@@ -74,8 +86,21 @@ export function patchEventTarget(
7486

7587
const tasks = target[zoneSymbolEventNames[event.type][TRUE_STR]];
7688
if (tasks) {
77-
for (let i = 0; i < tasks.length; i++) {
78-
invokeTask(tasks[i], target, event);
89+
// invoke all tasks which attached to current target with given event.type and capture = false
90+
if (tasks.length === 1) {
91+
invokeTask(tasks[0], target, event);
92+
} else {
93+
// https://github.com/angular/zone.js/issues/836
94+
// copy the tasks array before invoke, to avoid
95+
// the callback will remove itself or other listener
96+
const copyTasks = tasks.slice();
97+
for (let i = 0; i < copyTasks.length; i++) {
98+
const task = copyTasks[i];
99+
if (tasks.indexOf(task) === -1) {
100+
continue;
101+
}
102+
invokeTask(task, target, event);
103+
}
79104
}
80105
}
81106
};

0 commit comments

Comments
 (0)