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,5 +1,6 @@
1
- /** @import { Blocker, Effect, Source, Value } from '#client' */
2
- import { DESTROYED, STALE_REACTION } from '#client/constants';
1
+ /** @import { Effect, Source, Value } from '#client' */
2
+ /** @import { Batch } from './batch.js' */
3
+ import { DESTROYED, EFFECT_TRANSPARENT, STALE_REACTION } from '#client/constants';
3
4
  import { DEV } from 'esm-env';
4
5
  import {
5
6
  component_context,
@@ -12,18 +13,20 @@ import { invoke_error_boundary } from '../error-handling.js';
12
13
  import {
13
14
  active_effect,
14
15
  active_reaction,
16
+ get,
15
17
  set_active_effect,
16
18
  set_active_reaction
17
19
  } from '../runtime.js';
18
- import { Batch, current_batch } from './batch.js';
20
+ import { current_batch } from './batch.js';
19
21
  import {
20
- async_derived,
21
22
  reactivity_loss_tracker,
22
23
  derived,
23
24
  derived_safe_equal,
24
- set_reactivity_loss_tracker
25
+ set_reactivity_loss_tracker,
26
+ async_expression,
27
+ is_uninitialized
25
28
  } from './deriveds.js';
26
- import { aborted } from './effects.js';
29
+ import { async_effect, block, branch } from './effects.js';
27
30
 
28
31
  /**
29
32
  * @param {Blocker[]} blockers
@@ -32,12 +35,8 @@ import { aborted } from './effects.js';
32
35
  * @param {(values: Value[]) => any} fn
33
36
  */
34
37
  export function flatten(blockers, sync, async, fn) {
35
- const d = is_runes() ? derived : derived_safe_equal;
36
-
37
- // Filter out already-settled blockers - no need to wait for them
38
- var pending = blockers.filter((b) => !b.settled);
39
-
40
- var deriveds = sync.map(d);
38
+ var parent = /** @type {Effect} */ (active_effect);
39
+ var deriveds = sync.map(is_runes() ? derived : derived_safe_equal);
41
40
 
42
41
  if (DEV) {
43
42
  deriveds.forEach((d, i) => {
@@ -51,69 +50,67 @@ export function flatten(blockers, sync, async, fn) {
51
50
  });
52
51
  }
53
52
 
54
- if (async.length === 0 && pending.length === 0) {
55
- fn(deriveds);
56
- return;
57
- }
58
-
59
- var parent = /** @type {Effect} */ (active_effect);
53
+ // Filter out already-settled blockers - no need to wait for them
54
+ var pending = blockers.filter((b) => !b.settled);
60
55
 
61
- var restore = capture();
62
- var blocker_promise =
63
- pending.length === 1
64
- ? pending[0].promise
65
- : pending.length > 1
66
- ? Promise.all(pending.map((b) => b.promise))
67
- : null;
56
+ if (pending.length > 0) {
57
+ var restore = capture();
58
+ var decrement_pending = increment_pending();
59
+ var p = pending.length === 1 ? pending[0].promise : Promise.all(pending.map((b) => b.promise));
68
60
 
69
- /**
70
- * @param {Source[]} async
71
- */
72
- function finish(async) {
73
- if ((parent.f & DESTROYED) !== 0) {
74
- return;
75
- }
61
+ p.then(() => {
62
+ if ((parent.f & DESTROYED) !== 0) {
63
+ return;
64
+ }
76
65
 
77
- restore();
66
+ // Keep the caller's context, but use the script's batch to read its first values.
67
+ restore(pending[0].run.get_batch());
78
68
 
79
- try {
80
- fn([...deriveds, ...async]);
81
- } catch (error) {
82
- invoke_error_boundary(error, parent);
83
- }
69
+ try {
70
+ run();
71
+ } catch (error) {
72
+ invoke_error_boundary(error, parent);
73
+ }
84
74
 
85
- unset_context();
75
+ unset_context();
76
+ }).finally(decrement_pending);
77
+ } else {
78
+ run();
86
79
  }
87
80
 
88
- var decrement_pending = increment_pending();
81
+ function run() {
82
+ if (blockers.some((b) => b.failed)) return;
89
83
 
90
- // Fast path: blockers but no async expressions
91
- if (async.length === 0) {
92
- /** @type {Promise<any>} */ (blocker_promise).then(() => finish([])).finally(decrement_pending);
93
- return;
94
- }
84
+ if (async.length === 0) {
85
+ fn(deriveds);
86
+ return;
87
+ }
95
88
 
96
- // Full path: has async expressions
97
- function run() {
98
- Promise.all(async.map((expression) => async_derived(expression)))
99
- .then(finish)
100
- .catch((error) => invoke_error_boundary(error, parent))
101
- .finally(decrement_pending);
102
- }
89
+ var signals = async.map((fn) => async_expression(fn));
103
90
 
104
- if (blocker_promise) {
105
- blocker_promise.then(() => {
106
- if ((parent.f & DESTROYED) !== 0) {
107
- decrement_pending();
108
- return;
91
+ /** @type {Effect | null} */
92
+ var effect = null;
93
+
94
+ block(() => {
95
+ for (var s of signals) {
96
+ if (is_uninitialized(s)) {
97
+ get(s);
98
+ return;
99
+ }
109
100
  }
110
101
 
111
- restore();
112
- run();
113
- unset_context();
114
- });
115
- } else {
116
- run();
102
+ if (effect !== null) {
103
+ /** @type {Batch} */ (current_batch).revalidate_branch(effect);
104
+ } else {
105
+ effect = branch(() => {
106
+ try {
107
+ fn([...deriveds, ...signals]);
108
+ } catch (error) {
109
+ invoke_error_boundary(error, parent);
110
+ }
111
+ });
112
+ }
113
+ }, EFFECT_TRANSPARENT);
117
114
  }
118
115
  }
119
116
 
@@ -140,16 +137,19 @@ export function capture() {
140
137
  var previous_dev_stack = dev_stack;
141
138
  }
142
139
 
143
- return function restore(activate_batch = true) {
140
+ /**
141
+ * @param {Batch | null} [batch] the batch to continue in, if not the one that was active before
142
+ */
143
+ return function restore(batch = previous_batch) {
144
144
  set_active_effect(previous_effect);
145
145
  set_active_reaction(previous_reaction);
146
146
  set_component_context(previous_component_context);
147
147
 
148
- if (activate_batch && (previous_effect.f & DESTROYED) === 0) {
149
- // TODO we only need optional chaining here because `{#await ...}` blocks
148
+ if (batch !== null && (previous_effect.f & DESTROYED) === 0) {
149
+ // A batch that was merged into another one continues as that one.
150
+ // TODO we only need the check here because `{#await ...}` blocks
150
151
  // are anomalous. Once we retire them we can get rid of it
151
- previous_batch?.activate();
152
- previous_batch?.apply();
152
+ merge_target(batch).activate();
153
153
  }
154
154
 
155
155
  if (DEV) {
@@ -306,78 +306,184 @@ export function unset_context(deactivate_batch = true) {
306
306
  }
307
307
 
308
308
  /**
309
- * @param {Array<() => void | Promise<void>>} thunks
309
+ * The script's state while a `run` step executes.
310
+ * Async expressions set `batch` when they deliver their first value. `continued` holds callbacks
311
+ * to run after the script resumes, and `signals` lists the expressions in creation order.
312
+ * @type {Runner | null}
310
313
  */
311
- export function run(thunks) {
312
- const restore = capture();
314
+ export let current_runner = null;
313
315
 
314
- const decrement_pending = increment_pending();
316
+ class Runner {
317
+ /** @type {ReturnType<typeof capture>} */
318
+ #restore;
315
319
 
316
- var active = /** @type {Effect} */ (active_effect);
320
+ /**
321
+ * Other batches that share this run
322
+ * @type {Set<Batch>}
323
+ */
324
+ #adopters = new Set();
317
325
 
318
- /** @type {null | { error: any }} */
319
- var errored = null;
326
+ /** @type {Batch} */
327
+ #batch;
320
328
 
321
- /** @param {any} error */
322
- const handle_error = (error) => {
323
- errored = { error }; // wrap in object in case a promise rejects with a falsy value
329
+ /** @type {Array<() => void>} */
330
+ continued = [];
324
331
 
325
- if (!aborted(active)) {
326
- invoke_error_boundary(error, active);
327
- }
328
- };
332
+ /**
333
+ * Async deriveds created during this run
334
+ * @type {Source[]}
335
+ */
336
+ signals = [];
329
337
 
330
- var promise = Promise.resolve(thunks[0]()).catch(handle_error);
338
+ /** Whether the full initialization promise chain is still pending. */
339
+ running = true;
331
340
 
332
- /** @type {Blocker} */
333
- var blocker = { promise, settled: false };
334
- var blockers = [blocker];
341
+ constructor() {
342
+ // TODO this should be unnecessary! these should just be class initializers. but
343
+ // something somewhere (vitest?) must be doing some transpilation that breaks classes
344
+ this.#batch = /** @type {Batch} */ (current_batch);
345
+ this.#restore = capture();
346
+ }
335
347
 
336
- promise.finally(() => {
337
- blocker.settled = true;
338
- unset_context();
339
- });
348
+ /** The batch to continue in, even if the original fork was discarded. */
349
+ get_batch() {
350
+ if (!merge_target(this.#batch).linked) {
351
+ for (const b of this.#adopters) {
352
+ var target = merge_target(b);
353
+ if (target.linked) this.#batch = target;
354
+ }
355
+ }
340
356
 
341
- for (const fn of thunks.slice(1)) {
342
- promise = promise
343
- .then(() => {
344
- restore();
357
+ return this.#batch;
358
+ }
345
359
 
346
- try {
347
- if (errored) {
348
- throw errored.error;
349
- }
360
+ restore() {
361
+ this.#restore(this.get_batch());
362
+ }
350
363
 
351
- if (aborted(active)) {
352
- throw STALE_REACTION;
353
- }
364
+ /**
365
+ *
366
+ * @param {Array<() => void | Promise<void>>} thunks
367
+ */
368
+ run(thunks) {
369
+ var active = /** @type {Effect} */ (active_effect);
370
+
371
+ var promise = Promise.resolve(this.#invoke(thunks[0]));
372
+ var blockers = [new Blocker(this, promise)];
373
+
374
+ for (const fn of thunks.slice(1)) {
375
+ promise = promise.then(() => {
376
+ if ((active.f & DESTROYED) !== 0) {
377
+ throw STALE_REACTION;
378
+ }
354
379
 
355
- return fn();
380
+ try {
381
+ this.restore();
382
+ return this.#invoke(fn);
356
383
  } finally {
357
384
  // We gotta unset context directly in case the function returns a promise, in which case
358
- // unset_context in .finally() would be too late ...
385
+ // unset_context in .finally() would be too late
359
386
  unset_context();
387
+ // Release buffered results now that the script has used the previous step's first values.
388
+ this.#continue();
389
+ }
390
+ });
391
+
392
+ blockers.push(new Blocker(this, promise));
393
+ }
394
+
395
+ promise = promise
396
+ .catch((error) => {
397
+ if ((active.f & DESTROYED) === 0) {
398
+ this.restore();
399
+
400
+ try {
401
+ invoke_error_boundary(error, active);
402
+ } finally {
403
+ unset_context();
404
+ }
360
405
  }
361
406
  })
362
- .catch(handle_error);
407
+ // The last step has no following step to release its buffered results.
408
+ .finally(() => this.#continue());
363
409
 
364
- const blocker = { promise, settled: false };
365
- blockers.push(blocker);
410
+ async_effect(() => {
411
+ // Inside an effect so later adopting branches can rerun this
412
+ var batch = /** @type {Batch} */ (current_batch);
413
+ this.#adopters.add(batch);
414
+ var decrement_pending = increment_pending();
366
415
 
367
- promise.finally(() => {
368
- blocker.settled = true;
369
- // ... but we also need it after such a promise has resolved in case it restores our context
370
- unset_context();
416
+ promise.finally(() => {
417
+ // An adopting batch must also run effects created after it adopted the script's branch.
418
+ var target = merge_target(batch);
419
+
420
+ if (target.linked && (active.f & DESTROYED) === 0) {
421
+ target.revalidate_branch(active);
422
+ }
423
+
424
+ decrement_pending();
425
+
426
+ this.running = false;
427
+ });
371
428
  });
429
+
430
+ return blockers;
431
+ }
432
+
433
+ /**
434
+ * Switch away from the previous batch (which is now obsolete)
435
+ * to another one
436
+ * @param {Batch} batch
437
+ */
438
+ switch(batch) {
439
+ this.#batch = batch;
440
+ }
441
+
442
+ #continue() {
443
+ for (const fn of this.continued.splice(0)) fn();
444
+ }
445
+
446
+ /**
447
+ * @param {() => void | Promise<void>} fn
448
+ */
449
+ #invoke(fn) {
450
+ var previous_runner = current_runner;
451
+ current_runner = this;
452
+
453
+ try {
454
+ return fn();
455
+ } finally {
456
+ current_runner = previous_runner;
457
+ }
372
458
  }
459
+ }
373
460
 
374
- promise
375
- // wait one more tick, so that template effects are
376
- // guaranteed to run before `$effect(...)`
377
- .then(() => Promise.resolve())
378
- .finally(decrement_pending);
461
+ export class Blocker {
462
+ settled = false;
463
+ failed = false;
464
+
465
+ /**
466
+ * @param {Runner} run
467
+ * @param {Promise<any>} promise
468
+ */
469
+ constructor(run, promise) {
470
+ this.run = run;
471
+ this.promise = promise
472
+ .catch(() => {
473
+ this.failed = true;
474
+ })
475
+ .finally(() => {
476
+ this.settled = true;
477
+ unset_context();
478
+ });
479
+ }
480
+ }
379
481
 
380
- return blockers;
482
+ /**
483
+ * @param {Array<() => void | Promise<void>>} thunks
484
+ */
485
+ export function run(thunks) {
486
+ return new Runner().run(thunks);
381
487
  }
382
488
 
383
489
  /**
@@ -388,19 +494,39 @@ export function wait(blockers) {
388
494
  }
389
495
 
390
496
  /**
391
- * @returns {(skip?: boolean) => void}
497
+ * Adds pending work to the current batch, and to the boundary of the active effect. If the batch is merged
498
+ * into another one before the work is done, it's that batch's from then on (see `Batch#merge`)
499
+ * @returns {() => void}
392
500
  */
393
501
  export function increment_pending() {
394
502
  var effect = /** @type {Effect} */ (active_effect);
395
503
  var boundary = effect.b; // undefined if called outside the render tree, e.g. a standalone $effect.root
396
- var batch = /** @type {Batch} */ (current_batch);
397
- var blocking = !!boundary?.is_rendered();
504
+ var batch = merge_target(/** @type {Batch} */ (current_batch));
398
505
 
399
- boundary?.update_pending_count(1, batch);
400
- batch.increment(blocking, effect);
506
+ /** The nearest boundary with a pending snippet, if there is one */
507
+ var pending = boundary;
508
+
509
+ while (pending && !pending.is_pending) {
510
+ pending = pending.parent;
511
+ }
512
+
513
+ batch.increment(effect);
514
+ pending?.increment(effect, batch);
515
+ boundary?.update_effect_pending(1);
401
516
 
402
517
  return () => {
403
- boundary?.update_pending_count(-1, batch);
404
- batch.decrement(blocking, effect);
518
+ merge_target(batch).decrement(effect);
519
+ // The boundary keeps counts under the originating batch, even if it was merged.
520
+ pending?.decrement(effect, batch);
521
+ boundary?.update_effect_pending(-1);
405
522
  };
406
523
  }
524
+
525
+ /**
526
+ * The batch that `batch` was merged into, if any (transitively), else `batch` itself
527
+ * @param {Batch} batch
528
+ */
529
+ export function merge_target(batch) {
530
+ while (batch.merged !== null) batch = batch.merged;
531
+ return batch;
532
+ }