svelte 5.57.3 → 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 (81) hide show
  1. package/README.md +1 -1
  2. package/compiler/index.js +1 -1
  3. package/elements.d.ts +1 -2
  4. package/package.json +2 -2
  5. package/src/compiler/phases/1-parse/acorn.js +4 -14
  6. package/src/compiler/phases/1-parse/index.js +8 -1
  7. package/src/compiler/phases/1-parse/read/context.js +3 -2
  8. package/src/compiler/phases/1-parse/read/expression.js +2 -4
  9. package/src/compiler/phases/1-parse/utils/bracket.js +6 -0
  10. package/src/compiler/phases/2-analyze/css/css-prune.js +0 -8
  11. package/src/compiler/phases/2-analyze/index.js +2 -1
  12. package/src/compiler/phases/2-analyze/visitors/AwaitExpression.js +17 -15
  13. package/src/compiler/phases/3-transform/client/transform-template/index.js +27 -9
  14. package/src/compiler/phases/3-transform/client/utils.js +3 -2
  15. package/src/compiler/phases/3-transform/client/visitors/EachBlock.js +1 -1
  16. package/src/compiler/phases/3-transform/client/visitors/Fragment.js +13 -1
  17. package/src/compiler/phases/3-transform/client/visitors/HtmlTag.js +10 -2
  18. package/src/compiler/phases/3-transform/client/visitors/RegularElement.js +11 -2
  19. package/src/compiler/phases/3-transform/client/visitors/RenderTag.js +15 -7
  20. package/src/compiler/phases/3-transform/client/visitors/SlotElement.js +19 -1
  21. package/src/compiler/phases/3-transform/client/visitors/SvelteComponent.js +0 -1
  22. package/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js +1 -1
  23. package/src/compiler/phases/3-transform/client/visitors/shared/element.js +1 -1
  24. package/src/compiler/phases/3-transform/client/visitors/shared/events.js +1 -1
  25. package/src/compiler/phases/3-transform/client/visitors/shared/utils.js +1 -1
  26. package/src/compiler/phases/3-transform/server/visitors/ExpressionStatement.js +8 -0
  27. package/src/compiler/phases/3-transform/server/visitors/Fragment.js +6 -1
  28. package/src/compiler/phases/3-transform/server/visitors/TitleElement.js +1 -1
  29. package/src/compiler/phases/3-transform/server/visitors/shared/element.js +2 -1
  30. package/src/compiler/phases/3-transform/server/visitors/shared/utils.js +1 -1
  31. package/src/compiler/phases/patterns.js +0 -1
  32. package/src/compiler/print/index.js +49 -73
  33. package/src/compiler/state.js +1 -1
  34. package/src/compiler/utils/builders.js +0 -1
  35. package/src/compiler/utils/mapped_code.js +1 -12
  36. package/src/constants.js +0 -8
  37. package/src/index-client.js +3 -3
  38. package/src/internal/client/constants.js +8 -1
  39. package/src/internal/client/dev/debug.js +6 -6
  40. package/src/internal/client/dev/elements.js +25 -9
  41. package/src/internal/client/dev/tracing.js +67 -11
  42. package/src/internal/client/dom/blocks/async.js +2 -1
  43. package/src/internal/client/dom/blocks/await.js +109 -20
  44. package/src/internal/client/dom/blocks/boundary.js +107 -70
  45. package/src/internal/client/dom/blocks/branches.js +17 -23
  46. package/src/internal/client/dom/blocks/each.js +88 -13
  47. package/src/internal/client/dom/blocks/html.js +44 -10
  48. package/src/internal/client/dom/blocks/slot.js +19 -1
  49. package/src/internal/client/dom/blocks/snippet.js +13 -0
  50. package/src/internal/client/dom/blocks/svelte-component.js +1 -1
  51. package/src/internal/client/dom/blocks/svelte-element.js +2 -1
  52. package/src/internal/client/dom/elements/attributes.js +4 -3
  53. package/src/internal/client/dom/elements/bindings/input.js +19 -10
  54. package/src/internal/client/dom/elements/bindings/select.js +3 -7
  55. package/src/internal/client/dom/elements/events.js +27 -2
  56. package/src/internal/client/dom/hydration.js +4 -1
  57. package/src/internal/client/dom/operations.js +2 -3
  58. package/src/internal/client/dom/template.js +0 -1
  59. package/src/internal/client/error-handling.js +4 -12
  60. package/src/internal/client/errors.js +18 -2
  61. package/src/internal/client/index.js +0 -1
  62. package/src/internal/client/reactivity/async.js +249 -123
  63. package/src/internal/client/reactivity/batch.js +1109 -538
  64. package/src/internal/client/reactivity/deriveds.js +281 -177
  65. package/src/internal/client/reactivity/effects.js +44 -28
  66. package/src/internal/client/reactivity/equality.js +0 -9
  67. package/src/internal/client/reactivity/props.js +0 -1
  68. package/src/internal/client/reactivity/sources.js +33 -125
  69. package/src/internal/client/reactivity/status.js +1 -1
  70. package/src/internal/client/render.js +11 -0
  71. package/src/internal/client/runtime.js +925 -152
  72. package/src/internal/client/validate.js +2 -2
  73. package/src/internal/server/renderer.js +52 -118
  74. package/src/internal/shared/utils.js +12 -0
  75. package/src/motion/spring.js +1 -1
  76. package/src/motion/tweened.js +1 -1
  77. package/src/reactivity/create-subscriber.js +1 -1
  78. package/src/version.js +1 -1
  79. package/types/index.d.ts +3 -3
  80. package/types/index.d.ts.map +1 -1
  81. package/src/internal/client/reactivity/utils.js +0 -18
@@ -1,4 +1,4 @@
1
- /** @import { Source, TemplateNode } from '#client' */
1
+ /** @import { Effect, Source, TemplateNode } from '#client' */
2
2
  import { is_promise } from '../../../shared/utils.js';
3
3
  import { block } from '../../reactivity/effects.js';
4
4
  import { internal_set, mutable_source, source } from '../../reactivity/sources.js';
@@ -7,16 +7,18 @@ import {
7
7
  hydrating,
8
8
  skip_nodes,
9
9
  set_hydrate_node,
10
- set_hydrating,
11
- hydrate_node
10
+ set_hydrating
12
11
  } from '../hydration.js';
13
12
  import { queue_micro_task } from '../task.js';
13
+ import { should_defer_append } from '../operations.js';
14
14
  import { HYDRATION_START_ELSE, UNINITIALIZED } from '../../../../constants.js';
15
+ import { ASYNC, DESTROYED, EFFECT_PRESERVED } from '#client/constants';
15
16
  import { is_runes } from '../../context.js';
16
17
  import { Batch, current_batch, flushSync, is_flushing_sync } from '../../reactivity/batch.js';
17
18
  import { BranchManager } from './branches.js';
18
- import { capture, unset_context } from '../../reactivity/async.js';
19
+ import { capture, merge_target, unset_context } from '../../reactivity/async.js';
19
20
  import { DEV } from 'esm-env';
21
+ import { active_effect } from '../../runtime.js';
20
22
 
21
23
  const PENDING = 0;
22
24
  const THEN = 1;
@@ -44,6 +46,11 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
44
46
  var value = runes ? source(v) : mutable_source(v, false, false);
45
47
  var error = runes ? source(v) : mutable_source(v, false, false);
46
48
 
49
+ // like the result of an async expression, the states of a run belong to the batch
50
+ // that holds them: another batch's commit doesn't overwrite them (see `BatchValue#async`)
51
+ value.f |= ASYNC;
52
+ error.f |= ASYNC;
53
+
47
54
  if (DEV) {
48
55
  value.label = '{#await ...} value';
49
56
  error.label = '{#await ...} error';
@@ -51,11 +58,77 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
51
58
 
52
59
  var branches = new BranchManager(node);
53
60
 
61
+ /** The latest run of the block in the real world */
62
+ var current = {};
63
+
64
+ /**
65
+ * The latest run of the block in each fork that ran it. Once the fork is committed, that's a run of the real
66
+ * world (and so are its later runs), until a later run in the real world supersedes it (`null`)
67
+ * @type {Map<Batch, {} | null>}
68
+ */
69
+ var forks = new Map();
70
+
71
+ /** @param {Batch} batch */
72
+ var commit = (batch) => {
73
+ var run = forks.get(batch);
74
+ if (run) current = run;
75
+ forks.delete(batch);
76
+ };
77
+
78
+ /** @param {Batch} batch */
79
+ var discard = (batch) => {
80
+ forks.delete(batch);
81
+ };
82
+
83
+ /**
84
+ * @param {{}} run
85
+ * @param {Batch} batch
86
+ */
87
+ var is_latest = (run, batch) => {
88
+ if (forks.has(batch)) return forks.get(batch) === run;
89
+ if (run !== current) return false;
90
+
91
+ // a fork that was committed since supersedes it
92
+ for (const [b, r] of forks) {
93
+ if (!b.is_fork && r !== null) return false;
94
+ }
95
+
96
+ return true;
97
+ };
98
+
99
+ /**
100
+ * The batch that the states of a run in `batch` go into, if they aren't their own batch: the fork, so that they
101
+ * don't show before it's committed — and once it is, the batch it became, so that they land along with the rest
102
+ * of its changes while those are pending (its later runs, too: they replace the states it holds)
103
+ * @param {Batch} batch
104
+ */
105
+ var owner = (batch) => {
106
+ var target = merge_target(batch);
107
+ return forks.has(batch) && target.linked ? target : null;
108
+ };
109
+
54
110
  block(() => {
111
+ var effect = /** @type {Effect} */ (active_effect);
55
112
  var batch = /** @type {Batch} */ (current_batch);
56
113
  var input = get_input();
114
+ var run = {};
115
+
116
+ // (not the first run, which creates the block for every batch's view, see `should_defer_append`)
117
+ var fork = batch.is_fork && should_defer_append();
118
+
119
+ if (fork || forks.has(batch)) {
120
+ forks.set(batch, run);
121
+ batch.oncommit(commit);
122
+ batch.ondiscard(discard);
123
+ }
124
+
125
+ if (!fork) {
126
+ for (const b of forks.keys()) {
127
+ if (!b.is_fork && b !== batch) forks.set(b, null);
128
+ }
57
129
 
58
- var destroyed = false;
130
+ current = run;
131
+ }
59
132
 
60
133
  /** Whether or not there was a hydration mismatch. Needs to be a `let` or else it isn't treeshaken out */
61
134
  // @ts-ignore coercing `node` to a `Comment` causes TypeScript and Prettier to fight
@@ -72,15 +145,33 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
72
145
  var resolved = false;
73
146
 
74
147
  /**
75
- * @param {() => void} fn
148
+ * @param {(set: (source: Source, value: any) => void) => void} fn
76
149
  */
77
150
  const resolve = (fn) => {
78
- if (destroyed) return;
151
+ if ((effect.f & DESTROYED) !== 0 || !is_latest(run, batch)) return;
79
152
 
80
153
  resolved = true;
154
+
155
+ var target = owner(batch);
156
+
157
+ if (target !== null) {
158
+ var previous = current_batch;
159
+ restore(target);
160
+
161
+ try {
162
+ fn((source, v) => /** @type {Batch} */ (target).capture(source, v, source.equals(v)));
163
+ } finally {
164
+ unset_context(false);
165
+ target.flush();
166
+ if (previous?.linked) previous.activate();
167
+ }
168
+
169
+ return;
170
+ }
171
+
81
172
  // We don't want to restore the previous batch here; {#await} blocks don't follow the async logic
82
173
  // we have elsewhere, instead pending/resolve/fail states are each their own batch so to speak.
83
- restore(false);
174
+ restore(null);
84
175
  // ...but it might still be set here. That means a `save(...)` has restored it — but that batch will
85
176
  // likely already have been committed by the time it resolves, and this resolve should be processed
86
177
  // in a separate batch. We're not using batch.deactivate()/activate() above because get_input()
@@ -93,7 +184,7 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
93
184
  Batch.ensure();
94
185
 
95
186
  try {
96
- fn();
187
+ fn(internal_set);
97
188
  } finally {
98
189
  unset_context(false);
99
190
 
@@ -105,19 +196,19 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
105
196
 
106
197
  input.then(
107
198
  (v) => {
108
- resolve(() => {
109
- internal_set(value, v);
199
+ resolve((set) => {
200
+ set(value, v);
110
201
  branches.ensure(THEN, then_fn && ((target) => then_fn(target, value)));
111
202
  });
112
203
  },
113
204
  (e) => {
114
- resolve(() => {
115
- internal_set(error, e);
205
+ resolve((set) => {
206
+ set(error, e);
116
207
  branches.ensure(CATCH, catch_fn && ((target) => catch_fn(target, error)));
117
208
 
118
209
  if (!catch_fn) {
119
210
  // Rethrow the error if no catch block exists
120
- throw error.v;
211
+ throw e;
121
212
  }
122
213
  });
123
214
  }
@@ -137,7 +228,9 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
137
228
  });
138
229
  }
139
230
  } else {
140
- internal_set(value, input);
231
+ if (owner(batch) === batch) batch.capture(value, input, value.equals(input));
232
+ else internal_set(value, input);
233
+
141
234
  branches.ensure(THEN, then_fn && ((target) => then_fn(target, value)));
142
235
  }
143
236
 
@@ -145,9 +238,5 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
145
238
  // continue in hydration mode
146
239
  set_hydrating(true);
147
240
  }
148
-
149
- return () => {
150
- destroyed = true;
151
- };
152
- });
241
+ }, EFFECT_PRESERVED); // states are selected asynchronously: keep the effect even without dependencies
153
242
  }
@@ -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;
@@ -1,5 +1,6 @@
1
1
  /** @import { Effect, TemplateNode } from '#client' */
2
- import { Batch, current_batch } from '../../reactivity/batch.js';
2
+ /** @import { Batch } from '../../reactivity/batch.js' */
3
+ import { current_batch } from '../../reactivity/batch.js';
3
4
  import {
4
5
  branch,
5
6
  destroy_effect,
@@ -8,6 +9,7 @@ import {
8
9
  resume_effect
9
10
  } from '../../reactivity/effects.js';
10
11
  import { HMR_ANCHOR } from '../../constants.js';
12
+ import { EFFECT_OFFSCREEN } from '#client/constants';
11
13
  import { hydrate_node, hydrating } from '../hydration.js';
12
14
  import { create_text, should_defer_append } from '../operations.js';
13
15
  import { DEV } from 'esm-env';
@@ -107,26 +109,12 @@ export class BranchManager {
107
109
  // ...and append the fragment
108
110
  this.anchor.before(offscreen.fragment);
109
111
  onscreen = offscreen.effect;
112
+ onscreen.f &= ~EFFECT_OFFSCREEN;
110
113
  }
111
114
  }
112
115
 
113
- for (const [b, k] of this.#batches) {
114
- this.#batches.delete(b);
115
-
116
- if (b === batch) {
117
- // keep values for newer batches
118
- break;
119
- }
120
-
121
- const offscreen = this.#offscreen.get(k);
122
-
123
- if (offscreen) {
124
- // for older batches, destroy offscreen effects
125
- // as they will never be committed
126
- destroy_effect(offscreen.effect);
127
- this.#offscreen.delete(k);
128
- }
129
- }
116
+ // other batches can still commit (in any order), so their selections remain
117
+ this.#discard(batch);
130
118
 
131
119
  // outro/destroy all onscreen effects...
132
120
  for (const [k, effect] of this.#onscreen) {
@@ -166,7 +154,10 @@ export class BranchManager {
166
154
  * @param {Batch} batch
167
155
  */
168
156
  #discard = (batch) => {
169
- 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
+ }
170
161
 
171
162
  const keys = Array.from(this.#batches.values());
172
163
 
@@ -194,10 +185,13 @@ export class BranchManager {
194
185
 
195
186
  fragment.append(target);
196
187
 
197
- this.#offscreen.set(key, {
198
- effect: branch(() => fn(target)),
199
- fragment
200
- });
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 });
201
195
  } else {
202
196
  this.#onscreen.set(
203
197
  key,