Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/web): Copy EventTarget list before dispatch #19360

Merged
merged 4 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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: 14 additions & 0 deletions cli/tests/unit/event_target_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 1 addition & 5 deletions ext/web/02_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,12 +736,8 @@ function innerInvokeEventListeners(
return found;
}

let handlers = targetListeners[type];

// Copy event listeners before iterating since the list can be modified during the iteration.
if (handlers.length > 1) {
handlers = ArrayPrototypeSlice(targetListeners[type]);
}
const handlers = ArrayPrototypeSlice(targetListeners[type]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this was a useful optimization for us to keep our websocket perf up. Rather than reverting this and always cloning the array, I would suggest that we memoize the handlers.length instead so that in the case where we're not slicing the array, we'll only ever go through the loop once.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Github won't allow me to added suggestions on deleted lines, but I think it would look like the code below:

  let handlers = targetListeners[type];
  const handlersLength = handlers.length;
  // Copy event listeners before iterating since the list can be modified during the iteration.
  if (handlersLength > 1) {
    handlers = ArrayPrototypeSlice(targetListeners[type]);
  }
  
for (let i = 0; i < handlersLength; i++) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good! Your suggestion has been committed 🙂


for (let i = 0; i < handlers.length; i++) {
const listener = handlers[i];
Expand Down