svelte 5.58.0 → 5.59.0

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 (100) hide show
  1. package/compiler/index.js +1 -1
  2. package/package.json +19 -1
  3. package/renderer.d.ts +1 -0
  4. package/src/compiler/errors.js +19 -0
  5. package/src/compiler/index.js +37 -2
  6. package/src/compiler/migrate/index.js +2 -1
  7. package/src/compiler/phases/1-parse/read/options.js +20 -0
  8. package/src/compiler/phases/2-analyze/index.js +21 -2
  9. package/src/compiler/phases/2-analyze/visitors/AnimateDirective.js +5 -0
  10. package/src/compiler/phases/2-analyze/visitors/Attribute.js +6 -2
  11. package/src/compiler/phases/2-analyze/visitors/BindDirective.js +4 -0
  12. package/src/compiler/phases/2-analyze/visitors/ExpressionTag.js +2 -1
  13. package/src/compiler/phases/2-analyze/visitors/HtmlTag.js +5 -0
  14. package/src/compiler/phases/2-analyze/visitors/OnDirective.js +6 -0
  15. package/src/compiler/phases/2-analyze/visitors/RegularElement.js +13 -6
  16. package/src/compiler/phases/2-analyze/visitors/SvelteBody.js +5 -0
  17. package/src/compiler/phases/2-analyze/visitors/SvelteDocument.js +5 -0
  18. package/src/compiler/phases/2-analyze/visitors/SvelteElement.js +5 -1
  19. package/src/compiler/phases/2-analyze/visitors/SvelteHead.js +5 -0
  20. package/src/compiler/phases/2-analyze/visitors/SvelteWindow.js +5 -0
  21. package/src/compiler/phases/2-analyze/visitors/Text.js +3 -1
  22. package/src/compiler/phases/2-analyze/visitors/TransitionDirective.js +6 -0
  23. package/src/compiler/phases/2-analyze/visitors/shared/element.js +3 -2
  24. package/src/compiler/phases/3-transform/client/transform-client.js +68 -1
  25. package/src/compiler/phases/3-transform/client/transform-template/index.js +4 -3
  26. package/src/compiler/phases/3-transform/client/transform-template/template.js +1 -1
  27. package/src/compiler/phases/3-transform/client/utils.js +3 -2
  28. package/src/compiler/phases/3-transform/client/visitors/RegularElement.js +23 -13
  29. package/src/compiler/phases/3-transform/client/visitors/RenderTag.js +13 -1
  30. package/src/compiler/phases/3-transform/client/visitors/SnippetBlock.js +11 -1
  31. package/src/compiler/phases/3-transform/client/visitors/SvelteBoundary.js +8 -5
  32. package/src/compiler/phases/3-transform/client/visitors/shared/component.js +17 -8
  33. package/src/compiler/phases/3-transform/client/visitors/shared/element.js +4 -3
  34. package/src/compiler/phases/3-transform/client/visitors/shared/fragment.js +7 -1
  35. package/src/compiler/phases/3-transform/server/transform-server.js +14 -1
  36. package/src/compiler/phases/3-transform/server/visitors/ExpressionStatement.js +8 -0
  37. package/src/compiler/phases/3-transform/server/visitors/RegularElement.js +8 -4
  38. package/src/compiler/print/index.js +45 -57
  39. package/src/compiler/state.js +12 -0
  40. package/src/compiler/utils/builders.js +2 -2
  41. package/src/compiler/validate-options.js +47 -1
  42. package/src/index-client.js +3 -3
  43. package/src/internal/client/constants.js +15 -1
  44. package/src/internal/client/custom-renderer/containers.js +213 -0
  45. package/src/internal/client/custom-renderer/cross.js +145 -0
  46. package/src/internal/client/custom-renderer/index.js +28 -0
  47. package/src/internal/client/custom-renderer/state.js +59 -0
  48. package/src/internal/client/dev/css.js +3 -1
  49. package/src/internal/client/dev/debug.js +6 -6
  50. package/src/internal/client/dev/elements.js +9 -7
  51. package/src/internal/client/dev/tracing.js +67 -11
  52. package/src/internal/client/dev/validation.js +2 -1
  53. package/src/internal/client/dom/blocks/async.js +2 -1
  54. package/src/internal/client/dom/blocks/await.js +110 -19
  55. package/src/internal/client/dom/blocks/boundary.js +142 -88
  56. package/src/internal/client/dom/blocks/branches.js +94 -75
  57. package/src/internal/client/dom/blocks/css-props.js +31 -3
  58. package/src/internal/client/dom/blocks/each.js +166 -45
  59. package/src/internal/client/dom/blocks/html.js +19 -13
  60. package/src/internal/client/dom/blocks/slot.js +3 -3
  61. package/src/internal/client/dom/blocks/snippet.js +52 -3
  62. package/src/internal/client/dom/blocks/svelte-element.js +17 -6
  63. package/src/internal/client/dom/blocks/svelte-head.js +13 -4
  64. package/src/internal/client/dom/css.js +4 -4
  65. package/src/internal/client/dom/elements/attributes.js +91 -51
  66. package/src/internal/client/dom/elements/bindings/input.js +19 -10
  67. package/src/internal/client/dom/elements/bindings/select.js +13 -9
  68. package/src/internal/client/dom/elements/class.js +16 -5
  69. package/src/internal/client/dom/elements/events.js +153 -21
  70. package/src/internal/client/dom/elements/misc.js +3 -2
  71. package/src/internal/client/dom/elements/style.js +17 -6
  72. package/src/internal/client/dom/hydration.js +6 -6
  73. package/src/internal/client/dom/operations.js +435 -14
  74. package/src/internal/client/dom/reconciler.js +2 -2
  75. package/src/internal/client/dom/template.js +94 -66
  76. package/src/internal/client/error-handling.js +4 -12
  77. package/src/internal/client/errors.js +85 -2
  78. package/src/internal/client/index.js +9 -2
  79. package/src/internal/client/reactivity/async.js +257 -122
  80. package/src/internal/client/reactivity/batch.js +1124 -531
  81. package/src/internal/client/reactivity/deriveds.js +281 -177
  82. package/src/internal/client/reactivity/effects.js +87 -54
  83. package/src/internal/client/reactivity/sources.js +30 -124
  84. package/src/internal/client/reactivity/status.js +1 -1
  85. package/src/internal/client/render.js +60 -87
  86. package/src/internal/client/runtime.js +931 -153
  87. package/src/internal/client/validate.js +2 -2
  88. package/src/internal/disclose-version.js +1 -1
  89. package/src/internal/flags/custom-renderer-server.js +10 -0
  90. package/src/internal/flags/custom-renderer.js +3 -0
  91. package/src/internal/flags/index.js +6 -0
  92. package/src/internal/init-operations.js +3 -0
  93. package/src/internal/shared/errors.js +20 -0
  94. package/src/legacy/legacy-client.js +6 -2
  95. package/src/renderer/index-server.js +18 -0
  96. package/src/renderer/index.js +2 -0
  97. package/src/version.js +1 -1
  98. package/types/index.d.ts +245 -25
  99. package/types/index.d.ts.map +10 -1
  100. package/src/internal/client/reactivity/utils.js +0 -18
@@ -1,52 +1,51 @@
1
- /** @import { Derived, Effect, Reaction, Source, Value } from '#client' */
1
+ /** @import { BatchRecord, BatchValue, Derived, DerivedRecord, Effect, Reaction, Source, Value } from '#client' */
2
2
  /** @import { Batch } from './batch.js'; */
3
- /** @import { Boundary } from '../dom/blocks/boundary.js'; */
4
3
  import { DEV } from 'esm-env';
5
4
  import {
6
- ERROR_VALUE,
7
5
  DERIVED,
8
6
  DIRTY,
9
7
  EFFECT_PRESERVED,
10
8
  STALE_REACTION,
11
9
  ASYNC,
12
10
  DESTROYED,
13
- CLEAN,
14
- REACTION_RAN,
15
- INERT
11
+ INERT,
12
+ CONNECTED,
13
+ ASSIGNED,
14
+ ERROR_VALUE,
15
+ HAS_ABORT_CONTROLLER
16
16
  } from '#client/constants';
17
17
  import {
18
18
  active_reaction,
19
19
  active_effect,
20
20
  update_reaction,
21
- increment_write_version,
22
21
  set_active_effect,
23
22
  push_reaction_value,
24
23
  is_destroying_effect,
25
24
  update_effect,
26
25
  remove_reactions,
27
26
  skipped_deps,
28
- new_deps
27
+ new_deps,
28
+ is_current,
29
+ promote_record,
30
+ changed_since,
31
+ is_dirty,
32
+ abort_all,
33
+ abort_controller,
34
+ get
29
35
  } from '../runtime.js';
30
- import { without_reactive_context } from '../dom/elements/bindings/shared.js';
31
36
  import { equals, safe_equals } from './equality.js';
32
37
  import * as e from '../errors.js';
33
38
  import * as w from '../warnings.js';
34
- import {
35
- async_effect,
36
- destroy_effect,
37
- destroy_effect_children,
38
- effect_tracking,
39
- teardown
40
- } from './effects.js';
41
- import { eager_effects, internal_set, set_eager_effects, source } from './sources.js';
42
- import { get_error } from '../../shared/dev.js';
39
+ import { async_effect, destroy_effect, destroy_effect_children, teardown } from './effects.js';
40
+ import { eager_effects, set_eager_effects, source } from './sources.js';
41
+ import { get_error, invariant } from '../../shared/dev.js';
43
42
  import { async_mode_flag, tracing_mode_flag } from '../../flags/index.js';
44
43
  import { component_context } from '../context.js';
45
44
  import { UNINITIALIZED } from '../../../constants.js';
46
- import { batch_values, current_batch, previous_batch } from './batch.js';
47
- import { increment_pending, unset_context } from './async.js';
45
+ import { batch_values, current_batch, view_batch, view_cache } from './batch.js';
46
+ import { current_runner, increment_pending, merge_target, unset_context } from './async.js';
48
47
  import { deferred, includes, noop } from '../../shared/utils.js';
49
- import { set_signal_status, update_derived_status } from './status.js';
48
+ import { update_derived_status } from './status.js';
50
49
  import { stringify_derived } from '../dev/debug.js';
51
50
 
52
51
  /**
@@ -82,6 +81,8 @@ export function derived(fn) {
82
81
  const signal = {
83
82
  ctx: component_context,
84
83
  deps: null,
84
+ dv: null,
85
+ edges: null,
85
86
  effects: null,
86
87
  equals,
87
88
  f: flags,
@@ -108,27 +109,46 @@ export const OBSOLETE = Symbol('obsolete');
108
109
  * @param {() => V | Promise<V>} fn
109
110
  * @param {string} [label]
110
111
  * @param {string} [location] If provided, print a warning if the value is not read immediately after update
111
- * @returns {Promise<Source<V>>}
112
+ * @param {(error: unknown) => void} [onready] Called when the first result is captured; receives its error, if any
113
+ * @returns {Source<V>}
112
114
  */
113
115
  /*#__NO_SIDE_EFFECTS__*/
114
- export function async_derived(fn, label, location) {
115
- let parent = /** @type {Effect | null} */ (active_effect);
116
-
117
- if (parent === null) {
118
- e.async_derived_orphan();
119
- }
120
-
121
- var promise = /** @type {Promise<V>} */ (/** @type {unknown} */ (undefined));
116
+ export function async_expression(fn, label, location, onready) {
122
117
  var signal = source(/** @type {V} */ (UNINITIALIZED));
118
+ signal.f |= ASYNC;
123
119
 
124
120
  if (DEV) signal.label = label ?? stringify_derived(fn);
125
121
 
126
- // only suspend in async deriveds created on initialisation
127
- var should_suspend = !active_reaction;
128
-
129
122
  /** @type {Set<ReturnType<typeof deferred<V>>>} */
130
123
  var deferreds = new Set();
131
124
 
125
+ // `run` continues in the creating batch's view; other batches' results wait until then.
126
+ // A superseded or obsolete run lets another batch supply the first value.
127
+ var runner = current_runner;
128
+
129
+ /**
130
+ * The batch whose run delivers the first value (`null`: whichever comes first)
131
+ * @type {Batch | null}
132
+ */
133
+ var resolver = runner && current_batch;
134
+
135
+ /**
136
+ * Results buffered until the script continues; `null` disables buffering.
137
+ * @type {Array<() => void> | null}
138
+ */
139
+ var waiting = runner && [];
140
+
141
+ /**
142
+ * Retry buffered results, or release them once the script has continued.
143
+ * @param {boolean} [continued]
144
+ */
145
+ function flush_waiting(continued = false) {
146
+ var fns = /** @type {Array<() => void>} */ (waiting);
147
+ waiting = continued ? null : [];
148
+ if (continued) resolver = null;
149
+ for (const fn of fns) fn();
150
+ }
151
+
132
152
  async_effect(() => {
133
153
  var effect = /** @type {Effect} */ (active_effect);
134
154
 
@@ -138,22 +158,37 @@ export function async_derived(fn, label, location) {
138
158
 
139
159
  /** @type {ReturnType<typeof deferred<V>>} */
140
160
  var d = deferred();
141
- promise = d.promise;
142
161
 
143
162
  try {
163
+ var result = fn();
164
+ if (result === UNINITIALIZED) return;
165
+
144
166
  // If this code is changed at some point, make sure to still access the then property
145
167
  // of fn() to read any signals it might access, so that we track them as dependencies.
168
+ var promise = Promise.resolve(result);
169
+ // The then getter may also call `getAbortSignal()`, so capture its signal afterwards.
170
+ var abort_signal = abort_controller?.signal;
171
+
146
172
  // We call `unset_context` to undo any `save` calls that happen inside `fn()`
147
- Promise.resolve(fn())
148
- .then(d.resolve, (e) => {
149
- // if the promise was rejected by the user, via `getAbortSignal`, then
150
- // wait for a subsequent resolution instead of flushing the batch
151
- if (e !== STALE_REACTION) d.reject(e);
152
- })
173
+ promise
174
+ .then(
175
+ (value) => {
176
+ if (!abort_signal?.aborted) d.resolve(value);
177
+ },
178
+ (e) => {
179
+ // An aborted run waits for its replacement, even if the user catches the abort.
180
+ // A script awaiting another reaction's aborted promise must release its resolver:
181
+ // the eager merge includes the signal's owner, but not necessarily this waiter.
182
+ if (abort_signal?.aborted || (e === STALE_REACTION && runner === null)) {
183
+ return;
184
+ }
185
+
186
+ d.reject(e === STALE_REACTION ? OBSOLETE : e);
187
+ }
188
+ )
153
189
  .finally(unset_context);
154
190
  } catch (error) {
155
191
  d.reject(error);
156
- unset_context();
157
192
  }
158
193
 
159
194
  if (DEV) {
@@ -179,31 +214,11 @@ export function async_derived(fn, label, location) {
179
214
  }
180
215
 
181
216
  var batch = /** @type {Batch} */ (current_batch);
217
+ var decrement_pending = increment_pending();
182
218
 
183
- if (should_suspend) {
184
- // we only increment the batch's pending state for updates, not creation, otherwise
185
- // we will decrement to zero before the work that depends on this promise (e.g. a
186
- // template effect) has initialized, causing the batch to resolve prematurely
187
- if ((effect.f & REACTION_RAN) !== 0) {
188
- var decrement_pending = increment_pending();
189
- }
190
-
191
- if (
192
- // boundary can be null if the async derived is inside an $effect.root not connected to the component render tree
193
- parent.b?.is_rendered()
194
- ) {
195
- batch.async_deriveds.get(effect)?.reject(OBSOLETE);
196
- } else {
197
- // While the boundary is still showing pending, a new run supersedes all older in-flight runs
198
- // for this async expression. Cancel eagerly so resolution cannot commit stale values.
199
- for (const d of deferreds.values()) {
200
- d.reject(OBSOLETE);
201
- }
202
- }
203
-
204
- deferreds.add(d);
205
- batch.async_deriveds.set(effect, d);
206
- }
219
+ batch.async_deriveds.get(effect)?.reject(OBSOLETE);
220
+ deferreds.add(d);
221
+ batch.async_deriveds.set(effect, d);
207
222
 
208
223
  /**
209
224
  * @param {any} value
@@ -211,41 +226,76 @@ export function async_derived(fn, label, location) {
211
226
  */
212
227
  const handler = (value, error = undefined) => {
213
228
  if (DEV) {
229
+ invariant(view_batch === null, 'Batch is currently active');
230
+
214
231
  reactivity_loss_tracker = null;
215
232
  }
216
233
 
217
- decrement_pending?.();
218
- deferreds.delete(d);
219
-
220
- if (error === OBSOLETE) return;
234
+ if (waiting !== null && error !== OBSOLETE) {
235
+ resolver ??= batch;
221
236
 
222
- batch.activate();
237
+ if (merge_target(batch) !== merge_target(resolver)) {
238
+ // Recheck staleness after waiting: the batch may have rerun or been discarded.
239
+ // Merged batches remain valid while their merge target is linked.
240
+ waiting.push(() => {
241
+ var obsolete =
242
+ (effect.f & DESTROYED) !== 0 ||
243
+ (batch.linked ? batch.async_deriveds.get(effect) !== d : !merge_target(batch).linked);
223
244
 
224
- if (error) {
225
- signal.f |= ERROR_VALUE;
245
+ handler(value, obsolete ? OBSOLETE : error);
246
+ });
226
247
 
227
- // @ts-expect-error the error is the wrong type, but we don't care
228
- internal_set(signal, error);
229
- } else {
230
- if ((signal.f & ERROR_VALUE) !== 0) {
231
- signal.f ^= ERROR_VALUE;
248
+ return;
232
249
  }
250
+ }
233
251
 
234
- if (DEV && location !== undefined && !signal.equals(value)) {
235
- recent_async_deriveds.add(signal);
252
+ decrement_pending();
253
+ deferreds.delete(d);
236
254
 
237
- setTimeout(() => {
238
- if (recent_async_deriveds.has(signal) && (effect.f & DESTROYED) === 0) {
239
- w.await_waterfall(/** @type {string} */ (signal.label), location);
240
- recent_async_deriveds.delete(signal);
241
- }
242
- });
255
+ if (error) {
256
+ // If the resolver's run is obsolete, let the next result supply the first value.
257
+ // Do the same on failure if other results are pending. Try buffered results first.
258
+ // A rerun in the same batch keeps ownership.
259
+ if (
260
+ waiting !== null &&
261
+ resolver !== null &&
262
+ (deferreds.size > 0 || error === OBSOLETE) &&
263
+ merge_target(resolver) === merge_target(batch) &&
264
+ merge_target(batch).async_deriveds.get(effect) === d
265
+ ) {
266
+ resolver = null;
267
+ flush_waiting();
268
+ return;
269
+ } else if (error === OBSOLETE) {
270
+ return;
243
271
  }
272
+ }
273
+
274
+ var same = signal.equals(value) && !error && (signal.f & ERROR_VALUE) === 0;
275
+ merge_target(batch).capture(signal, error ?? value, same, !!error);
276
+
277
+ if (runner !== null) {
278
+ // Let the script consume the first value in this batch's view before releasing buffered results.
279
+ runner.switch(batch);
244
280
 
245
- internal_set(signal, value);
281
+ runner.continued.push(() => flush_waiting(true));
282
+
283
+ runner = null;
246
284
  }
247
285
 
248
- batch.deactivate();
286
+ onready?.(error);
287
+ onready = undefined;
288
+
289
+ if (DEV && location !== undefined && !signal.equals(value)) {
290
+ recent_async_deriveds.add(signal);
291
+
292
+ setTimeout(() => {
293
+ if (recent_async_deriveds.has(signal) && (effect.f & DESTROYED) === 0) {
294
+ w.await_waterfall(/** @type {string} */ (signal.label), location);
295
+ recent_async_deriveds.delete(signal);
296
+ }
297
+ });
298
+ }
249
299
  };
250
300
 
251
301
  d.promise.then(handler, (e) => handler(null, e || 'unknown'));
@@ -257,32 +307,70 @@ export function async_derived(fn, label, location) {
257
307
  }
258
308
  });
259
309
 
260
- if (DEV) {
261
- // add a flag that lets this be printed as a derived
262
- // when using `$inspect.trace()`
263
- signal.f |= ASYNC;
310
+ return signal;
311
+ }
312
+
313
+ /**
314
+ * @template V
315
+ * @param {() => V | Promise<V>} fn
316
+ * @param {string} [label]
317
+ * @param {string} [location] If provided, print a warning if the value is not read immediately after update
318
+ * @returns {Promise<Source<V>>}
319
+ */
320
+ /*#__NO_SIDE_EFFECTS__*/
321
+ export function async_derived(fn, label, location) {
322
+ if (!current_runner) {
323
+ e.async_derived_orphan();
264
324
  }
265
325
 
266
- return new Promise((fulfil) => {
267
- /** @param {Promise<V>} p */
268
- function next(p) {
269
- function go() {
270
- if (p === promise) {
271
- fulfil(signal);
272
- } else {
273
- // if the effect re-runs before the initial promise
274
- // resolves, delay resolution until we have a value
275
- next(promise);
326
+ const runner = current_runner;
327
+ const batch = /** @type {Batch} */ (current_batch);
328
+
329
+ return new Promise((fulfil, reject) => {
330
+ var source = async_expression(
331
+ () => {
332
+ var effect = /** @type {Effect} */ (active_effect);
333
+ var target = merge_target(/** @type {Batch} */ (current_batch));
334
+
335
+ if (runner.running && target !== batch && is_uninitialized(source)) {
336
+ // Check all earlier expressions: assignments can initialize them out of order.
337
+ for (var i = index - 1; i >= 0; i--) {
338
+ var s = runner.signals[i];
339
+
340
+ if (is_uninitialized(s)) {
341
+ // re-run until we have a value
342
+ get(s);
343
+
344
+ var pending = /** @type {Batch} */ (current_batch).async_deriveds;
345
+ pending.get(effect)?.reject(OBSOLETE);
346
+ pending.delete(effect);
347
+ return UNINITIALIZED;
348
+ }
349
+ }
276
350
  }
277
- }
278
351
 
279
- p.then(go, go);
280
- }
352
+ return fn();
353
+ },
354
+ label,
355
+ location,
356
+ (error) => {
357
+ if (error === undefined) fulfil(/** @type {Source<V>} */ (source));
358
+ else reject(error);
359
+ }
360
+ );
281
361
 
282
- next(promise);
362
+ var index = runner.signals.push(source) - 1;
283
363
  });
284
364
  }
285
365
 
366
+ /**
367
+ * Whether the signal of an async expression has no value yet in the active view of the world
368
+ * @param {Source} signal
369
+ */
370
+ export function is_uninitialized(signal) {
371
+ return (batch_values?.get(signal) ?? signal).v === UNINITIALIZED;
372
+ }
373
+
286
374
  /**
287
375
  * @template V
288
376
  * @param {() => V} fn
@@ -338,7 +426,6 @@ let stack = [];
338
426
  * @returns {T}
339
427
  */
340
428
  export function execute_derived(derived) {
341
- var value;
342
429
  var prev_active_effect = active_effect;
343
430
  var parent = derived.parent;
344
431
 
@@ -350,93 +437,115 @@ export function execute_derived(derived) {
350
437
  ) {
351
438
  w.derived_inert();
352
439
 
440
+ // (as if it ran — it can't change anymore)
441
+ update_derived_status(derived);
442
+
353
443
  return derived.v;
354
444
  }
355
445
 
446
+ // Check before pushing, so that a recursive call doesn't remove the outer evaluation from the stack.
447
+ if (DEV && includes.call(stack, derived)) {
448
+ e.derived_references_self();
449
+ }
450
+
356
451
  set_active_effect(parent);
357
452
 
453
+ var prev_eager_effects = eager_effects;
454
+
358
455
  if (DEV) {
359
- let prev_eager_effects = eager_effects;
360
456
  set_eager_effects(new Set());
361
- try {
362
- if (includes.call(stack, derived)) {
363
- e.derived_references_self();
364
- }
457
+ stack.push(derived);
458
+ }
365
459
 
366
- stack.push(derived);
460
+ try {
461
+ destroy_derived_effects(derived);
462
+ return update_reaction(derived);
463
+ } finally {
464
+ set_active_effect(prev_active_effect);
367
465
 
368
- destroy_derived_effects(derived);
369
- value = update_reaction(derived);
370
- } finally {
371
- set_active_effect(prev_active_effect);
466
+ if (DEV) {
372
467
  set_eager_effects(prev_eager_effects);
373
468
  stack.pop();
374
469
  }
375
- } else {
376
- try {
377
- destroy_derived_effects(derived);
378
- value = update_reaction(derived);
379
- } finally {
380
- set_active_effect(prev_active_effect);
381
- }
382
470
  }
383
-
384
- return value;
385
471
  }
386
472
 
387
473
  /**
388
- * @param {Derived} derived
474
+ * Update the global state of a derived, adopting the active batch's run if its view has
475
+ * become the real world (e.g. because a fork was committed), or re-evaluating it otherwise.
476
+ * `signal` can be a source, too, which is cheaper than checking first (this is called for every dependency that
477
+ * is checked): only reactions ever get a dirty status, so `is_dirty` returns `false` for a source right away
478
+ * @param {Value} signal
389
479
  * @returns {void}
390
480
  */
391
- export function update_derived(derived) {
392
- var value = execute_derived(derived);
393
-
394
- if (!derived.equals(value)) {
395
- derived.wv = increment_write_version();
396
-
397
- // in a fork, we don't update the underlying value, just `batch_values`.
398
- // the underlying value will be updated when the fork is committed.
399
- // otherwise, the next time we get here after a 'real world' state
400
- // change, `derived.equals` may incorrectly return `true`
401
- if (!current_batch?.is_fork || derived.deps === null) {
402
- if (current_batch !== null) {
403
- // We also write to previous_batch because if it exists, it is a sign that we're
404
- // currently in the process of flushing effects. These updates to deriveds may belong
405
- // to the previous batch, not the new one (which can already exist if an earlier
406
- // effect wrote to a source). This can cause bugs when running batch.#commit() later,
407
- // but not adding it to current_batch can, too, so we add it to both.
408
- // See https://github.com/sveltejs/svelte/pull/18117 for more details.
409
- current_batch.capture(derived, value, true);
410
- previous_batch?.capture(derived, value, true);
411
- } else {
412
- derived.v = value;
413
- }
481
+ export function update_derived(signal) {
482
+ var derived = /** @type {Derived} */ (signal);
414
483
 
415
- // deriveds without dependencies should never be recomputed
416
- if (derived.deps === null) {
417
- set_signal_status(derived, CLEAN);
418
- return;
419
- }
420
- }
421
- }
484
+ if (!is_dirty(derived)) return;
422
485
 
423
- // don't mark derived clean if we're reading it inside a
424
- // cleanup function, or it will cache a stale value
425
- if (is_destroying_effect) {
426
- return;
486
+ var records = view_batch?.records;
487
+ var record = records?.get(derived);
488
+
489
+ if (record !== undefined && is_current(record.deps, record.dv)) {
490
+ /** @type {Map<Reaction, BatchRecord>} */ (records).delete(derived);
491
+ promote_record(derived, record);
492
+ } else {
493
+ // (the result is stored by `update_dependencies`)
494
+ execute_derived(derived);
427
495
  }
496
+ }
428
497
 
429
- // During time traveling we don't want to reset the status so that
430
- // traversal of the graph in the other batches still happens
431
- if (batch_values !== null) {
432
- // only cache the value if we're in a tracking context, otherwise we won't
433
- // clear the cache in `mark_reactions` when dependencies are updated
434
- if (effect_tracking() || current_batch?.is_fork) {
435
- batch_values.set(derived, value);
436
- }
498
+ /**
499
+ * The value of `derived` as the view of the world of the active batch sees it, with its version: an
500
+ * assigned value that holds, or a run of it whose versions are what the view holds — a record in the
501
+ * batch, or the derived itself (i.e. its global state) — or else a new run. Results are cached for the
502
+ * lifetime of the view (see `view_cache`), unless the derived is disconnected, in which case nothing
503
+ * would remove them from the cache when they become stale.
504
+ * @param {Derived} derived
505
+ * @returns {Derived | DerivedRecord | BatchValue}
506
+ */
507
+ export function get_view(derived) {
508
+ var cache = /** @type {Map<Derived, Derived | DerivedRecord | BatchValue>} */ (view_cache);
509
+ var entry = cache.get(derived);
510
+
511
+ if (entry !== undefined) return entry;
512
+
513
+ var batch = /** @type {Batch} */ (view_batch);
514
+
515
+ // What the view holds for the derived is a write to it — an assignment (in this batch, or made while it commits),
516
+ // or the value from before another batch's assignment that this view undoes — or else its global state. An assigned value
517
+ // holds until one of the derived's dependencies — what its run in this view (its record in the batch, if
518
+ // any, else its global run) read — is written after it (see `changed_since`). The value from before an
519
+ // undone assignment is only used if it was assigned, too: a computed value was computed in some other
520
+ // view of the world, and may depend on writes that this view undoes
521
+ var written = /** @type {Map<Value, BatchValue>} */ (batch_values).get(derived);
522
+ var held = written ?? derived;
523
+ var assigned = written === undefined ? (derived.f & ASSIGNED) !== 0 : written.assigned;
524
+ var record = /** @type {DerivedRecord | undefined} */ (batch.get_record(derived));
525
+
526
+ if (assigned && !changed_since((record ?? derived).deps, held.wv)) {
527
+ entry = held;
528
+ } else if (record !== undefined && is_current(record.deps, record.dv)) {
529
+ entry = record;
530
+ } else if (
531
+ written === undefined &&
532
+ !assigned &&
533
+ derived.dv !== null &&
534
+ is_current(derived.deps, derived.dv)
535
+ ) {
536
+ entry = derived;
437
537
  } else {
438
- update_derived_status(derived);
538
+ execute_derived(derived);
539
+
540
+ // the run is stored in the batch, unless it is the real-world value (see `update_record`)
541
+ entry = /** @type {DerivedRecord | undefined} */ (batch.records?.get(derived)) ?? derived;
542
+ }
543
+
544
+ if ((derived.f & CONNECTED) !== 0) {
545
+ cache.set(derived, entry);
439
546
  }
547
+
548
+ return entry;
440
549
  }
441
550
 
442
551
  /**
@@ -447,14 +556,9 @@ export function freeze_derived_effects(derived) {
447
556
 
448
557
  for (const e of derived.effects) {
449
558
  // if the effect has a teardown function or abort signal, call it
450
- if (e.teardown || e.ac) {
559
+ if (e.teardown || (e.f & HAS_ABORT_CONTROLLER) !== 0) {
451
560
  e.teardown?.();
452
- if (e.ac !== null) {
453
- without_reactive_context(() => {
454
- /** @type {AbortController} */ (e.ac).abort(STALE_REACTION);
455
- e.ac = null;
456
- });
457
- }
561
+ abort_all(e);
458
562
 
459
563
  // make it a noop so it doesn't get called again if the derived
460
564
  // is unfrozen. we don't set it to `null`, because the existence
@@ -462,7 +566,7 @@ export function freeze_derived_effects(derived) {
462
566
  // effect runs again during unfreezing (but not for teardown-only effects)
463
567
  if (e.fn !== null) e.teardown = noop;
464
568
 
465
- remove_reactions(e, 0);
569
+ remove_reactions(e);
466
570
  destroy_effect_children(e);
467
571
  }
468
572
  }