diff --git a/cli/tests/unit/event_target_test.ts b/cli/tests/unit/event_target_test.ts index 49bd354aa2..c7acab364c 100644 --- a/cli/tests/unit/event_target_test.ts +++ b/cli/tests/unit/event_target_test.ts @@ -245,6 +245,20 @@ Deno.test(function eventTargetDispatchShouldSetTargetInListener() { assertEquals(called, true); }); +Deno.test(function eventTargetDispatchShouldFireCurrentListenersOnly() { + const target = new EventTarget(); + const event = new Event("foo"); + let callCount = 0; + target.addEventListener("foo", () => { + ++callCount; + target.addEventListener("foo", () => { + ++callCount; + }); + }); + target.dispatchEvent(event); + assertEquals(callCount, 1); +}); + Deno.test(function eventTargetAddEventListenerGlobalAbort() { return new Promise((resolve) => { const c = new AbortController(); diff --git a/ext/web/02_event.js b/ext/web/02_event.js index e7553ea89b..142fa66b2b 100644 --- a/ext/web/02_event.js +++ b/ext/web/02_event.js @@ -737,13 +737,14 @@ function innerInvokeEventListeners( } let handlers = targetListeners[type]; + const handlersLength = handlers.length; // Copy event listeners before iterating since the list can be modified during the iteration. - if (handlers.length > 1) { + if (handlersLength > 1) { handlers = ArrayPrototypeSlice(targetListeners[type]); } - for (let i = 0; i < handlers.length; i++) { + for (let i = 0; i < handlersLength; i++) { const listener = handlers[i]; let capture, once, passive;