svelte 5.58.0 → 5.58.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/compiler/index.js +1 -1
  2. package/package.json +1 -1
  3. package/src/compiler/phases/3-transform/client/utils.js +3 -2
  4. package/src/compiler/phases/3-transform/server/visitors/ExpressionStatement.js +8 -0
  5. package/src/compiler/print/index.js +45 -57
  6. package/src/index-client.js +3 -3
  7. package/src/internal/client/constants.js +8 -1
  8. package/src/internal/client/dev/debug.js +6 -6
  9. package/src/internal/client/dev/tracing.js +67 -11
  10. package/src/internal/client/dom/blocks/async.js +2 -1
  11. package/src/internal/client/dom/blocks/await.js +108 -18
  12. package/src/internal/client/dom/blocks/boundary.js +107 -70
  13. package/src/internal/client/dom/blocks/branches.js +15 -22
  14. package/src/internal/client/dom/blocks/each.js +88 -13
  15. package/src/internal/client/dom/elements/attributes.js +2 -1
  16. package/src/internal/client/dom/elements/bindings/input.js +19 -10
  17. package/src/internal/client/dom/elements/bindings/select.js +2 -7
  18. package/src/internal/client/dom/elements/events.js +27 -2
  19. package/src/internal/client/dom/operations.js +2 -3
  20. package/src/internal/client/error-handling.js +4 -12
  21. package/src/internal/client/errors.js +18 -2
  22. package/src/internal/client/index.js +0 -1
  23. package/src/internal/client/reactivity/async.js +247 -122
  24. package/src/internal/client/reactivity/batch.js +1102 -530
  25. package/src/internal/client/reactivity/deriveds.js +281 -177
  26. package/src/internal/client/reactivity/effects.js +44 -28
  27. package/src/internal/client/reactivity/sources.js +30 -124
  28. package/src/internal/client/reactivity/status.js +1 -1
  29. package/src/internal/client/render.js +11 -0
  30. package/src/internal/client/runtime.js +925 -152
  31. package/src/internal/client/validate.js +2 -2
  32. package/src/version.js +1 -1
  33. package/types/index.d.ts +1 -1
  34. package/types/index.d.ts.map +1 -1
  35. package/src/internal/client/reactivity/utils.js +0 -18
@@ -3,8 +3,10 @@ import {
3
3
  BOUNDARY_EFFECT,
4
4
  DESTROYED,
5
5
  DESTROYING,
6
+ DIRTY,
6
7
  EFFECT_PRESERVED,
7
- EFFECT_TRANSPARENT
8
+ EFFECT_TRANSPARENT,
9
+ MAYBE_DIRTY
8
10
  } from '#client/constants';
9
11
  import {
10
12
  HYDRATION_ERROR,
@@ -45,7 +47,7 @@ import { internal_set, source } from '../../reactivity/sources.js';
45
47
  import { tag } from '../../dev/tracing.js';
46
48
  import { createSubscriber } from '../../../../reactivity/create-subscriber.js';
47
49
  import { create_text } from '../operations.js';
48
- import { defer_effect } from '../../reactivity/utils.js';
50
+ import { set_signal_status } from '../../reactivity/status.js';
49
51
 
50
52
  /**
51
53
  * @typedef {{
@@ -109,11 +111,20 @@ export class Boundary {
109
111
  #offscreen_fragment = null;
110
112
 
111
113
  #local_pending_count = 0;
112
- #pending_count = 0;
113
114
  #pending_count_update_queued = false;
114
115
 
115
- /** @type {Map<Effect, number>} */
116
- #dirty_reactions = new Map();
116
+ /**
117
+ * User effects created inside this boundary. They will run when the boundary is revealed
118
+ * @type {Effect[]}
119
+ */
120
+ #effects = [];
121
+
122
+ /**
123
+ * Async effects that are currently in flight, grouped by the batch that started them.
124
+ * A speculative fork's work must not hold up the real world's boundary.
125
+ * @type {Map<Batch, Map<Effect, number>>}
126
+ */
127
+ #pending = new Map();
117
128
 
118
129
  /**
119
130
  * A source containing the number of pending async deriveds/expressions.
@@ -285,7 +296,6 @@ export class Boundary {
285
296
 
286
297
  var fragment = (this.#offscreen_fragment = document.createDocumentFragment());
287
298
  var anchor = create_text();
288
- var handled = false;
289
299
 
290
300
  fragment.append(anchor);
291
301
 
@@ -295,7 +305,6 @@ export class Boundary {
295
305
  } catch (error) {
296
306
  try {
297
307
  this.error(error);
298
- handled = true;
299
308
  } catch (error) {
300
309
  invoke_error_boundary(error, this.#effect.parent);
301
310
  }
@@ -304,21 +313,14 @@ export class Boundary {
304
313
  }
305
314
  });
306
315
 
307
- if (this.#main_effect === null) {
308
- this.#offscreen_fragment = null;
309
- if (handled) this.#resolve(/** @type {Batch} */ (current_batch));
310
- return;
311
- }
312
-
313
- if (this.#pending_count === 0) {
316
+ if (this.#main_effect !== null && this.#pending.size === 0) {
317
+ this.is_pending = false;
314
318
  this.#anchor.before(fragment);
315
319
  this.#offscreen_fragment = null;
316
320
 
317
321
  pause_effect(/** @type {Effect} */ (this.#pending_effect), () => {
318
322
  this.#pending_effect = null;
319
323
  });
320
-
321
- this.#resolve(/** @type {Batch} */ (current_batch));
322
324
  }
323
325
  });
324
326
  }
@@ -326,52 +328,35 @@ export class Boundary {
326
328
  #render() {
327
329
  try {
328
330
  this.is_pending = this.has_pending_snippet();
329
- this.#pending_count = 0;
330
331
  this.#local_pending_count = 0;
331
332
 
332
333
  this.#main_effect = branch(() => {
333
334
  this.#children(this.#anchor);
334
335
  });
335
336
 
336
- if (this.#pending_count > 0) {
337
- var fragment = (this.#offscreen_fragment = document.createDocumentFragment());
338
- move_effect(this.#main_effect, fragment);
339
-
340
- const pending = /** @type {(anchor: Node) => void} */ (this.#props.pending);
341
- this.#pending_effect = branch(() => pending(this.#anchor));
337
+ if (this.#pending.size === 0) {
338
+ this.is_pending = false;
342
339
  } else {
343
- this.#resolve(/** @type {Batch} */ (current_batch));
340
+ const pending = this.#props.pending;
341
+
342
+ if (pending) {
343
+ var fragment = (this.#offscreen_fragment = document.createDocumentFragment());
344
+ move_effect(this.#main_effect, fragment);
345
+
346
+ this.#pending_effect = branch(() => pending(this.#anchor));
347
+ }
344
348
  }
345
349
  } catch (error) {
346
350
  this.error(error);
347
351
  }
348
352
  }
349
353
 
350
- /**
351
- * @param {Batch} batch
352
- */
353
- #resolve(batch) {
354
- this.is_pending = false;
355
-
356
- // any effects that were previously deferred should be transferred
357
- // to the batch, which will flush in the next microtask
358
- batch.transfer_reactions(this.#dirty_reactions);
359
- }
360
-
361
354
  /**
362
355
  * Defer an effect inside a pending boundary until the boundary resolves
363
356
  * @param {Effect} effect
364
357
  */
365
358
  defer_effect(effect) {
366
- defer_effect(effect, this.#dirty_reactions);
367
- }
368
-
369
- /**
370
- * Returns `false` if the effect exists inside a boundary whose pending snippet is shown
371
- * @returns {boolean}
372
- */
373
- is_rendered() {
374
- return !this.is_pending && (!this.parent || this.parent.is_rendered());
359
+ this.#effects.push(effect);
375
360
  }
376
361
 
377
362
  has_pending_snippet() {
@@ -402,49 +387,101 @@ export class Boundary {
402
387
  }
403
388
 
404
389
  /**
405
- * Updates the pending count associated with the currently visible pending snippet,
406
- * if any, such that we can replace the snippet with content once work is done
407
- * @param {1 | -1} d
408
390
  * @param {Batch} batch
409
391
  */
410
- #update_pending_count(d, batch) {
411
- if (!this.has_pending_snippet()) {
412
- if (this.parent) {
413
- this.parent.#update_pending_count(d, batch);
392
+ is_ready(batch) {
393
+ if (batch.is_fork && this.#pending.size > 0) {
394
+ return false;
395
+ }
396
+
397
+ for (const [owner, pending] of this.#pending) {
398
+ if (owner.is_fork) continue;
399
+
400
+ for (const effect of pending.keys()) {
401
+ if (batch.is_blocked_by(effect, this.#main_effect)) {
402
+ return false;
403
+ }
414
404
  }
405
+ }
406
+
407
+ return true;
408
+ }
415
409
 
416
- // if there's no parent, we're in a scope with no pending snippet
417
- return;
410
+ /**
411
+ * @param {Effect[]} effects
412
+ */
413
+ reveal(effects) {
414
+ this.is_pending = false;
415
+
416
+ // Add any deferred effects to the batch's effect queue
417
+ for (const effect of this.#effects) {
418
+ set_signal_status(effect, DIRTY);
419
+ effects.push(effect);
418
420
  }
419
421
 
420
- this.#pending_count += d;
422
+ this.#effects.length = 0;
421
423
 
422
- if (this.#pending_count === 0) {
423
- this.#resolve(batch);
424
+ if (this.#pending_effect) {
425
+ pause_effect(this.#pending_effect, () => {
426
+ this.#pending_effect = null;
427
+ });
428
+ }
424
429
 
425
- if (this.#pending_effect) {
426
- pause_effect(this.#pending_effect, () => {
427
- this.#pending_effect = null;
428
- });
429
- }
430
+ if (this.#offscreen_fragment) {
431
+ this.#anchor.before(this.#offscreen_fragment);
432
+ this.#offscreen_fragment = null;
433
+ }
434
+ }
430
435
 
431
- if (this.#offscreen_fragment) {
432
- this.#anchor.before(this.#offscreen_fragment);
433
- this.#offscreen_fragment = null;
436
+ /**
437
+ * @param {Effect} effect
438
+ * @param {Batch} batch
439
+ */
440
+ increment(effect, batch) {
441
+ var pending = this.#pending.get(batch);
442
+
443
+ if (pending === undefined) {
444
+ this.#pending.set(batch, (pending = new Map()));
445
+ }
446
+
447
+ var count = pending.get(effect) ?? 0;
448
+ pending.set(effect, count + 1);
449
+ }
450
+
451
+ /**
452
+ * @param {Effect} effect
453
+ * @param {Batch} batch
454
+ */
455
+ decrement(effect, batch) {
456
+ var pending = /** @type {Map<Effect, number>} */ (this.#pending.get(batch));
457
+ var count = /** @type {number} */ (pending.get(effect));
458
+
459
+ if (count === 1) {
460
+ pending.delete(effect);
461
+
462
+ if (pending.size === 0) {
463
+ this.#pending.delete(batch);
434
464
  }
465
+
466
+ // The remaining work may belong to speculative forks. Check again in a normal batch,
467
+ // after the result's batch has flushed, so that we reveal committed content and run
468
+ // deferred effects in the same view.
469
+ queue_micro_task(() => {
470
+ if (this.is_pending && !this.#is_destroyed()) {
471
+ Batch.ensure().schedule(this.#effect, MAYBE_DIRTY);
472
+ }
473
+ });
474
+ } else {
475
+ pending.set(effect, count - 1);
435
476
  }
436
477
  }
437
478
 
438
479
  /**
439
- * Update the source that powers `$effect.pending()` inside this boundary,
440
- * and controls when the current `pending` snippet (if any) is removed.
480
+ * Update the source that powers `$effect.pending()` inside this boundary.
441
481
  * Do not call from inside the class
442
482
  * @param {1 | -1} d
443
- * @param {Batch} batch
444
483
  */
445
- update_pending_count(d, batch) {
446
- this.#update_pending_count(d, batch);
447
-
484
+ update_effect_pending(d) {
448
485
  this.#local_pending_count += d;
449
486
 
450
487
  if (!this.#effect_pending || this.#pending_count_update_queued) return;
@@ -9,6 +9,7 @@ import {
9
9
  resume_effect
10
10
  } from '../../reactivity/effects.js';
11
11
  import { HMR_ANCHOR } from '../../constants.js';
12
+ import { EFFECT_OFFSCREEN } from '#client/constants';
12
13
  import { hydrate_node, hydrating } from '../hydration.js';
13
14
  import { create_text, should_defer_append } from '../operations.js';
14
15
  import { DEV } from 'esm-env';
@@ -108,26 +109,12 @@ export class BranchManager {
108
109
  // ...and append the fragment
109
110
  this.anchor.before(offscreen.fragment);
110
111
  onscreen = offscreen.effect;
112
+ onscreen.f &= ~EFFECT_OFFSCREEN;
111
113
  }
112
114
  }
113
115
 
114
- for (const [b, k] of this.#batches) {
115
- this.#batches.delete(b);
116
-
117
- if (b === batch) {
118
- // keep values for newer batches
119
- break;
120
- }
121
-
122
- const offscreen = this.#offscreen.get(k);
123
-
124
- if (offscreen) {
125
- // for older batches, destroy offscreen effects
126
- // as they will never be committed
127
- destroy_effect(offscreen.effect);
128
- this.#offscreen.delete(k);
129
- }
130
- }
116
+ // other batches can still commit (in any order), so their selections remain
117
+ this.#discard(batch);
131
118
 
132
119
  // outro/destroy all onscreen effects...
133
120
  for (const [k, effect] of this.#onscreen) {
@@ -167,7 +154,10 @@ export class BranchManager {
167
154
  * @param {Batch} batch
168
155
  */
169
156
  #discard = (batch) => {
170
- this.#batches.delete(batch);
157
+ for (const b of this.#batches.keys()) {
158
+ // (batches that were merged into another one don't select anything anymore)
159
+ if (b === batch || !b.linked) this.#batches.delete(b);
160
+ }
171
161
 
172
162
  const keys = Array.from(this.#batches.values());
173
163
 
@@ -195,10 +185,13 @@ export class BranchManager {
195
185
 
196
186
  fragment.append(target);
197
187
 
198
- this.#offscreen.set(key, {
199
- effect: branch(() => fn(target)),
200
- fragment
201
- });
188
+ var effect = branch(() => fn(target));
189
+
190
+ // until it is committed, the branch only exists for the batches that select it
191
+ effect.f |= EFFECT_OFFSCREEN;
192
+ batch.unskip_new_effect(effect);
193
+
194
+ this.#offscreen.set(key, { effect, fragment });
202
195
  } else {
203
196
  this.#onscreen.set(
204
197
  key,
@@ -133,23 +133,26 @@ function pause_effects(state, to_destroy, controlled_anchor) {
133
133
 
134
134
  /**
135
135
  * @param {EachState} state
136
- * @param {Effect[]} to_destroy
137
- * @param {boolean} remove_dom
136
+ * @returns {Set<Effect>}
138
137
  */
139
- function destroy_effects(state, to_destroy, remove_dom = true) {
140
- /** @type {Set<Effect> | undefined} */
141
- var preserved_effects;
142
-
138
+ function selected_effects(state) {
139
+ var selected = new Set();
143
140
  // The loop-in-a-loop isn't ideal, but we should only hit this in relatively rare cases
144
- if (state.pending.size > 0) {
145
- preserved_effects = new Set();
146
-
147
- for (const keys of state.pending.values()) {
148
- for (const key of keys) {
149
- preserved_effects.add(/** @type {EachItem} */ (state.items.get(key)).e);
150
- }
141
+ for (const keys of state.pending.values()) {
142
+ for (const key of keys) {
143
+ selected.add(/** @type {EachItem} */ (state.items.get(key)).e);
151
144
  }
152
145
  }
146
+ return selected;
147
+ }
148
+
149
+ /**
150
+ * @param {EachState} state
151
+ * @param {Effect[]} to_destroy
152
+ * @param {boolean} remove_dom
153
+ */
154
+ function destroy_effects(state, to_destroy, remove_dom = true) {
155
+ var preserved_effects = state.pending.size > 0 ? selected_effects(state) : undefined;
153
156
 
154
157
  for (var i = 0; i < to_destroy.length; i++) {
155
158
  var e = to_destroy[i];
@@ -159,12 +162,34 @@ function destroy_effects(state, to_destroy, remove_dom = true) {
159
162
 
160
163
  const fragment = document.createDocumentFragment();
161
164
  move_effect(e, fragment);
165
+
166
+ // `reconcile` expects offscreen items to come after all onscreen items
167
+ if (e !== state.effect.last) {
168
+ link(state, e.prev, e.next);
169
+ link(state, state.effect.last, e);
170
+ link(state, e, null);
171
+ }
162
172
  } else {
163
173
  destroy_effect(to_destroy[i], remove_dom);
164
174
  }
165
175
  }
166
176
  }
167
177
 
178
+ /**
179
+ * Whether the item is outroing, i.e. `pause_effects` destroys it (or keeps it offscreen) once its outro is done
180
+ * @param {EachState} state
181
+ * @param {Effect} effect
182
+ */
183
+ function is_outroing(state, effect) {
184
+ if (state.outrogroups !== null) {
185
+ for (const group of state.outrogroups) {
186
+ if (group.pending.has(effect) || group.done.has(effect)) return true;
187
+ }
188
+ }
189
+
190
+ return false;
191
+ }
192
+
168
193
  /** @type {TemplateNode} */
169
194
  var offscreen_anchor;
170
195
 
@@ -256,6 +281,8 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
256
281
  });
257
282
  }
258
283
  }
284
+
285
+ destroy_unselected();
259
286
  }
260
287
 
261
288
  /**
@@ -263,6 +290,42 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
263
290
  */
264
291
  function discard(batch) {
265
292
  state.pending.delete(batch);
293
+
294
+ if ((state.effect.f & DESTROYED) === 0) {
295
+ destroy_unselected();
296
+ }
297
+ }
298
+
299
+ /**
300
+ * Destroys the offscreen items that no pending batch selects anymore. Offscreen items only exist
301
+ * for the batches that select them: they were created in their view, or kept for them when a
302
+ * commit removed them (see `destroy_effects`). A batch stops selecting an item when it's discarded,
303
+ * or when its block runs again without it. `reconcile` doesn't destroy such an item if it was kept,
304
+ * as kept items are paused, and it leaves paused items alone (taking them to be outroing)
305
+ */
306
+ function destroy_unselected() {
307
+ /** @type {Set<Effect> | undefined} */
308
+ var selected;
309
+ var effect = state.effect.last;
310
+
311
+ // offscreen items come after all onscreen items (see `destroy_effects`)
312
+ while (effect !== null) {
313
+ var prev = effect.prev;
314
+
315
+ // (an offscreen fallback isn't paused, so the next commit shows it or destroys it, see above)
316
+ if ((effect.f & BRANCH_EFFECT) !== 0 && effect !== fallback) {
317
+ if ((effect.f & EFFECT_OFFSCREEN) === 0) break;
318
+
319
+ // create lazily to save work in the common case
320
+ selected ??= selected_effects(state);
321
+
322
+ if (!selected.has(effect) && !is_outroing(state, effect)) {
323
+ destroy_effect(effect);
324
+ }
325
+ }
326
+
327
+ effect = prev;
328
+ }
266
329
  }
267
330
 
268
331
  var effect = block(() => {
@@ -336,7 +399,9 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
336
399
  );
337
400
 
338
401
  if (!first_run) {
402
+ // until it is committed, the item only exists for the batches that select it
339
403
  item.e.f |= EFFECT_OFFSCREEN;
404
+ if (defer) batch.unskip_new_effect(item.e);
340
405
  }
341
406
 
342
407
  items.set(key, item);
@@ -350,7 +415,17 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
350
415
  fallback = branch(() => fallback_fn(anchor));
351
416
  } else {
352
417
  fallback = branch(() => fallback_fn((offscreen_anchor ??= create_text())));
418
+ // until it is committed, the fallback only exists for the batches that select it
353
419
  fallback.f |= EFFECT_OFFSCREEN;
420
+ if (defer) batch.unskip_new_effect(fallback);
421
+ }
422
+ }
423
+
424
+ if (defer && fallback !== null) {
425
+ if (length === 0) {
426
+ batch.unskip_effect(fallback);
427
+ } else {
428
+ batch.skip_effect(fallback);
354
429
  }
355
430
  }
356
431
 
@@ -1,4 +1,5 @@
1
- /** @import { Blocker, Effect } from '#client' */
1
+ /** @import { Effect } from '#client' */
2
+ /** @import { Blocker } from '../../reactivity/async.js' */
2
3
  import { DEV } from 'esm-env';
3
4
  import { hydrating, set_hydrating } from '../hydration.js';
4
5
  import { get_descriptors, get_prototype_of } from '../../../shared/utils.js';
@@ -7,8 +7,7 @@ import { is } from '../../../proxy.js';
7
7
  import { queue_micro_task } from '../../task.js';
8
8
  import { hydrating } from '../../hydration.js';
9
9
  import { tick, untrack } from '../../../runtime.js';
10
- import { current_batch, previous_batch } from '../../../reactivity/batch.js';
11
- import { async_mode_flag } from '../../../../flags/index.js';
10
+ import { current_batch, view_batch } from '../../../reactivity/batch.js';
12
11
 
13
12
  /**
14
13
  * @param {HTMLInputElement} input
@@ -87,16 +86,12 @@ export function bind_value(input, get, set = get) {
87
86
  var value = get();
88
87
 
89
88
  if (input === document.activeElement) {
90
- // In sync mode render effects are executed during tree traversal -> needs current_batch
91
- // In async mode render effects are flushed once batch resolved, at which point current_batch is null -> needs previous_batch
92
- var batch = /** @type {Batch} */ (async_mode_flag ? previous_batch : current_batch);
93
-
94
89
  // Never rewrite the contents of a focused input. We can get here if, for example,
95
90
  // an update is deferred because of async work depending on the input:
96
91
  //
97
92
  // <input bind:value={query}>
98
93
  // <p>{await find(query)}</p>
99
- if (batches.has(batch)) {
94
+ if (batches.has(/** @type {Batch} */ (view_batch))) {
100
95
  return;
101
96
  }
102
97
  }
@@ -294,9 +289,23 @@ function to_number(value) {
294
289
  * @param {(value: FileList | null) => void} set
295
290
  */
296
291
  export function bind_files(input, get, set = get) {
297
- listen_to_event_and_reset_event(input, 'change', () => {
298
- set(input.files);
299
- });
292
+ listen_to_event_and_reset_event(
293
+ input,
294
+ 'change',
295
+ () => {
296
+ set(input.files);
297
+ },
298
+ () => {
299
+ // A form reset can empty the input's `FileList` in place, which is then the very
300
+ // object the binding already holds and would not be seen as a change: give the
301
+ // input a new, empty one instead
302
+ if (typeof DataTransfer === 'function') {
303
+ input.files = new DataTransfer().files;
304
+ }
305
+
306
+ set(input.files);
307
+ }
308
+ );
300
309
 
301
310
  if (
302
311
  // If we are hydrating and the value has since changed,
@@ -4,8 +4,7 @@ import { listen_to_event_and_reset_event } from './shared.js';
4
4
  import { is } from '../../../proxy.js';
5
5
  import { is_array } from '../../../../shared/utils.js';
6
6
  import * as w from '../../../warnings.js';
7
- import { current_batch, previous_batch } from '../../../reactivity/batch.js';
8
- import { async_mode_flag } from '../../../../flags/index.js';
7
+ import { current_batch, view_batch } from '../../../reactivity/batch.js';
9
8
 
10
9
  /**
11
10
  * Sets the `selected` attribute on an option so form reset can restore it.
@@ -195,16 +194,12 @@ export function bind_select_value(select, get, set = get) {
195
194
  var value = get();
196
195
 
197
196
  if (select === document.activeElement) {
198
- // In sync mode render effects are executed during tree traversal -> needs current_batch
199
- // In async mode render effects are flushed once batch resolved, at which point current_batch is null -> needs previous_batch
200
- var batch = /** @type {Batch} */ (async_mode_flag ? previous_batch : current_batch);
201
-
202
197
  // Don't update the <select> if it is focused. We can get here if, for example,
203
198
  // an update is deferred because of async work depending on the select:
204
199
  //
205
200
  // <select bind:value={selected}>...</select>
206
201
  // <p>{await find(selected)}</p>
207
- if (batches.has(batch)) {
202
+ if (batches.has(/** @type {Batch} */ (view_batch))) {
208
203
  return;
209
204
  }
210
205
  }
@@ -18,6 +18,9 @@ import { without_reactive_context } from './bindings/shared.js';
18
18
  */
19
19
  export const event_symbol = Symbol('events');
20
20
 
21
+ // The native listener boundary at which a delegated descendant stopped propagation.
22
+ const propagation_stopped_at = Symbol('propagation stopped at');
23
+
21
24
  /** @type {Set<string>} */
22
25
  export const all_registered_events = new Set();
23
26
 
@@ -62,7 +65,10 @@ export function create_event(event_name, dom, handler, options = {}) {
62
65
  // Only call in the bubble phase, else delegated events would be called before the capturing events
63
66
  handle_event_propagation.call(dom, event);
64
67
  }
65
- if (!event.cancelBubble) {
68
+ // Native stopPropagation() does not stop other listeners on the same element.
69
+ // Only skip this callback if a delegated descendant stopped before reaching it.
70
+ // @ts-expect-error
71
+ if (options.capture || event[propagation_stopped_at] !== dom) {
66
72
  return without_reactive_context(() => {
67
73
  return handler?.call(this, event);
68
74
  });
@@ -180,6 +186,19 @@ let last_propagated_event_clear_scheduled = false;
180
186
  */
181
187
  export function handle_event_propagation(event) {
182
188
  var handler_element = this;
189
+ var was_stopped = event.cancelBubble;
190
+
191
+ // Another listener at this boundary may already have dispatched the delegated
192
+ // handler that stopped propagation. Do not dispatch it again.
193
+ // @ts-expect-error
194
+ if (was_stopped && event[propagation_stopped_at] === handler_element) return;
195
+
196
+ if (!was_stopped) {
197
+ // The same event object may be dispatched again.
198
+ // @ts-expect-error
199
+ delete event[propagation_stopped_at];
200
+ }
201
+
183
202
  var owner_document = /** @type {Node} */ (handler_element).ownerDocument;
184
203
  var event_name = event.type;
185
204
  var path = event.composedPath?.() || [];
@@ -302,7 +321,13 @@ export function handle_event_propagation(event) {
302
321
  throw_error = error;
303
322
  }
304
323
  }
305
- if (event.cancelBubble) break;
324
+ if (event.cancelBubble) {
325
+ if (!was_stopped) {
326
+ // @ts-expect-error
327
+ event[propagation_stopped_at] = handler_element;
328
+ }
329
+ break;
330
+ }
306
331
 
307
332
  path_idx++;
308
333
  current_target = path_idx < path.length ? /** @type {Element} */ (path[path_idx]) : null;
@@ -13,7 +13,7 @@ import {
13
13
  TEXT_CACHE,
14
14
  TEXT_NODE
15
15
  } from '#client/constants';
16
- import { eager_block_effects } from '../reactivity/batch.js';
16
+ import { eager_block_writes } from '../reactivity/batch.js';
17
17
  import { NAMESPACE_HTML } from '../../../constants.js';
18
18
 
19
19
  // export these for reference in the compiled code, making global name deduplication unnecessary
@@ -245,8 +245,7 @@ export function clear_text_content(node) {
245
245
  * current `<svelte:boundary>`
246
246
  */
247
247
  export function should_defer_append() {
248
- if (!async_mode_flag) return false;
249
- if (eager_block_effects !== null) return false;
248
+ if (!async_mode_flag && eager_block_writes === null) return false;
250
249
 
251
250
  var flags = /** @type {Effect} */ (active_effect).f;
252
251
  return (flags & REACTION_RAN) !== 0;