Skip to content

wip: store additional property in WeakMap instead of on the event object directly. #16532

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

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 6 additions & 16 deletions packages/svelte/src/internal/client/dom/elements/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,7 @@ export function delegate(events) {
}
}

// used to store the reference to the currently propagated event
// to prevent garbage collection between microtasks in Firefox
// If the event object is GCed too early, the expando __root property
// set on the event object is lost, causing the event delegation
// to process the event twice
let last_propagated_event = null;
let event_propagation_root = new WeakMap();

/**
* @this {EventTarget}
Expand All @@ -160,16 +155,13 @@ export function handle_event_propagation(event) {
var path = event.composedPath?.() || [];
var current_target = /** @type {null | Element} */ (path[0] || event.target);

last_propagated_event = event;

// composedPath contains list of nodes the event has propagated through.
// We check __root to skip all nodes below it in case this is a
// parent of the __root node, which indicates that there's nested
// We check to skip all nodes below it in case this is a
// parent of the event_propagation_root node, which indicates that there's nested
// mounted apps. In this case we don't want to trigger events multiple times.
var path_idx = 0;

// @ts-expect-error is added below
var handled_at = event.__root;
var handled_at = event_propagation_root.get(event);

if (handled_at) {
var at_idx = path.indexOf(handled_at);
Expand All @@ -180,8 +172,7 @@ export function handle_event_propagation(event) {
// This is the fallback document listener or a window listener, but the event was already handled
// -> ignore, but set handle_at to document/window so that we're resetting the event
// chain in case someone manually dispatches the same event object again.
// @ts-expect-error
event.__root = handler_element;
event_propagation_root.set(event, handler_element);
return;
}

Expand Down Expand Up @@ -285,8 +276,7 @@ export function handle_event_propagation(event) {
throw throw_error;
}
} finally {
// @ts-expect-error is used above
event.__root = handler_element;
event_propagation_root.set(event, handler_element);
// @ts-ignore remove proxy on currentTarget
delete event.currentTarget;
set_active_reaction(previous_reaction);
Expand Down
Loading