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
@@ -1,18 +1,11 @@
1
- /** @import { Derived, Effect } from '#client' */
1
+ /** @import { Effect } from '#client' */
2
2
  /** @import { Boundary } from './dom/blocks/boundary.js' */
3
3
  import { DEV } from 'esm-env';
4
4
  import { FILENAME, HYDRATION_ERROR } from '../../constants.js';
5
5
  import { is_firefox } from './dom/operations.js';
6
- import {
7
- ERROR_VALUE,
8
- BOUNDARY_EFFECT,
9
- REACTION_RAN,
10
- EFFECT,
11
- DESTROYED,
12
- DESTROYING
13
- } from './constants.js';
6
+ import { BOUNDARY_EFFECT, REACTION_RAN, EFFECT, DESTROYED, DESTROYING } from './constants.js';
14
7
  import { define_property, get_descriptor } from '../shared/utils.js';
15
- import { active_effect, active_reaction } from './runtime.js';
8
+ import { active_effect } from './runtime.js';
16
9
 
17
10
  const adjustments = new WeakMap();
18
11
 
@@ -22,9 +15,8 @@ const adjustments = new WeakMap();
22
15
  export function handle_error(error) {
23
16
  var effect = active_effect;
24
17
 
25
- // for unowned deriveds, don't throw until we read the value
18
+ // for unowned deriveds, don't throw until we read the value (the error becomes their value)
26
19
  if (effect === null) {
27
- /** @type {Derived} */ (active_reaction).f |= ERROR_VALUE;
28
20
  return error;
29
21
  }
30
22
 
@@ -5,12 +5,12 @@ import { DEV } from 'esm-env';
5
5
  export * from '../shared/errors.js';
6
6
 
7
7
  /**
8
- * Cannot create a `$derived(...)` with an `await` expression outside of an effect tree
8
+ * Can only create a `$derived(...)` with an `await` expression during component initialization or in a declaration tag
9
9
  * @returns {never}
10
10
  */
11
11
  export function async_derived_orphan() {
12
12
  if (DEV) {
13
- const error = new Error(`async_derived_orphan\nCannot create a \`$derived(...)\` with an \`await\` expression outside of an effect tree\nhttps://svelte.dev/e/async_derived_orphan`);
13
+ const error = new Error(`async_derived_orphan\nCan only create a \`$derived(...)\` with an \`await\` expression during component initialization or in a declaration tag\nhttps://svelte.dev/e/async_derived_orphan`);
14
14
 
15
15
  error.name = 'Svelte error';
16
16
 
@@ -296,6 +296,22 @@ export function fork_timing() {
296
296
  }
297
297
  }
298
298
 
299
+ /**
300
+ * `getAbortSignal()` cannot be called after an `await`
301
+ * @returns {never}
302
+ */
303
+ export function get_abort_signal_after_await() {
304
+ if (DEV) {
305
+ const error = new Error(`get_abort_signal_after_await\n\`getAbortSignal()\` cannot be called after an \`await\`\nhttps://svelte.dev/e/get_abort_signal_after_await`);
306
+
307
+ error.name = 'Svelte error';
308
+
309
+ throw error;
310
+ } else {
311
+ throw new Error(`https://svelte.dev/e/get_abort_signal_after_await`);
312
+ }
313
+ }
314
+
299
315
  /**
300
316
  * `getAbortSignal()` can only be called inside an effect or derived
301
317
  * @returns {never}
@@ -119,7 +119,6 @@ export {
119
119
  derived_safe_equal
120
120
  } from './reactivity/deriveds.js';
121
121
  export {
122
- aborted,
123
122
  effect_tracking,
124
123
  effect_root,
125
124
  legacy_pre_effect,
@@ -1,6 +1,6 @@
1
- /** @import { Blocker, Effect, Source, Value } from '#client' */
1
+ /** @import { Effect, Source, Value } from '#client' */
2
2
  /** @import { Batch } from './batch.js' */
3
- import { DESTROYED, STALE_REACTION } from '#client/constants';
3
+ import { DESTROYED, EFFECT_TRANSPARENT, STALE_REACTION } from '#client/constants';
4
4
  import { DEV } from 'esm-env';
5
5
  import {
6
6
  component_context,
@@ -13,18 +13,20 @@ import { invoke_error_boundary } from '../error-handling.js';
13
13
  import {
14
14
  active_effect,
15
15
  active_reaction,
16
+ get,
16
17
  set_active_effect,
17
18
  set_active_reaction
18
19
  } from '../runtime.js';
19
20
  import { current_batch } from './batch.js';
20
21
  import {
21
- async_derived,
22
22
  reactivity_loss_tracker,
23
23
  derived,
24
24
  derived_safe_equal,
25
- set_reactivity_loss_tracker
25
+ set_reactivity_loss_tracker,
26
+ async_expression,
27
+ is_uninitialized
26
28
  } from './deriveds.js';
27
- import { aborted } from './effects.js';
29
+ import { async_effect, block, branch } from './effects.js';
28
30
 
29
31
  /**
30
32
  * @param {Blocker[]} blockers
@@ -33,12 +35,8 @@ import { aborted } from './effects.js';
33
35
  * @param {(values: Value[]) => any} fn
34
36
  */
35
37
  export function flatten(blockers, sync, async, fn) {
36
- const d = is_runes() ? derived : derived_safe_equal;
37
-
38
- // Filter out already-settled blockers - no need to wait for them
39
- var pending = blockers.filter((b) => !b.settled);
40
-
41
- var deriveds = sync.map(d);
38
+ var parent = /** @type {Effect} */ (active_effect);
39
+ var deriveds = sync.map(is_runes() ? derived : derived_safe_equal);
42
40
 
43
41
  if (DEV) {
44
42
  deriveds.forEach((d, i) => {
@@ -52,69 +50,67 @@ export function flatten(blockers, sync, async, fn) {
52
50
  });
53
51
  }
54
52
 
55
- if (async.length === 0 && pending.length === 0) {
56
- fn(deriveds);
57
- return;
58
- }
59
-
60
- 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);
61
55
 
62
- var restore = capture();
63
- var blocker_promise =
64
- pending.length === 1
65
- ? pending[0].promise
66
- : pending.length > 1
67
- ? Promise.all(pending.map((b) => b.promise))
68
- : 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));
69
60
 
70
- /**
71
- * @param {Source[]} async
72
- */
73
- function finish(async) {
74
- if ((parent.f & DESTROYED) !== 0) {
75
- return;
76
- }
61
+ p.then(() => {
62
+ if ((parent.f & DESTROYED) !== 0) {
63
+ return;
64
+ }
77
65
 
78
- 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());
79
68
 
80
- try {
81
- fn([...deriveds, ...async]);
82
- } catch (error) {
83
- invoke_error_boundary(error, parent);
84
- }
69
+ try {
70
+ run();
71
+ } catch (error) {
72
+ invoke_error_boundary(error, parent);
73
+ }
85
74
 
86
- unset_context();
75
+ unset_context();
76
+ }).finally(decrement_pending);
77
+ } else {
78
+ run();
87
79
  }
88
80
 
89
- var decrement_pending = increment_pending();
81
+ function run() {
82
+ if (blockers.some((b) => b.failed)) return;
90
83
 
91
- // Fast path: blockers but no async expressions
92
- if (async.length === 0) {
93
- /** @type {Promise<any>} */ (blocker_promise).then(() => finish([])).finally(decrement_pending);
94
- return;
95
- }
84
+ if (async.length === 0) {
85
+ fn(deriveds);
86
+ return;
87
+ }
96
88
 
97
- // Full path: has async expressions
98
- function run() {
99
- Promise.all(async.map((expression) => async_derived(expression)))
100
- .then(finish)
101
- .catch((error) => invoke_error_boundary(error, parent))
102
- .finally(decrement_pending);
103
- }
89
+ var signals = async.map((fn) => async_expression(fn));
104
90
 
105
- if (blocker_promise) {
106
- blocker_promise.then(() => {
107
- if ((parent.f & DESTROYED) !== 0) {
108
- decrement_pending();
109
- 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
+ }
110
100
  }
111
101
 
112
- restore();
113
- run();
114
- unset_context();
115
- });
116
- } else {
117
- 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);
118
114
  }
119
115
  }
120
116
 
@@ -141,16 +137,19 @@ export function capture() {
141
137
  var previous_dev_stack = dev_stack;
142
138
  }
143
139
 
144
- 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) {
145
144
  set_active_effect(previous_effect);
146
145
  set_active_reaction(previous_reaction);
147
146
  set_component_context(previous_component_context);
148
147
 
149
- if (activate_batch && (previous_effect.f & DESTROYED) === 0) {
150
- // 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
151
151
  // are anomalous. Once we retire them we can get rid of it
152
- previous_batch?.activate();
153
- previous_batch?.apply();
152
+ merge_target(batch).activate();
154
153
  }
155
154
 
156
155
  if (DEV) {
@@ -307,78 +306,184 @@ export function unset_context(deactivate_batch = true) {
307
306
  }
308
307
 
309
308
  /**
310
- * @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}
311
313
  */
312
- export function run(thunks) {
313
- const restore = capture();
314
+ export let current_runner = null;
314
315
 
315
- const decrement_pending = increment_pending();
316
+ class Runner {
317
+ /** @type {ReturnType<typeof capture>} */
318
+ #restore;
316
319
 
317
- var active = /** @type {Effect} */ (active_effect);
320
+ /**
321
+ * Other batches that share this run
322
+ * @type {Set<Batch>}
323
+ */
324
+ #adopters = new Set();
318
325
 
319
- /** @type {null | { error: any }} */
320
- var errored = null;
326
+ /** @type {Batch} */
327
+ #batch;
321
328
 
322
- /** @param {any} error */
323
- const handle_error = (error) => {
324
- errored = { error }; // wrap in object in case a promise rejects with a falsy value
329
+ /** @type {Array<() => void>} */
330
+ continued = [];
325
331
 
326
- if (!aborted(active)) {
327
- invoke_error_boundary(error, active);
328
- }
329
- };
332
+ /**
333
+ * Async deriveds created during this run
334
+ * @type {Source[]}
335
+ */
336
+ signals = [];
330
337
 
331
- var promise = Promise.resolve(thunks[0]()).catch(handle_error);
338
+ /** Whether the full initialization promise chain is still pending. */
339
+ running = true;
332
340
 
333
- /** @type {Blocker} */
334
- var blocker = { promise, settled: false };
335
- 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
+ }
336
347
 
337
- promise.finally(() => {
338
- blocker.settled = true;
339
- unset_context();
340
- });
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
+ }
341
356
 
342
- for (const fn of thunks.slice(1)) {
343
- promise = promise
344
- .then(() => {
345
- restore();
357
+ return this.#batch;
358
+ }
346
359
 
347
- try {
348
- if (errored) {
349
- throw errored.error;
350
- }
360
+ restore() {
361
+ this.#restore(this.get_batch());
362
+ }
351
363
 
352
- if (aborted(active)) {
353
- throw STALE_REACTION;
354
- }
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
+ }
355
379
 
356
- return fn();
380
+ try {
381
+ this.restore();
382
+ return this.#invoke(fn);
357
383
  } finally {
358
384
  // We gotta unset context directly in case the function returns a promise, in which case
359
- // unset_context in .finally() would be too late ...
385
+ // unset_context in .finally() would be too late
360
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
+ }
361
405
  }
362
406
  })
363
- .catch(handle_error);
407
+ // The last step has no following step to release its buffered results.
408
+ .finally(() => this.#continue());
364
409
 
365
- const blocker = { promise, settled: false };
366
- 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();
367
415
 
368
- promise.finally(() => {
369
- blocker.settled = true;
370
- // ... but we also need it after such a promise has resolved in case it restores our context
371
- 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
+ });
372
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
+ }
373
458
  }
459
+ }
374
460
 
375
- promise
376
- // wait one more tick, so that template effects are
377
- // guaranteed to run before `$effect(...)`
378
- .then(() => Promise.resolve())
379
- .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
+ }
380
481
 
381
- return blockers;
482
+ /**
483
+ * @param {Array<() => void | Promise<void>>} thunks
484
+ */
485
+ export function run(thunks) {
486
+ return new Runner().run(thunks);
382
487
  }
383
488
 
384
489
  /**
@@ -389,19 +494,39 @@ export function wait(blockers) {
389
494
  }
390
495
 
391
496
  /**
392
- * @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}
393
500
  */
394
501
  export function increment_pending() {
395
502
  var effect = /** @type {Effect} */ (active_effect);
396
503
  var boundary = effect.b; // undefined if called outside the render tree, e.g. a standalone $effect.root
397
- var batch = /** @type {Batch} */ (current_batch);
398
- var blocking = !!boundary?.is_rendered();
504
+ var batch = merge_target(/** @type {Batch} */ (current_batch));
399
505
 
400
- boundary?.update_pending_count(1, batch);
401
- 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);
402
516
 
403
517
  return () => {
404
- boundary?.update_pending_count(-1, batch);
405
- 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);
406
522
  };
407
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
+ }