Skip to content

Commit 57969ed

Browse files
committed
store additional property in WeakMap instead of on the event object directly.
1 parent 72e46d3 commit 57969ed

File tree

1 file changed

+6
-16
lines changed
  • packages/svelte/src/internal/client/dom/elements

1 file changed

+6
-16
lines changed

packages/svelte/src/internal/client/dom/elements/events.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,7 @@ export function delegate(events) {
141141
}
142142
}
143143

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

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

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

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

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

@@ -285,8 +276,7 @@ export function handle_event_propagation(event) {
285276
throw throw_error;
286277
}
287278
} finally {
288-
// @ts-expect-error is used above
289-
event.__root = handler_element;
279+
event_propagation_root.set(event, handler_element);
290280
// @ts-ignore remove proxy on currentTarget
291281
delete event.currentTarget;
292282
set_active_reaction(previous_reaction);

0 commit comments

Comments
 (0)