Skip to content

Commit ffc0e6e

Browse files
randalmurphalrmurphyclaude
authored
fix: release last_propagated_event after event propagation settles (#18569)
Fixes #18568 `last_propagated_event` was added in #16527 to stop Firefox from garbage collecting the event wrapper mid-propagation. The problem with this is that the even is now retained until the next event, which could be a while. This removes it after a macrotask. Co-authored-by: rmurphy <[email protected]> Co-authored-by: Claude Fable 5 <[email protected]>
1 parent f159e14 commit ffc0e6e

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎.changeset/spotty-ducks-refuse.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: release `last_propagated_event` after event propagation settles so it no longer retains the last event's target subtree

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,15 @@ export function delegate(events) {
159159
}
160160

161161
// used to store the reference to the currently propagated event
162-
// to prevent garbage collection between microtasks in Firefox
162+
// to prevent garbage collection between microtasks in Firefox (<= 141)
163163
// If the event object is GCed too early, the expando __root property
164164
// set on the event object is lost, causing the event delegation
165165
// to process the event twice
166166
let last_propagated_event = null;
167167

168+
// whether a task is already queued to clear `last_propagated_event`
169+
let last_propagated_event_clear_scheduled = false;
170+
168171
/**
169172
* @this {EventTarget}
170173
* @param {Event} event
@@ -179,6 +182,21 @@ export function handle_event_propagation(event) {
179182

180183
last_propagated_event = event;
181184

185+
// The reference is only needed while the event can still reach another
186+
// delegated root, i.e. during the current (synchronous) dispatch and its
187+
// microtask checkpoints. Clearing it in a later task preserves the
188+
// Firefox workaround while making sure the slot doesn't retain the last
189+
// event forever — through `event.target` it would otherwise keep the
190+
// entire detached subtree of whatever the user last clicked in alive
191+
// until the next delegated event happens to arrive.
192+
if (!last_propagated_event_clear_scheduled) {
193+
last_propagated_event_clear_scheduled = true;
194+
setTimeout(() => {
195+
last_propagated_event_clear_scheduled = false;
196+
last_propagated_event = null;
197+
});
198+
}
199+
182200
// composedPath contains list of nodes the event has propagated through.
183201
// We check `event_symbol` to skip all nodes below it in case this is a
184202
// parent of the `event_symbol` node, which indicates that there's nested

0 commit comments

Comments
 (0)