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.
- package/compiler/index.js +1 -1
- package/package.json +19 -1
- package/renderer.d.ts +1 -0
- package/src/compiler/errors.js +19 -0
- package/src/compiler/index.js +37 -2
- package/src/compiler/migrate/index.js +2 -1
- package/src/compiler/phases/1-parse/read/options.js +20 -0
- package/src/compiler/phases/2-analyze/index.js +21 -2
- package/src/compiler/phases/2-analyze/visitors/AnimateDirective.js +5 -0
- package/src/compiler/phases/2-analyze/visitors/Attribute.js +6 -2
- package/src/compiler/phases/2-analyze/visitors/BindDirective.js +4 -0
- package/src/compiler/phases/2-analyze/visitors/ExpressionTag.js +2 -1
- package/src/compiler/phases/2-analyze/visitors/HtmlTag.js +5 -0
- package/src/compiler/phases/2-analyze/visitors/OnDirective.js +6 -0
- package/src/compiler/phases/2-analyze/visitors/RegularElement.js +13 -6
- package/src/compiler/phases/2-analyze/visitors/SvelteBody.js +5 -0
- package/src/compiler/phases/2-analyze/visitors/SvelteDocument.js +5 -0
- package/src/compiler/phases/2-analyze/visitors/SvelteElement.js +5 -1
- package/src/compiler/phases/2-analyze/visitors/SvelteHead.js +5 -0
- package/src/compiler/phases/2-analyze/visitors/SvelteWindow.js +5 -0
- package/src/compiler/phases/2-analyze/visitors/Text.js +3 -1
- package/src/compiler/phases/2-analyze/visitors/TransitionDirective.js +6 -0
- package/src/compiler/phases/2-analyze/visitors/shared/element.js +3 -2
- package/src/compiler/phases/3-transform/client/transform-client.js +68 -1
- package/src/compiler/phases/3-transform/client/transform-template/index.js +4 -3
- package/src/compiler/phases/3-transform/client/transform-template/template.js +1 -1
- package/src/compiler/phases/3-transform/client/utils.js +3 -2
- package/src/compiler/phases/3-transform/client/visitors/RegularElement.js +23 -13
- package/src/compiler/phases/3-transform/client/visitors/RenderTag.js +13 -1
- package/src/compiler/phases/3-transform/client/visitors/SnippetBlock.js +11 -1
- package/src/compiler/phases/3-transform/client/visitors/SvelteBoundary.js +8 -5
- package/src/compiler/phases/3-transform/client/visitors/shared/component.js +17 -8
- package/src/compiler/phases/3-transform/client/visitors/shared/element.js +4 -3
- package/src/compiler/phases/3-transform/client/visitors/shared/fragment.js +7 -1
- package/src/compiler/phases/3-transform/server/transform-server.js +14 -1
- package/src/compiler/phases/3-transform/server/visitors/ExpressionStatement.js +8 -0
- package/src/compiler/phases/3-transform/server/visitors/RegularElement.js +8 -4
- package/src/compiler/print/index.js +45 -57
- package/src/compiler/state.js +12 -0
- package/src/compiler/utils/builders.js +2 -2
- package/src/compiler/validate-options.js +47 -1
- package/src/index-client.js +3 -3
- package/src/internal/client/constants.js +15 -1
- package/src/internal/client/custom-renderer/containers.js +213 -0
- package/src/internal/client/custom-renderer/cross.js +145 -0
- package/src/internal/client/custom-renderer/index.js +28 -0
- package/src/internal/client/custom-renderer/state.js +59 -0
- package/src/internal/client/dev/css.js +3 -1
- package/src/internal/client/dev/debug.js +6 -6
- package/src/internal/client/dev/elements.js +9 -7
- package/src/internal/client/dev/tracing.js +67 -11
- package/src/internal/client/dev/validation.js +2 -1
- package/src/internal/client/dom/blocks/async.js +2 -1
- package/src/internal/client/dom/blocks/await.js +110 -19
- package/src/internal/client/dom/blocks/boundary.js +142 -88
- package/src/internal/client/dom/blocks/branches.js +94 -75
- package/src/internal/client/dom/blocks/css-props.js +31 -3
- package/src/internal/client/dom/blocks/each.js +166 -45
- package/src/internal/client/dom/blocks/html.js +19 -13
- package/src/internal/client/dom/blocks/slot.js +3 -3
- package/src/internal/client/dom/blocks/snippet.js +52 -3
- package/src/internal/client/dom/blocks/svelte-element.js +17 -6
- package/src/internal/client/dom/blocks/svelte-head.js +13 -4
- package/src/internal/client/dom/css.js +4 -4
- package/src/internal/client/dom/elements/attributes.js +91 -51
- package/src/internal/client/dom/elements/bindings/input.js +19 -10
- package/src/internal/client/dom/elements/bindings/select.js +13 -9
- package/src/internal/client/dom/elements/class.js +16 -5
- package/src/internal/client/dom/elements/events.js +153 -21
- package/src/internal/client/dom/elements/misc.js +3 -2
- package/src/internal/client/dom/elements/style.js +17 -6
- package/src/internal/client/dom/hydration.js +6 -6
- package/src/internal/client/dom/operations.js +435 -14
- package/src/internal/client/dom/reconciler.js +2 -2
- package/src/internal/client/dom/template.js +94 -66
- package/src/internal/client/error-handling.js +4 -12
- package/src/internal/client/errors.js +85 -2
- package/src/internal/client/index.js +9 -2
- package/src/internal/client/reactivity/async.js +257 -122
- package/src/internal/client/reactivity/batch.js +1124 -531
- package/src/internal/client/reactivity/deriveds.js +281 -177
- package/src/internal/client/reactivity/effects.js +87 -54
- package/src/internal/client/reactivity/sources.js +30 -124
- package/src/internal/client/reactivity/status.js +1 -1
- package/src/internal/client/render.js +60 -87
- package/src/internal/client/runtime.js +931 -153
- package/src/internal/client/validate.js +2 -2
- package/src/internal/disclose-version.js +1 -1
- package/src/internal/flags/custom-renderer-server.js +10 -0
- package/src/internal/flags/custom-renderer.js +3 -0
- package/src/internal/flags/index.js +6 -0
- package/src/internal/init-operations.js +3 -0
- package/src/internal/shared/errors.js +20 -0
- package/src/legacy/legacy-client.js +6 -2
- package/src/renderer/index-server.js +18 -0
- package/src/renderer/index.js +2 -0
- package/src/version.js +1 -1
- package/types/index.d.ts +245 -25
- package/types/index.d.ts.map +10 -1
- package/src/internal/client/reactivity/utils.js +0 -18
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** @import { Derived, Effect, Reaction, Source, Value } from '#client' */
|
|
1
|
+
/** @import { BatchRecord, BatchValue, Derived, DerivedRecord, Effect, Reaction, Source, Value } from '#client' */
|
|
2
2
|
import { DEV } from 'esm-env';
|
|
3
3
|
import { get_descriptors, get_prototype_of, includes, index_of } from '../shared/utils.js';
|
|
4
4
|
import {
|
|
@@ -21,19 +21,22 @@ import {
|
|
|
21
21
|
REACTION_IS_UPDATING,
|
|
22
22
|
STALE_REACTION,
|
|
23
23
|
ERROR_VALUE,
|
|
24
|
+
HAS_ABORT_CONTROLLER,
|
|
24
25
|
MANAGED_EFFECT,
|
|
25
|
-
REACTION_RAN
|
|
26
|
+
REACTION_RAN,
|
|
27
|
+
ASYNC,
|
|
28
|
+
ASSIGNED
|
|
26
29
|
} from './constants.js';
|
|
27
|
-
import { old_values } from './reactivity/sources.js';
|
|
28
30
|
import {
|
|
29
31
|
reactivity_loss_tracker,
|
|
30
32
|
execute_derived,
|
|
31
33
|
freeze_derived_effects,
|
|
32
34
|
recent_async_deriveds,
|
|
33
35
|
unfreeze_derived_effects,
|
|
34
|
-
update_derived
|
|
36
|
+
update_derived,
|
|
37
|
+
get_view
|
|
35
38
|
} from './reactivity/deriveds.js';
|
|
36
|
-
import { async_mode_flag, tracing_mode_flag } from '../flags/index.js';
|
|
39
|
+
import { async_mode_flag, tracing_mode_flag, custom_renderers_flag } from '../flags/index.js';
|
|
37
40
|
import { tracing_expressions } from './dev/tracing.js';
|
|
38
41
|
import { get_error } from '../shared/dev.js';
|
|
39
42
|
import {
|
|
@@ -49,24 +52,45 @@ import {
|
|
|
49
52
|
Batch,
|
|
50
53
|
batch_values,
|
|
51
54
|
current_batch,
|
|
55
|
+
first_batch,
|
|
52
56
|
flushSync,
|
|
53
|
-
|
|
54
|
-
|
|
57
|
+
push_teardown_context,
|
|
58
|
+
teardown_context,
|
|
59
|
+
view_batch,
|
|
60
|
+
view_cache
|
|
55
61
|
} from './reactivity/batch.js';
|
|
56
62
|
import { handle_error } from './error-handling.js';
|
|
57
63
|
import { UNINITIALIZED } from '../../constants.js';
|
|
58
64
|
import { captured_signals } from './legacy.js';
|
|
59
65
|
import { without_reactive_context } from './dom/elements/bindings/shared.js';
|
|
60
66
|
import { set_signal_status, update_derived_status } from './reactivity/status.js';
|
|
67
|
+
import { merge_target } from './reactivity/async.js';
|
|
68
|
+
import * as e from './errors.js';
|
|
61
69
|
import * as w from './warnings.js';
|
|
70
|
+
import { push_renderer, pop_renderer } from './custom-renderer/state.js';
|
|
62
71
|
|
|
63
72
|
export let is_destroying_effect = false;
|
|
64
73
|
|
|
65
|
-
/**
|
|
66
|
-
export
|
|
74
|
+
/** The write version when the running teardown started — later writes happened in it, and aren't reverted */
|
|
75
|
+
export let teardown_version = 0;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @param {boolean} value
|
|
79
|
+
* @param {number} [version]
|
|
80
|
+
*/
|
|
81
|
+
export function set_is_destroying_effect(value, version = write_version) {
|
|
67
82
|
is_destroying_effect = value;
|
|
83
|
+
teardown_version = version;
|
|
68
84
|
}
|
|
69
85
|
|
|
86
|
+
/**
|
|
87
|
+
* The result of the latest run of a derived in a teardown, which only the teardown sees: `update_dependencies` sets
|
|
88
|
+
* it — that's where the run's dependencies, which determine its version, are known — instead of storing the run, and
|
|
89
|
+
* `teardown_view` takes it right after `execute_derived`
|
|
90
|
+
* @type {BatchValue}
|
|
91
|
+
*/
|
|
92
|
+
let teardown_run = { v: undefined, wv: 0, e: false, assigned: false };
|
|
93
|
+
|
|
70
94
|
/** @type {null | Reaction} */
|
|
71
95
|
export let active_reaction = null;
|
|
72
96
|
|
|
@@ -126,7 +150,9 @@ export function set_untracked_writes(value) {
|
|
|
126
150
|
}
|
|
127
151
|
|
|
128
152
|
/**
|
|
129
|
-
* @type {number} Used by sources and deriveds for handling updates
|
|
153
|
+
* @type {number} Used by sources and deriveds for handling updates: every new value — whether it is
|
|
154
|
+
* the real-world (i.e. latest) one, or only exists within a batch — gets a new version. Versions
|
|
155
|
+
* identify values, they are only ever compared for equality (except for `$inspect.trace`).
|
|
130
156
|
* Version starts from 1 so that unowned deriveds differentiate between a created effect and a run one for tracing
|
|
131
157
|
**/
|
|
132
158
|
export let write_version = 1;
|
|
@@ -145,43 +171,158 @@ export function increment_write_version() {
|
|
|
145
171
|
return ++write_version;
|
|
146
172
|
}
|
|
147
173
|
|
|
174
|
+
/**
|
|
175
|
+
* The version `signal` has in the active view of the world, bringing deriveds up to date
|
|
176
|
+
* @param {Value} signal
|
|
177
|
+
*/
|
|
178
|
+
export function version_of(signal) {
|
|
179
|
+
if (batch_values === null) {
|
|
180
|
+
update_derived(signal);
|
|
181
|
+
return signal.wv;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if ((signal.f & DERIVED) !== 0) {
|
|
185
|
+
return get_view(/** @type {Derived} */ (signal)).wv;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return (batch_values.get(signal) ?? signal).wv;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Whether the values with the given versions are (still) what `deps` hold in the active view of the world.
|
|
193
|
+
* Dependencies are checked in order and deriveds are brought up to date along the way, so that we
|
|
194
|
+
* don't evaluate a derived that the reaction would no longer read (e.g. `a ? b : c` where `a` changed)
|
|
195
|
+
* @param {Value[] | null} deps
|
|
196
|
+
* @param {number[]} dv
|
|
197
|
+
*/
|
|
198
|
+
export function is_current(deps, dv) {
|
|
199
|
+
if (deps !== null) {
|
|
200
|
+
for (var i = 0; i < dv.length; i++) {
|
|
201
|
+
if (version_of(deps[i]) !== dv[i]) return false;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Whether none of `deps` has been written since the version `wv` in the active view of the world, which is
|
|
210
|
+
* how a value assigned to a derived (whose version is `wv`) holds: until one of the values read by the run
|
|
211
|
+
* that applies in the view (see `get_view`) is written after it. Comparing versions with those the
|
|
212
|
+
* dependencies had at the time of the assignment, like `is_current` does, wouldn't work: a view might undo
|
|
213
|
+
* a write the assignment saw, and a committed fork might apply one it didn't see — but both writes happened
|
|
214
|
+
* before the assignment, so they don't make it outdated.
|
|
215
|
+
*
|
|
216
|
+
* This (and its counterpart in `teardown_view`) is where versions are compared by order across views. That only works for versions of writes
|
|
217
|
+
* (including assignments), which are allocated when the write happens. A derived's version is allocated when it is
|
|
218
|
+
* evaluated, which can be after the assignment even if everything it read was written before it (e.g. because it is
|
|
219
|
+
* evaluated lazily, or again in another view). So a derived only counts as changed if, in addition, one of the
|
|
220
|
+
* values its run in the view read was written after the assignment
|
|
221
|
+
* @param {Value[] | null} deps
|
|
222
|
+
* @param {number} wv
|
|
223
|
+
* @param {Set<Derived | DerivedRecord | BatchValue> | null} [unchanged] runs that this walk found unchanged,
|
|
224
|
+
* to avoid walking a derived that is reachable by more than one path (e.g. a diamond) once per path. Only valid
|
|
225
|
+
* within one top-level call: `wv` and the view are fixed, and the walk itself doesn't write anything
|
|
226
|
+
*/
|
|
227
|
+
export function changed_since(deps, wv, unchanged = null) {
|
|
228
|
+
if (deps !== null) {
|
|
229
|
+
for (var i = 0; i < deps.length; i++) {
|
|
230
|
+
var dep = deps[i];
|
|
231
|
+
|
|
232
|
+
if ((dep.f & DERIVED) === 0) {
|
|
233
|
+
if (version_of(dep) > wv) return true;
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// the run of the derived that applies in the view, brought up to date — or a value assigned to it
|
|
238
|
+
/** @type {Derived | DerivedRecord | BatchValue} */
|
|
239
|
+
var run = /** @type {Derived} */ (dep);
|
|
240
|
+
|
|
241
|
+
if (batch_values === null) {
|
|
242
|
+
update_derived(/** @type {Derived} */ (dep));
|
|
243
|
+
} else {
|
|
244
|
+
run = get_view(/** @type {Derived} */ (dep));
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// (an equal value keeps its version, so a derived whose value didn't change since doesn't count either)
|
|
248
|
+
if (run.wv > wv) {
|
|
249
|
+
var assigned = run === dep ? (dep.f & ASSIGNED) !== 0 : !('dv' in run);
|
|
250
|
+
|
|
251
|
+
if (assigned) return true;
|
|
252
|
+
|
|
253
|
+
// this run was already found to have no writes after `wv` below it
|
|
254
|
+
if (unchanged === null) {
|
|
255
|
+
unchanged = new Set();
|
|
256
|
+
} else if (unchanged.has(run)) {
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (changed_since(/** @type {DerivedRecord} */ (run).deps, wv, unchanged)) {
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
unchanged.add(run);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* For block/async effects returns the record of the {@link view_batch} (if any), else reaction itself.
|
|
274
|
+
* @param {Reaction} reaction
|
|
275
|
+
* @returns {Reaction | BatchRecord}
|
|
276
|
+
*/
|
|
277
|
+
export function run_of(reaction) {
|
|
278
|
+
return (
|
|
279
|
+
((reaction.f & (BLOCK_EFFECT | ASYNC)) !== 0 && view_batch?.get_record(reaction)) || reaction
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
|
|
148
283
|
/**
|
|
149
284
|
* Determines whether a derived or effect is dirty.
|
|
150
|
-
* If it is MAYBE_DIRTY, will set the status to CLEAN
|
|
285
|
+
* If it is MAYBE_DIRTY (or an assigned derived) and isn't, will set the status to CLEAN.
|
|
286
|
+
* Deriveds are only checked like this in the real world — within the view of a batch,
|
|
287
|
+
* see {@link get_view}. Effects are checked against the run that applies (see `run_of`).
|
|
151
288
|
* @param {Reaction} reaction
|
|
152
289
|
* @returns {boolean}
|
|
153
290
|
*/
|
|
154
291
|
export function is_dirty(reaction) {
|
|
155
292
|
var flags = reaction.f;
|
|
156
293
|
|
|
157
|
-
if ((flags & DIRTY)
|
|
158
|
-
return
|
|
294
|
+
if ((flags & (DIRTY | MAYBE_DIRTY)) === 0) {
|
|
295
|
+
return false;
|
|
159
296
|
}
|
|
160
297
|
|
|
161
|
-
if ((flags &
|
|
162
|
-
|
|
163
|
-
|
|
298
|
+
if ((flags & ASSIGNED) !== 0) {
|
|
299
|
+
// A value assigned to a derived holds until one of its dependencies is written after it, which its flags
|
|
300
|
+
// can't tell: writes that don't change the real world (e.g. those of a fork) mark the derived, too
|
|
301
|
+
if (changed_since(reaction.deps, reaction.wv)) return true;
|
|
302
|
+
} else if ((flags & DIRTY) !== 0) {
|
|
303
|
+
return true;
|
|
304
|
+
} else {
|
|
305
|
+
var run = run_of(reaction);
|
|
306
|
+
var deps = run.deps;
|
|
307
|
+
var dv = run.dv;
|
|
164
308
|
|
|
165
|
-
|
|
166
|
-
var dependency = dependencies[i];
|
|
309
|
+
if (dv === null) return true;
|
|
167
310
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
311
|
+
if (batch_values === null) {
|
|
312
|
+
for (var i = 0, length = dv.length; i < length; i++) {
|
|
313
|
+
var dependency = /** @type {Value[]} */ (deps)[i];
|
|
314
|
+
|
|
315
|
+
update_derived(dependency);
|
|
171
316
|
|
|
172
|
-
|
|
173
|
-
return true;
|
|
317
|
+
if (dependency.wv !== dv[i]) return true;
|
|
174
318
|
}
|
|
319
|
+
} else if (!is_current(deps, dv)) {
|
|
320
|
+
return true;
|
|
175
321
|
}
|
|
322
|
+
}
|
|
176
323
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
// During time traveling we don't want to reset the status so that
|
|
180
|
-
// traversal of the graph in the other batches still happens
|
|
181
|
-
batch_values === null
|
|
182
|
-
) {
|
|
183
|
-
set_signal_status(reaction, CLEAN);
|
|
184
|
-
}
|
|
324
|
+
if ((flags & CONNECTED) !== 0) {
|
|
325
|
+
set_signal_status(reaction, CLEAN);
|
|
185
326
|
}
|
|
186
327
|
|
|
187
328
|
return false;
|
|
@@ -206,18 +347,180 @@ function schedule_possible_effect_self_invalidation(signal, effect, root = true)
|
|
|
206
347
|
if ((reaction.f & DERIVED) !== 0) {
|
|
207
348
|
schedule_possible_effect_self_invalidation(/** @type {Derived} */ (reaction), effect, false);
|
|
208
349
|
} else if (effect === reaction) {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
}
|
|
214
|
-
schedule_effect(/** @type {Effect} */ (reaction));
|
|
350
|
+
/** @type {Batch} */ (current_batch).schedule(
|
|
351
|
+
/** @type {Effect} */ (reaction),
|
|
352
|
+
root ? DIRTY : MAYBE_DIRTY
|
|
353
|
+
);
|
|
215
354
|
}
|
|
216
355
|
}
|
|
217
356
|
}
|
|
218
357
|
|
|
358
|
+
/**
|
|
359
|
+
* The controller of the signal of the run in progress, if it asked for one (see `get_abort_signal`). Once the run is
|
|
360
|
+
* done, it's stored with the run: on the reaction for its global run, or on the record a batch holds of it
|
|
361
|
+
* @type {AbortController | null}
|
|
362
|
+
*/
|
|
363
|
+
export let abort_controller = null;
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* The signal of the run of `reaction` that is in progress. Its controller is stored with the run once it's done
|
|
367
|
+
* (see `set_global_signal` and `update_record`) — `reaction.ac` is the previous global run's until then
|
|
368
|
+
* @param {Reaction} reaction
|
|
369
|
+
*/
|
|
370
|
+
export function get_abort_signal(reaction) {
|
|
371
|
+
if ((reaction.f & REACTION_IS_UPDATING) === 0) {
|
|
372
|
+
// for now, disallow calling `getAbortSignal()` after an `await`. we could
|
|
373
|
+
// potentially relax this restriction in future, but it adds complexity
|
|
374
|
+
e.get_abort_signal_after_await();
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
reaction.f |= HAS_ABORT_CONTROLLER;
|
|
378
|
+
|
|
379
|
+
return (abort_controller ??= new AbortController()).signal;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/** @param {AbortController | null | undefined} controller */
|
|
383
|
+
function abort_signal(controller) {
|
|
384
|
+
if (controller) without_reactive_context(() => controller.abort(STALE_REACTION));
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Abort the signal of a record of `reaction` that a batch replaces or drops — unless another batch holds the
|
|
389
|
+
* record, too (see `keep_global_run`), in which case it aborts once the last one lets go of it
|
|
390
|
+
* @param {Reaction} reaction
|
|
391
|
+
* @param {BatchRecord | undefined} record
|
|
392
|
+
* @param {boolean} [rerun] whether `reaction` is about to run again (see `abort_replaced`). The active batch
|
|
393
|
+
* and the batches that the new run supersedes still hold the record then, but don't count
|
|
394
|
+
*/
|
|
395
|
+
export function abort_record(reaction, record, rerun = false) {
|
|
396
|
+
if (!record?.ac) return;
|
|
397
|
+
|
|
398
|
+
for (var b = first_batch; b !== null; b = b.next) {
|
|
399
|
+
if (
|
|
400
|
+
b.records?.get(reaction) === record &&
|
|
401
|
+
!(rerun && (b === view_batch || supersedes(current_batch, b)))
|
|
402
|
+
) {
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
abort_signal(record.ac);
|
|
408
|
+
record.ac = null;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Before `reaction` runs again, abort the signals of the runs it replaces: its run in the active batch (which the
|
|
413
|
+
* new run replaces either way, see `update_record`), its global run if the new run is going to replace that one,
|
|
414
|
+
* and the runs of earlier batches that a run in a real batch supersedes (see `supersedes`). (Forks stay
|
|
415
|
+
* independent until they're committed or discarded.) Whether a derived's run in a view replaces the global run
|
|
416
|
+
* is only known once it's done (see `set_global_signal`) — unless the global run is outdated in the real world
|
|
417
|
+
* anyway
|
|
418
|
+
* @param {Reaction} reaction
|
|
419
|
+
* @param {number} flags
|
|
420
|
+
*/
|
|
421
|
+
function abort_replaced(reaction, flags) {
|
|
422
|
+
var batch = current_batch;
|
|
423
|
+
var is_derived = (flags & DERIVED) !== 0;
|
|
424
|
+
|
|
425
|
+
// a teardown's run of a derived doesn't replace anything (see `teardown_view`)
|
|
426
|
+
if (is_derived && is_destroying_effect) return;
|
|
427
|
+
|
|
428
|
+
var real = batch === null || !batch.is_fork;
|
|
429
|
+
|
|
430
|
+
abort_record(reaction, view_batch?.records?.get(reaction), true);
|
|
431
|
+
|
|
432
|
+
if (is_derived ? batch_values === null || (real && is_outdated(reaction)) : real) {
|
|
433
|
+
// (the controller stays referenced until the new run is stored, see `keep_global_run`)
|
|
434
|
+
abort_signal(reaction.ac);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
for (var b = first_batch; b !== null; b = b.next) {
|
|
438
|
+
if (supersedes(batch, b)) abort_record(reaction, b.records?.get(reaction), true);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* The run that just ended is the global state of `reaction`: its signal replaces the global run's, which aborts —
|
|
444
|
+
* unless other batches took that run over, along with its signal (see `keep_global_run`)
|
|
445
|
+
* @param {Reaction} reaction
|
|
446
|
+
*/
|
|
447
|
+
function set_global_signal(reaction) {
|
|
448
|
+
abort_signal(reaction.ac);
|
|
449
|
+
reaction.ac = abort_controller;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Whether a run is invalid or a dependency's real-world version has changed.
|
|
454
|
+
* This does not evaluate derived dependencies, which may themselves be outdated.
|
|
455
|
+
* @param {Reaction | BatchRecord} run
|
|
456
|
+
*/
|
|
457
|
+
export function is_outdated({ deps, dv }) {
|
|
458
|
+
return dv === null || dv.some((version, i) => /** @type {Value[]} */ (deps)[i].wv !== version);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Whether a run in `batch` supersedes the runs of batch `other`, so that their signals abort (see `abort_replaced`):
|
|
463
|
+
* a run in a real batch supersedes those of earlier batches whose writes it saw — because it ran in the real world,
|
|
464
|
+
* or in a view that folds them, or undoes none of their writes (e.g. because they committed, see `Batch#activate`).
|
|
465
|
+
* The runs of other earlier batches read their writes, which this run didn't see, so they still apply in their
|
|
466
|
+
* views. Forks stay independent until they're committed or discarded
|
|
467
|
+
* @param {Batch | null} batch
|
|
468
|
+
* @param {Batch} other
|
|
469
|
+
*/
|
|
470
|
+
function supersedes(batch, other) {
|
|
471
|
+
return (
|
|
472
|
+
batch !== null &&
|
|
473
|
+
!batch.is_fork &&
|
|
474
|
+
!other.is_fork &&
|
|
475
|
+
other.id < batch.id &&
|
|
476
|
+
(batch_values === null || other.previous.size === 0 || batch.folded.includes(other))
|
|
477
|
+
);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Abort all signals of `reaction` (see `getAbortSignal`), e.g. because it's destroyed.
|
|
482
|
+
* Returns whether a run had requested a signal, even if it was already aborted
|
|
483
|
+
* @param {Reaction} reaction
|
|
484
|
+
*/
|
|
485
|
+
export function abort_all(reaction) {
|
|
486
|
+
if ((reaction.f & HAS_ABORT_CONTROLLER) === 0) return false;
|
|
487
|
+
reaction.f ^= HAS_ABORT_CONTROLLER;
|
|
488
|
+
|
|
489
|
+
abort_signal(reaction.ac);
|
|
490
|
+
reaction.ac = null;
|
|
491
|
+
|
|
492
|
+
for (var b = first_batch; b !== null; b = b.next) {
|
|
493
|
+
var record = b.records?.get(reaction);
|
|
494
|
+
|
|
495
|
+
if (record?.ac) {
|
|
496
|
+
abort_signal(record.ac);
|
|
497
|
+
record.ac = null;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
return true;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Whether or not we should unconditionally record a reaction run within a batch, as opposed
|
|
506
|
+
* to updating the signal directly. True for async effects, and previously-initialized
|
|
507
|
+
* block effects (as long as they run during traversal, rather than on commit)
|
|
508
|
+
* @param {number} flags
|
|
509
|
+
*/
|
|
510
|
+
function is_recorded_effect(flags) {
|
|
511
|
+
if (
|
|
512
|
+
(flags & ASYNC) === 0 &&
|
|
513
|
+
(flags & (BLOCK_EFFECT | REACTION_RAN)) !== (BLOCK_EFFECT | REACTION_RAN)
|
|
514
|
+
) {
|
|
515
|
+
return false;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
return view_batch !== null && view_batch === current_batch;
|
|
519
|
+
}
|
|
520
|
+
|
|
219
521
|
/** @param {Reaction} reaction */
|
|
220
522
|
export function update_reaction(reaction) {
|
|
523
|
+
const batch = current_batch;
|
|
221
524
|
var previous_deps = new_deps;
|
|
222
525
|
var previous_skipped_deps = skipped_deps;
|
|
223
526
|
var previous_untracked_writes = untracked_writes;
|
|
@@ -239,12 +542,11 @@ export function update_reaction(reaction) {
|
|
|
239
542
|
untracking = false;
|
|
240
543
|
update_version = ++read_version;
|
|
241
544
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
/** @type {AbortController} */ (reaction.ac).abort(STALE_REACTION);
|
|
245
|
-
});
|
|
545
|
+
var previous_abort_controller = abort_controller;
|
|
546
|
+
abort_controller = null;
|
|
246
547
|
|
|
247
|
-
|
|
548
|
+
if ((flags & HAS_ABORT_CONTROLLER) !== 0) {
|
|
549
|
+
abort_replaced(reaction, flags);
|
|
248
550
|
}
|
|
249
551
|
|
|
250
552
|
try {
|
|
@@ -252,7 +554,7 @@ export function update_reaction(reaction) {
|
|
|
252
554
|
var fn = /** @type {Function} */ (reaction.fn);
|
|
253
555
|
var result = fn();
|
|
254
556
|
reaction.f |= REACTION_RAN;
|
|
255
|
-
|
|
557
|
+
update_dependencies(reaction, flags, result, false);
|
|
256
558
|
|
|
257
559
|
// If we're inside an effect and we have untracked writes, then we need to
|
|
258
560
|
// ensure that if any of those untracked writes result in re-invalidation
|
|
@@ -261,7 +563,8 @@ export function update_reaction(reaction) {
|
|
|
261
563
|
is_runes() &&
|
|
262
564
|
untracked_writes !== null &&
|
|
263
565
|
!untracking &&
|
|
264
|
-
|
|
566
|
+
// bail out if the effect didn't read anything
|
|
567
|
+
(new_deps !== null || skipped_deps > 0) &&
|
|
265
568
|
(reaction.f & (DERIVED | MAYBE_DIRTY | DIRTY)) === 0
|
|
266
569
|
) {
|
|
267
570
|
for (var i = 0; i < /** @type {Source[]} */ (untracked_writes).length; i++) {
|
|
@@ -302,17 +605,37 @@ export function update_reaction(reaction) {
|
|
|
302
605
|
}
|
|
303
606
|
}
|
|
304
607
|
|
|
305
|
-
if ((reaction.f & ERROR_VALUE) !== 0) {
|
|
306
|
-
reaction.f ^= ERROR_VALUE;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
608
|
return result;
|
|
310
609
|
} catch (error) {
|
|
311
|
-
|
|
312
|
-
|
|
610
|
+
var value = error;
|
|
611
|
+
|
|
612
|
+
try {
|
|
613
|
+
value = handle_error(error);
|
|
614
|
+
} finally {
|
|
615
|
+
// Still commit the deps read before the throw, otherwise deriveds connected by this run keep no reader
|
|
616
|
+
// and the reaction never re-runs when they change. For a derived, the error is the value of the run
|
|
617
|
+
// (`get` throws it) — unless an error boundary took care of it
|
|
618
|
+
update_dependencies(reaction, flags, value, value === error);
|
|
619
|
+
}
|
|
313
620
|
|
|
314
|
-
return
|
|
621
|
+
return value;
|
|
315
622
|
} finally {
|
|
623
|
+
var signal = /** @type {AbortController | null} */ (abort_controller)?.signal;
|
|
624
|
+
|
|
625
|
+
if (batch !== null && signal !== undefined) {
|
|
626
|
+
// The reaction that requested the signal identifies the merge target, even if
|
|
627
|
+
// another reaction awaits its result (e.g. `let promise = $derived(load(...))`).
|
|
628
|
+
signal.addEventListener('abort', () => {
|
|
629
|
+
if (current_batch !== null) {
|
|
630
|
+
const target = merge_target(batch);
|
|
631
|
+
|
|
632
|
+
if (target !== current_batch && target.linked && !target.is_fork) {
|
|
633
|
+
current_batch.merge_requests.add(target);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
|
|
316
639
|
reaction.f ^= REACTION_IS_UPDATING;
|
|
317
640
|
new_deps = previous_deps;
|
|
318
641
|
skipped_deps = previous_skipped_deps;
|
|
@@ -322,46 +645,435 @@ export function update_reaction(reaction) {
|
|
|
322
645
|
set_component_context(previous_component_context);
|
|
323
646
|
untracking = previous_untracking;
|
|
324
647
|
update_version = previous_update_version;
|
|
648
|
+
abort_controller = previous_abort_controller;
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/**
|
|
653
|
+
* Store the result of the current run of `reaction` where it belongs — the versions of the
|
|
654
|
+
* values it read and, for a derived, its value — and update the edges of the graph.
|
|
655
|
+
*
|
|
656
|
+
* A run belongs to the real world — and becomes the reaction's global state — unless
|
|
657
|
+
* - it is a derived that read values which aren't the latest ones (it is then stored in the batch
|
|
658
|
+
* whose view of the world it ran in, see {@link get_view})
|
|
659
|
+
* - it is an effect run that belongs to a batch (see {@link update_record})
|
|
660
|
+
* - it is a derived evaluated in a teardown, in which case only the teardown sees it (see `teardown_view`)
|
|
661
|
+
*
|
|
662
|
+
* The edges of the graph (`reaction.edges` and the `reactions` of its dependencies) are a
|
|
663
|
+
* superset of the dependencies of every live record, so that changes to any of them reach
|
|
664
|
+
* the reaction. They are only pruned when a global run happens and no batch holds a record.
|
|
665
|
+
* @param {Reaction} reaction
|
|
666
|
+
* @param {number} flags the flags of the reaction before the run
|
|
667
|
+
* @param {any} value the value of the run (only relevant for deriveds)
|
|
668
|
+
* @param {boolean} error whether the value is an error
|
|
669
|
+
*/
|
|
670
|
+
function update_dependencies(reaction, flags, value, error) {
|
|
671
|
+
var deps = reaction.deps;
|
|
672
|
+
var length = skipped_deps + (new_deps === null ? 0 : new_deps.length);
|
|
673
|
+
var dv = reaction.dv;
|
|
674
|
+
|
|
675
|
+
// an effect can be destroyed while it runs, e.g. by an error boundary
|
|
676
|
+
if ((reaction.f & DESTROYED) !== 0) {
|
|
677
|
+
abort_signal(abort_controller);
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
// ...and so can the last reaction of a derived, which aborts the derived's signals, see `remove_reaction`.
|
|
682
|
+
// `abort_all` removed the flag that the run in progress set, so its signal is still to be aborted
|
|
683
|
+
if (abort_controller !== null && (reaction.f & HAS_ABORT_CONTROLLER) === 0) {
|
|
684
|
+
abort_signal(abort_controller);
|
|
685
|
+
abort_controller = null;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
if ((flags & DERIVED) !== 0 && is_destroying_effect) {
|
|
689
|
+
// The run belongs to the teardown's view of the world (see `teardown_view`).
|
|
690
|
+
// Its value can't be more recent than what it read
|
|
691
|
+
var wv = 0;
|
|
692
|
+
|
|
693
|
+
for (var j = 0; j < length; j++) {
|
|
694
|
+
var dep =
|
|
695
|
+
j < skipped_deps
|
|
696
|
+
? /** @type {Value[]} */ (deps)[j]
|
|
697
|
+
: /** @type {Value[]} */ (new_deps)[j - skipped_deps];
|
|
698
|
+
|
|
699
|
+
wv = Math.max(wv, teardown_version_of(dep));
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
// TODO: the run's signal (if any) is never aborted — should we abort when the teardown's view of the
|
|
703
|
+
// world is done with the run?
|
|
704
|
+
teardown_run = { v: value, wv, e: error, assigned: (flags & ASSIGNED) !== 0 };
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
// A run of a derived in the real world is its new global state. Other batches may still see the current one
|
|
709
|
+
// (in a view, whether a derived's run is a global one is decided by `update_record`)
|
|
710
|
+
if (
|
|
711
|
+
(flags & DERIVED) !== 0 &&
|
|
712
|
+
batch_values === null &&
|
|
713
|
+
first_batch !== null &&
|
|
714
|
+
first_batch.next !== null
|
|
715
|
+
) {
|
|
716
|
+
keep_global_run(/** @type {Derived} */ (reaction), flags);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
// Fast path: the reaction read the same values as before, in the real world, and has no other edges
|
|
720
|
+
if (
|
|
721
|
+
new_deps === null &&
|
|
722
|
+
batch_values === null &&
|
|
723
|
+
dv !== null &&
|
|
724
|
+
length === deps?.length &&
|
|
725
|
+
reaction.edges === deps &&
|
|
726
|
+
(flags & (BLOCK_EFFECT | ASYNC)) === 0
|
|
727
|
+
) {
|
|
728
|
+
// The run saw what its dependencies hold now — a write during the run after it read
|
|
729
|
+
// the value marks it dirty (see `Batch#schedule`), so it doesn't take effect here
|
|
730
|
+
for (var i = 0; i < length; i++) dv[i] = deps[i].wv;
|
|
731
|
+
if (dv.length !== length) dv.length = length;
|
|
732
|
+
|
|
733
|
+
if ((flags & DERIVED) !== 0) set_derived_value(/** @type {Derived} */ (reaction), value, error);
|
|
734
|
+
if (reaction.ac || abort_controller) set_global_signal(reaction);
|
|
735
|
+
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
update_record(reaction, flags, length, value, error);
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/**
|
|
743
|
+
* Make `value` the (real-world) value of `derived` — it keeps its version if it is the same value
|
|
744
|
+
* @param {Derived} derived
|
|
745
|
+
* @param {any} value
|
|
746
|
+
* @param {boolean} error
|
|
747
|
+
*/
|
|
748
|
+
function set_derived_value(derived, value, error) {
|
|
749
|
+
// a run replaces a value that was assigned to the derived
|
|
750
|
+
var flags = derived.f & ~ASSIGNED;
|
|
751
|
+
|
|
752
|
+
if (error || (flags & ERROR_VALUE) !== 0 || !derived.equals(value)) {
|
|
753
|
+
derived.v = value;
|
|
754
|
+
derived.wv = increment_write_version();
|
|
755
|
+
flags = error ? flags | ERROR_VALUE : flags & ~ERROR_VALUE;
|
|
325
756
|
}
|
|
757
|
+
|
|
758
|
+
derived.f = flags;
|
|
759
|
+
update_derived_status(derived);
|
|
326
760
|
}
|
|
327
761
|
|
|
328
762
|
/**
|
|
763
|
+
* The general case of `update_dependencies`, which
|
|
764
|
+
* 1. decides where the run belongs: to the batch whose view of the world is active (as a record, see
|
|
765
|
+
* `Batch#records`), or to the real world (as the reaction's global state)
|
|
766
|
+
* 2. records the versions of the values the run read (`dv`)
|
|
767
|
+
* 3. updates the edges of the graph
|
|
768
|
+
* 4. stores the run where it belongs
|
|
329
769
|
* @param {Reaction} reaction
|
|
770
|
+
* @param {number} flags the flags of the reaction before the run
|
|
771
|
+
* @param {number} length the number of values the run read
|
|
772
|
+
* @param {any} value the value of the run (only relevant for deriveds)
|
|
773
|
+
* @param {boolean} error whether the value is an error
|
|
330
774
|
*/
|
|
331
|
-
function
|
|
775
|
+
function update_record(reaction, flags, length, value, error) {
|
|
776
|
+
// Block and async effects that run while a batch is processed produce a result for that batch (the
|
|
777
|
+
// branch it selected, the value an async expression resolved to for it), which it keeps until it
|
|
778
|
+
// commits. The first run of a block effect is the exception — it creates (and commits) its initial
|
|
779
|
+
// branch. (Whether a derived's run belongs to a batch depends on what it read, see below)
|
|
780
|
+
var should_record = is_recorded_effect(flags);
|
|
781
|
+
|
|
782
|
+
// A derived that has another version in the view than in the real world — because the view undoes an
|
|
783
|
+
// assignment to it, or it was assigned in a fork — never runs for the real world (see `get_view`)
|
|
784
|
+
if (batch_values !== null && (flags & DERIVED) !== 0) {
|
|
785
|
+
var written = batch_values.get(/** @type {Derived} */ (reaction));
|
|
786
|
+
|
|
787
|
+
if (written !== undefined && written.wv !== /** @type {Derived} */ (reaction).wv) {
|
|
788
|
+
should_record = true;
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
|
|
332
792
|
var deps = reaction.deps;
|
|
793
|
+
var read = deps;
|
|
794
|
+
if (new_deps !== null || skipped_deps !== deps?.length) {
|
|
795
|
+
read =
|
|
796
|
+
skipped_deps === 0
|
|
797
|
+
? new_deps
|
|
798
|
+
: /** @type {Value[]} */ (deps).slice(0, skipped_deps).concat(new_deps ?? []);
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
// A global run in the real world updates the versions in place. Anything else needs a new array:
|
|
802
|
+
// a record must not share it with the global state, and a derived's run can turn out to belong
|
|
803
|
+
// to the batch halfway through the loop
|
|
804
|
+
var dv = !should_record && batch_values === null && reaction.dv !== null ? reaction.dv : [];
|
|
805
|
+
|
|
806
|
+
for (var i = 0; i < length; i++) {
|
|
807
|
+
var dep = /** @type {Value[]} */ (read)[i];
|
|
808
|
+
dv[i] = seen_version(dep);
|
|
809
|
+
|
|
810
|
+
// Within a batch's view, a derived's run belongs to the batch unless everything it read is the latest
|
|
811
|
+
// value: the version it saw is the real-world one, and — for a derived — that one is up to date
|
|
812
|
+
if (
|
|
813
|
+
batch_values !== null &&
|
|
814
|
+
(flags & DERIVED) !== 0 &&
|
|
815
|
+
(dv[i] !== dep.wv || (dep.f & (DERIVED | CLEAN)) === DERIVED)
|
|
816
|
+
) {
|
|
817
|
+
should_record = true;
|
|
818
|
+
}
|
|
819
|
+
}
|
|
333
820
|
|
|
334
|
-
|
|
335
|
-
// they must remain for when fork is discarded
|
|
336
|
-
var is_fork = current_batch?.is_fork;
|
|
821
|
+
if (dv.length !== length) dv.length = length;
|
|
337
822
|
|
|
338
|
-
if (
|
|
339
|
-
|
|
823
|
+
if (!should_record && batch_values !== null && (flags & DERIVED) !== 0) {
|
|
824
|
+
keep_global_run(/** @type {Derived} */ (reaction), flags);
|
|
825
|
+
}
|
|
340
826
|
|
|
341
|
-
|
|
342
|
-
|
|
827
|
+
// The edges — `reaction.edges`, and `reaction` in the `reactions` of each of them — must cover what every
|
|
828
|
+
// live run of the reaction read (its global run, and the records batches hold of it), so that a change to
|
|
829
|
+
// any of those values reaches it. So unless this is a global run and no batch holds a record, edges are
|
|
830
|
+
// only added (and pruned by a later global run). Their order is independent of the run's reads
|
|
831
|
+
var keep = should_record || has_batch_record(reaction);
|
|
832
|
+
var tracking = effect_tracking() && (reaction.f & CONNECTED) !== 0;
|
|
833
|
+
var edges = reaction.edges;
|
|
834
|
+
|
|
835
|
+
if (keep && edges !== null) {
|
|
836
|
+
if (new_deps !== null) {
|
|
837
|
+
// the edges may be a run's `deps` as well, see `update_dependencies`
|
|
838
|
+
reaction.edges = edges = edges.slice();
|
|
839
|
+
for (dep of new_deps) {
|
|
840
|
+
if (!includes.call(edges, dep)) {
|
|
841
|
+
edges.push(dep);
|
|
842
|
+
if (tracking) (dep.reactions ??= []).push(reaction);
|
|
843
|
+
}
|
|
844
|
+
}
|
|
343
845
|
}
|
|
846
|
+
} else {
|
|
847
|
+
if (edges === deps) {
|
|
848
|
+
// The edges are exactly what the previous global run read (if anything), so — as on `main` —
|
|
849
|
+
// the values this run read again, in the same order, stay connected, and the rest is replaced
|
|
850
|
+
if (edges !== null) {
|
|
851
|
+
for (i = skipped_deps; i < edges.length; i++) remove_reaction(reaction, edges[i]);
|
|
852
|
+
}
|
|
344
853
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
for (i = 0; i < new_deps.length; i++) {
|
|
348
|
-
deps[skipped_deps + i] = new_deps[i];
|
|
854
|
+
if (tracking && new_deps !== null) {
|
|
855
|
+
for (dep of new_deps) (dep.reactions ??= []).push(reaction);
|
|
349
856
|
}
|
|
350
857
|
} else {
|
|
351
|
-
|
|
352
|
-
|
|
858
|
+
// The edges can include some that were retained for runs no batch holds anymore
|
|
859
|
+
var added = new Set(read);
|
|
860
|
+
for (dep of /** @type {Value[]} */ (edges)) {
|
|
861
|
+
if (!added.delete(dep)) remove_reaction(reaction, dep);
|
|
862
|
+
}
|
|
353
863
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
(deps[i].reactions ??= []).push(reaction);
|
|
864
|
+
if (tracking) {
|
|
865
|
+
for (dep of added) (dep.reactions ??= []).push(reaction);
|
|
357
866
|
}
|
|
358
867
|
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
deps.length = skipped_deps;
|
|
868
|
+
|
|
869
|
+
reaction.edges = read;
|
|
362
870
|
}
|
|
363
871
|
|
|
364
|
-
|
|
872
|
+
if (should_record) {
|
|
873
|
+
var batch = /** @type {Batch} */ (view_batch);
|
|
874
|
+
|
|
875
|
+
(batch.records ??= new Map()).set(
|
|
876
|
+
reaction,
|
|
877
|
+
(flags & DERIVED) !== 0
|
|
878
|
+
? {
|
|
879
|
+
deps: read,
|
|
880
|
+
dv,
|
|
881
|
+
v: value,
|
|
882
|
+
e: error,
|
|
883
|
+
wv: derived_version(/** @type {Derived} */ (reaction), batch, value, error),
|
|
884
|
+
ac: abort_controller
|
|
885
|
+
}
|
|
886
|
+
: { deps: read, dv, ac: abort_controller }
|
|
887
|
+
);
|
|
888
|
+
} else {
|
|
889
|
+
reaction.deps = read;
|
|
890
|
+
reaction.dv = dv;
|
|
891
|
+
if (reaction.ac || abort_controller) set_global_signal(reaction);
|
|
892
|
+
|
|
893
|
+
if ((flags & DERIVED) !== 0) {
|
|
894
|
+
set_derived_value(/** @type {Derived} */ (reaction), value, error);
|
|
895
|
+
|
|
896
|
+
// A record of the derived in the active batch is outdated now. (Effects never have one at this point:
|
|
897
|
+
// their runs only become global outside of processing, on their first run, or once the batch commits,
|
|
898
|
+
// whose records are promoted by then)
|
|
899
|
+
view_batch?.records?.delete(reaction);
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
/**
|
|
905
|
+
* A run of `derived` that becomes its global state replaces the current one, which other batches may still see:
|
|
906
|
+
* their views differ from the real world, and can undo the writes that made it outdated. It is kept as a record in
|
|
907
|
+
* each of them that doesn't have one, so that they don't evaluate the derived again — which would give an equal value
|
|
908
|
+
* a new version, and re-run what saw the current one (restarting async work, for example). That's also the case if
|
|
909
|
+
* the new value is equal, and so keeps its version: the derived itself would still run again, with whatever side
|
|
910
|
+
* effects it has (like asking for a new abort signal). (Like any record, it is only used where it `is_current`)
|
|
911
|
+
*
|
|
912
|
+
* If the derived uses an abort signal, the kept run's may have been aborted when the derived ran again. Only the batches
|
|
913
|
+
* that the new run supersedes (see `supersedes`) keep it regardless — it stays aborted for them until something it read
|
|
914
|
+
* changes in their view. The others (forks, later batches) evaluate the derived again.
|
|
915
|
+
*
|
|
916
|
+
* A fork's run supersedes nothing, though, so the run it replaces isn't aborted: a batch may still be waiting for it.
|
|
917
|
+
* Every other batch keeps it then, and the record takes over its signal, which aborts once no batch holds the record
|
|
918
|
+
* anymore (see `abort_record`)
|
|
919
|
+
* @param {Derived} derived
|
|
920
|
+
* @param {number} flags the flags of the derived before the run
|
|
921
|
+
*/
|
|
922
|
+
function keep_global_run(derived, flags) {
|
|
923
|
+
var dv = derived.dv;
|
|
924
|
+
|
|
925
|
+
if (
|
|
926
|
+
// no valid run, or one that doesn't depend on anything — which is only run again if it was invalidated
|
|
927
|
+
dv === null ||
|
|
928
|
+
dv.length === 0 ||
|
|
929
|
+
// an assigned value isn't a run, see `get_view`
|
|
930
|
+
(flags & ASSIGNED) !== 0
|
|
931
|
+
) {
|
|
932
|
+
return;
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
/** @type {DerivedRecord | undefined} */
|
|
936
|
+
var record;
|
|
937
|
+
|
|
938
|
+
var ac = derived.ac;
|
|
939
|
+
|
|
940
|
+
// whether the run's signal stays live, because a fork's view ran the derived (it's only aborted before the
|
|
941
|
+
// derived runs again if the run was in the real world, see `abort_replaced`)
|
|
942
|
+
var live = ac !== null && current_batch !== null && current_batch.is_fork && !ac.signal.aborted;
|
|
943
|
+
|
|
944
|
+
for (var batch = first_batch; batch !== null; batch = batch.next) {
|
|
945
|
+
if (
|
|
946
|
+
batch !== view_batch &&
|
|
947
|
+
!batch.records?.has(derived) &&
|
|
948
|
+
// otherwise, the run's signal was aborted when the derived ran again, or is about to be (see `set_global_signal`)
|
|
949
|
+
(ac === null || live || supersedes(current_batch, batch))
|
|
950
|
+
) {
|
|
951
|
+
// All batches that retain this run share one record. (Its versions are copied: the
|
|
952
|
+
// derived's next global run in the real world updates its own in place)
|
|
953
|
+
record ??= {
|
|
954
|
+
deps: derived.deps,
|
|
955
|
+
dv: dv.slice(),
|
|
956
|
+
v: derived.v,
|
|
957
|
+
wv: derived.wv,
|
|
958
|
+
e: (flags & ERROR_VALUE) !== 0
|
|
959
|
+
};
|
|
960
|
+
(batch.records ??= new Map()).set(derived, record);
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
if (live && record !== undefined) {
|
|
965
|
+
// the signal belongs to the batches that kept the run now. (If none did, `set_global_signal` aborts it)
|
|
966
|
+
record.ac = ac;
|
|
967
|
+
derived.ac = null;
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
/**
|
|
972
|
+
* The version of `signal` that the current run saw. Recording it never evaluates anything: a derived that
|
|
973
|
+
* isn't in the view's cache (anymore) was invalidated by a write during the run, or can't be cached —
|
|
974
|
+
* the run then doesn't count as having seen its current version, just like in the real world, where
|
|
975
|
+
* a derived that changes during the run gets a new version only once it is evaluated again
|
|
976
|
+
* @param {Value} signal
|
|
977
|
+
*/
|
|
978
|
+
function seen_version(signal) {
|
|
979
|
+
if (batch_values === null) return signal.wv;
|
|
980
|
+
|
|
981
|
+
if ((signal.f & DERIVED) !== 0) {
|
|
982
|
+
var cache = /** @type {Map<Derived, Derived | DerivedRecord | BatchValue>} */ (view_cache);
|
|
983
|
+
return cache.get(/** @type {Derived} */ (signal))?.wv ?? -1;
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
return (batch_values.get(signal) ?? signal).wv;
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
/**
|
|
990
|
+
* Reuse the version of an equal value in the batch's previous run, then the global value,
|
|
991
|
+
* or allocate a new one. Errors always get a new version
|
|
992
|
+
* @param {Derived} derived
|
|
993
|
+
* @param {Batch} batch
|
|
994
|
+
* @param {any} value
|
|
995
|
+
* @param {boolean} error
|
|
996
|
+
*/
|
|
997
|
+
function derived_version(derived, batch, value, error) {
|
|
998
|
+
if (!error) {
|
|
999
|
+
var previous = /** @type {DerivedRecord | undefined} */ (batch.get_record(derived));
|
|
1000
|
+
if (previous !== undefined && !previous.e && derived.equals.call(previous, value)) {
|
|
1001
|
+
return previous.wv;
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
if ((derived.f & ERROR_VALUE) === 0 && derived.equals(value)) return derived.wv;
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
return increment_write_version();
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
/**
|
|
1011
|
+
* Make the record a batch holds of `reaction` its global state, e.g. because the batch
|
|
1012
|
+
* committed, which makes its results the real-world ones (for a derived, that includes its value)
|
|
1013
|
+
* @param {Reaction} reaction
|
|
1014
|
+
* @param {BatchRecord} record
|
|
1015
|
+
*/
|
|
1016
|
+
export function promote_record(reaction, record) {
|
|
1017
|
+
reaction.deps = record.deps;
|
|
1018
|
+
// The global run updates its versions in place. An effect's record is dropped when it is promoted, but
|
|
1019
|
+
// other batches can still hold a derived's (see `keep_global_run`), so that one keeps its own
|
|
1020
|
+
reaction.dv = (reaction.f & DERIVED) !== 0 ? record.dv.slice() : record.dv;
|
|
1021
|
+
|
|
1022
|
+
if ((reaction.f & DERIVED) !== 0) {
|
|
1023
|
+
var derived = /** @type {Derived} */ (reaction);
|
|
1024
|
+
|
|
1025
|
+
derived.v = record.v;
|
|
1026
|
+
derived.wv = /** @type {number} */ (record.wv);
|
|
1027
|
+
derived.f = (record.e ? derived.f | ERROR_VALUE : derived.f & ~ERROR_VALUE) & ~ASSIGNED;
|
|
1028
|
+
update_derived_status(derived);
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
// the run's signal replaces the global run's (so that dropping the record doesn't abort it, see `Batch#release_records`)
|
|
1032
|
+
abort_signal(reaction.ac);
|
|
1033
|
+
reaction.ac = record.ac ?? null;
|
|
1034
|
+
record.ac = null;
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
/** @param {Reaction} reaction */
|
|
1038
|
+
function has_batch_record(reaction) {
|
|
1039
|
+
for (var batch = first_batch; batch !== null; batch = batch.next) {
|
|
1040
|
+
if (batch.records?.has(reaction)) return true;
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
return false;
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
/**
|
|
1047
|
+
* Remove the edges of an effect that only runs no batch holds anymore needed (because the batch committed and
|
|
1048
|
+
* its record was promoted, or because it was dropped), once no batch holds a record of it, so that its edges are
|
|
1049
|
+
* what its global run read again. `update_record` only prunes edges in a global run that happens while no batch
|
|
1050
|
+
* holds a record, which block and async effects hardly ever have: they run in batches.
|
|
1051
|
+
* (Not for deriveds: their edges also notify the reactions that read the value of a record that was dropped. They
|
|
1052
|
+
* are pruned by their next global run)
|
|
1053
|
+
* @param {Reaction} effect
|
|
1054
|
+
*/
|
|
1055
|
+
export function prune_edges(effect) {
|
|
1056
|
+
var edges = effect.edges;
|
|
1057
|
+
var deps = effect.deps;
|
|
1058
|
+
|
|
1059
|
+
if (edges === deps || has_batch_record(effect)) return;
|
|
1060
|
+
|
|
1061
|
+
// (the edges are a superset of `deps`, so nothing needs to be connected)
|
|
1062
|
+
for (var dep of /** @type {Value[]} */ (edges)) {
|
|
1063
|
+
if (deps === null || !includes.call(deps, dep)) remove_reaction(effect, dep);
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
effect.edges = deps;
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
/**
|
|
1070
|
+
* Connect a reaction outside its normal dependency update, without duplicating subscriptions
|
|
1071
|
+
* @param {Value} signal
|
|
1072
|
+
* @param {Reaction} reaction
|
|
1073
|
+
*/
|
|
1074
|
+
function add_reaction(signal, reaction) {
|
|
1075
|
+
var reactions = (signal.reactions ??= []);
|
|
1076
|
+
if (!includes.call(reactions, reaction)) reactions.push(reaction);
|
|
365
1077
|
}
|
|
366
1078
|
|
|
367
1079
|
/**
|
|
@@ -411,34 +1123,34 @@ function remove_reaction(signal, dependency) {
|
|
|
411
1123
|
update_derived_status(derived);
|
|
412
1124
|
}
|
|
413
1125
|
|
|
414
|
-
// Call abort controller, noone's listening to this derived anymore
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
1126
|
+
// Call abort controller, noone's listening to this derived anymore: no live run of any reaction, in any batch,
|
|
1127
|
+
// read it (the edges cover them all, see `Reaction#edges`). That includes the runs of it that batches hold
|
|
1128
|
+
if (abort_all(derived)) {
|
|
1129
|
+
// ensure it reruns right away next time instead of potentially returning a rejected promise as its value —
|
|
1130
|
+
// in the real world, and in every batch's view (which would otherwise reuse its record of the derived)
|
|
1131
|
+
set_signal_status(derived, DIRTY);
|
|
1132
|
+
derived.dv = null;
|
|
1133
|
+
for (var b = first_batch; b !== null; b = b.next) b.records?.delete(derived);
|
|
1134
|
+
view_cache?.delete(derived);
|
|
422
1135
|
}
|
|
423
1136
|
|
|
424
1137
|
// freeze any effects inside this derived
|
|
425
1138
|
freeze_derived_effects(derived);
|
|
426
1139
|
|
|
427
1140
|
// Disconnect any reactions owned by this reaction
|
|
428
|
-
remove_reactions(derived
|
|
1141
|
+
remove_reactions(derived);
|
|
429
1142
|
}
|
|
430
1143
|
}
|
|
431
1144
|
|
|
432
1145
|
/**
|
|
433
1146
|
* @param {Reaction} signal
|
|
434
|
-
* @param {number} start_index
|
|
435
1147
|
* @returns {void}
|
|
436
1148
|
*/
|
|
437
|
-
export function remove_reactions(signal
|
|
438
|
-
var dependencies = signal.
|
|
1149
|
+
export function remove_reactions(signal) {
|
|
1150
|
+
var dependencies = signal.edges;
|
|
439
1151
|
if (dependencies === null) return;
|
|
440
1152
|
|
|
441
|
-
for (var i =
|
|
1153
|
+
for (var i = 0; i < dependencies.length; i++) {
|
|
442
1154
|
remove_reaction(signal, dependencies[i]);
|
|
443
1155
|
}
|
|
444
1156
|
}
|
|
@@ -460,6 +1172,8 @@ export function update_effect(effect) {
|
|
|
460
1172
|
|
|
461
1173
|
active_effect = effect;
|
|
462
1174
|
|
|
1175
|
+
var previous_renderer = custom_renderers_flag ? push_renderer(effect.r) : null;
|
|
1176
|
+
|
|
463
1177
|
if (DEV) {
|
|
464
1178
|
var previous_component_fn = dev_current_component_function;
|
|
465
1179
|
set_dev_current_component_function(effect.component_function);
|
|
@@ -478,12 +1192,15 @@ export function update_effect(effect) {
|
|
|
478
1192
|
execute_effect_teardown(effect);
|
|
479
1193
|
var teardown = update_reaction(effect);
|
|
480
1194
|
effect.teardown = typeof teardown === 'function' ? teardown : null;
|
|
1195
|
+
|
|
1196
|
+
// Whether an effect is dirty is determined by the exact versions of its dependencies (see
|
|
1197
|
+
// `update_dependencies`) — this only marks when it last ran, which `$inspect.trace` uses
|
|
481
1198
|
effect.wv = write_version;
|
|
482
1199
|
|
|
483
1200
|
// In DEV, increment versions of any sources that were written to during the effect,
|
|
484
1201
|
// so that they are correctly marked as dirty when the effect re-runs
|
|
485
|
-
if (DEV && tracing_mode_flag && (effect.f & DIRTY) !== 0 && effect.
|
|
486
|
-
for (var dep of effect.
|
|
1202
|
+
if (DEV && tracing_mode_flag && (effect.f & DIRTY) !== 0 && effect.edges !== null) {
|
|
1203
|
+
for (var dep of effect.edges) {
|
|
487
1204
|
if (dep.set_during_effect) {
|
|
488
1205
|
dep.wv = increment_write_version();
|
|
489
1206
|
dep.set_during_effect = false;
|
|
@@ -493,6 +1210,8 @@ export function update_effect(effect) {
|
|
|
493
1210
|
} finally {
|
|
494
1211
|
active_effect = previous_effect;
|
|
495
1212
|
|
|
1213
|
+
if (custom_renderers_flag) pop_renderer(previous_renderer);
|
|
1214
|
+
|
|
496
1215
|
if (DEV) {
|
|
497
1216
|
set_dev_current_component_function(previous_component_fn);
|
|
498
1217
|
set_dev_stack(previous_stack);
|
|
@@ -543,6 +1262,9 @@ export function get(signal) {
|
|
|
543
1262
|
|
|
544
1263
|
captured_signals?.add(signal);
|
|
545
1264
|
|
|
1265
|
+
/** The reaction that reads `signal` after an `await`, if any */
|
|
1266
|
+
var late_reader = null;
|
|
1267
|
+
|
|
546
1268
|
// Register the dependency on the current reaction signal.
|
|
547
1269
|
if (active_reaction !== null && !untracking) {
|
|
548
1270
|
// if we're in a derived that is being read inside an _async_ derived,
|
|
@@ -570,23 +1292,15 @@ export function get(signal) {
|
|
|
570
1292
|
}
|
|
571
1293
|
}
|
|
572
1294
|
} else {
|
|
573
|
-
|
|
574
|
-
// We have to deduplicate deps/reactions in this case or remove_reactions could
|
|
575
|
-
// disconnect deps/reactions that are actually still in use (if skip_deps says
|
|
576
|
-
// "disconnect all after this index" and some of the signals are also present in
|
|
577
|
-
// list prior to the cutoff index, i.e. that should be kept).
|
|
578
|
-
active_reaction.deps ??= [];
|
|
579
|
-
if (!includes.call(active_reaction.deps, signal)) {
|
|
580
|
-
active_reaction.deps.push(signal);
|
|
581
|
-
}
|
|
1295
|
+
late_reader = active_reaction;
|
|
582
1296
|
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
if (
|
|
586
|
-
|
|
587
|
-
} else if (!includes.call(reactions, active_reaction)) {
|
|
588
|
-
reactions.push(active_reaction);
|
|
1297
|
+
// Late reads add an edge immediately, even if evaluating the value throws.
|
|
1298
|
+
var edges = active_reaction.edges;
|
|
1299
|
+
if (edges === null || !includes.call(edges, signal)) {
|
|
1300
|
+
active_reaction.edges = edges === null ? [signal] : [...edges, signal];
|
|
589
1301
|
}
|
|
1302
|
+
|
|
1303
|
+
add_reaction(signal, active_reaction);
|
|
590
1304
|
}
|
|
591
1305
|
}
|
|
592
1306
|
}
|
|
@@ -594,12 +1308,11 @@ export function get(signal) {
|
|
|
594
1308
|
if (DEV) {
|
|
595
1309
|
if (
|
|
596
1310
|
!untracking &&
|
|
597
|
-
|
|
598
|
-
// By checking that current/previous batch are null we filter out false positives.
|
|
1311
|
+
// By checking that no batch is active we filter out false positives.
|
|
599
1312
|
// reactivity_loss_tracker is only reset after a microtask, so if a flush happens
|
|
600
1313
|
// before that, we get warnings for things we shouldn't warn on.
|
|
601
|
-
|
|
602
|
-
|
|
1314
|
+
view_batch === null &&
|
|
1315
|
+
reactivity_loss_tracker &&
|
|
603
1316
|
!reactivity_loss_tracker.warned &&
|
|
604
1317
|
(reactivity_loss_tracker.effect.f & REACTION_IS_UPDATING) === 0 &&
|
|
605
1318
|
!reactivity_loss_tracker.effect_deps.has(signal)
|
|
@@ -648,30 +1361,26 @@ export function get(signal) {
|
|
|
648
1361
|
}
|
|
649
1362
|
}
|
|
650
1363
|
|
|
651
|
-
if (is_destroying_effect
|
|
652
|
-
|
|
1364
|
+
if (is_destroying_effect) {
|
|
1365
|
+
// teardowns have their own view of the world; nothing read here is recorded
|
|
1366
|
+
var held = teardown_view(signal);
|
|
1367
|
+
|
|
1368
|
+
if (held !== undefined) {
|
|
1369
|
+
if (held.e) throw held.v;
|
|
1370
|
+
return held.v;
|
|
1371
|
+
}
|
|
653
1372
|
}
|
|
654
1373
|
|
|
1374
|
+
/**
|
|
1375
|
+
* The value (and version) as the active view of the world sees it, if it differs from the real
|
|
1376
|
+
* world — for a derived, this is the derived itself if its global state applies (see `get_view`)
|
|
1377
|
+
* @type {{ v: any, wv: number, e?: boolean } | undefined}
|
|
1378
|
+
*/
|
|
1379
|
+
var view;
|
|
1380
|
+
|
|
655
1381
|
if (is_derived) {
|
|
656
1382
|
var derived = /** @type {Derived} */ (signal);
|
|
657
1383
|
|
|
658
|
-
if (is_destroying_effect) {
|
|
659
|
-
var value = derived.v;
|
|
660
|
-
|
|
661
|
-
// if the derived is dirty and has reactions, or depends on the values that just changed, re-execute
|
|
662
|
-
// (a derived can be maybe_dirty due to the effect destroy removing its last reaction)
|
|
663
|
-
if (
|
|
664
|
-
((derived.f & CLEAN) === 0 && derived.reactions !== null) ||
|
|
665
|
-
depends_on_old_values(derived)
|
|
666
|
-
) {
|
|
667
|
-
value = execute_derived(derived);
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
old_values.set(derived, value);
|
|
671
|
-
|
|
672
|
-
return value;
|
|
673
|
-
}
|
|
674
|
-
|
|
675
1384
|
// connect disconnected deriveds when reading them inside a connected reaction
|
|
676
1385
|
var should_connect =
|
|
677
1386
|
(derived.f & CONNECTED) === 0 &&
|
|
@@ -681,13 +1390,15 @@ export function get(signal) {
|
|
|
681
1390
|
|
|
682
1391
|
var is_new = (derived.f & REACTION_RAN) === 0;
|
|
683
1392
|
|
|
684
|
-
if (
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
}
|
|
1393
|
+
if (should_connect) {
|
|
1394
|
+
// set the flag before the derived is (re)evaluated, so that
|
|
1395
|
+
// it is added as a reaction to its dependencies
|
|
1396
|
+
derived.f |= CONNECTED;
|
|
1397
|
+
}
|
|
690
1398
|
|
|
1399
|
+
if (batch_values !== null) {
|
|
1400
|
+
view = get_view(derived);
|
|
1401
|
+
} else {
|
|
691
1402
|
update_derived(derived);
|
|
692
1403
|
}
|
|
693
1404
|
|
|
@@ -695,10 +1406,17 @@ export function get(signal) {
|
|
|
695
1406
|
unfreeze_derived_effects(derived);
|
|
696
1407
|
reconnect(derived);
|
|
697
1408
|
}
|
|
1409
|
+
} else if (batch_values !== null) {
|
|
1410
|
+
view = batch_values.get(signal);
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1413
|
+
if (late_reader !== null) {
|
|
1414
|
+
record_late_read(late_reader, signal, view === undefined ? signal.wv : view.wv);
|
|
698
1415
|
}
|
|
699
1416
|
|
|
700
|
-
if (
|
|
701
|
-
|
|
1417
|
+
if (view !== undefined && view !== signal) {
|
|
1418
|
+
if (view.e) throw view.v;
|
|
1419
|
+
return view.v;
|
|
702
1420
|
}
|
|
703
1421
|
|
|
704
1422
|
if ((signal.f & ERROR_VALUE) !== 0) {
|
|
@@ -708,6 +1426,23 @@ export function get(signal) {
|
|
|
708
1426
|
return signal.v;
|
|
709
1427
|
}
|
|
710
1428
|
|
|
1429
|
+
/**
|
|
1430
|
+
* Record that `reaction` read `signal` (with the given version) after an `await`, i.e. after
|
|
1431
|
+
* its run — as part of the record of that run
|
|
1432
|
+
* @param {Reaction} reaction
|
|
1433
|
+
* @param {Value} signal
|
|
1434
|
+
* @param {number} version
|
|
1435
|
+
*/
|
|
1436
|
+
function record_late_read(reaction, signal, version) {
|
|
1437
|
+
var run = run_of(reaction);
|
|
1438
|
+
var deps = run.deps;
|
|
1439
|
+
|
|
1440
|
+
if (run.dv !== null && (deps === null || !includes.call(deps, signal))) {
|
|
1441
|
+
run.deps = deps === null ? [signal] : [...deps, signal];
|
|
1442
|
+
run.dv.push(version);
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
|
|
711
1446
|
/**
|
|
712
1447
|
* (Re)connect a disconnected derived, so that it is notified
|
|
713
1448
|
* of changes in `mark_reactions`
|
|
@@ -716,16 +1451,10 @@ export function get(signal) {
|
|
|
716
1451
|
function reconnect(derived) {
|
|
717
1452
|
derived.f |= CONNECTED;
|
|
718
1453
|
|
|
719
|
-
if (derived.
|
|
720
|
-
|
|
721
|
-
for (const dep of derived.deps) {
|
|
722
|
-
var reactions = dep.reactions;
|
|
1454
|
+
if (derived.edges === null) return;
|
|
723
1455
|
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
} else if (!includes.call(reactions, derived)) {
|
|
727
|
-
reactions.push(derived);
|
|
728
|
-
}
|
|
1456
|
+
for (const dep of derived.edges) {
|
|
1457
|
+
add_reaction(dep, derived);
|
|
729
1458
|
|
|
730
1459
|
if ((dep.f & DERIVED) !== 0 && (dep.f & CONNECTED) === 0) {
|
|
731
1460
|
unfreeze_derived_effects(/** @type {Derived} */ (dep));
|
|
@@ -734,22 +1463,71 @@ function reconnect(derived) {
|
|
|
734
1463
|
}
|
|
735
1464
|
}
|
|
736
1465
|
|
|
737
|
-
/**
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
1466
|
+
/**
|
|
1467
|
+
* The value of `signal` in the view of the world of the running teardown, if it differs from the active one:
|
|
1468
|
+
* teardowns revert the writes that caused them, apart from their own (see `TeardownContext`). Deriveds always
|
|
1469
|
+
* get one, since their runs in a teardown only belong to it, and it is cached until the view changes:
|
|
1470
|
+
* - a value written to the derived, while none of its dependencies is written after it (like in `get_view`)
|
|
1471
|
+
* - its global run, if its dependencies still have the versions that run read
|
|
1472
|
+
* - otherwise a new run (see `update_dependencies`), which keeps the version of the value it replaces if equal
|
|
1473
|
+
* @param {Value} signal
|
|
1474
|
+
* @returns {BatchValue | undefined}
|
|
1475
|
+
*/
|
|
1476
|
+
function teardown_view(signal) {
|
|
1477
|
+
var written =
|
|
1478
|
+
signal.wv > teardown_version ? undefined : teardown_context?.reverted_values.get(signal);
|
|
1479
|
+
|
|
1480
|
+
if ((signal.f & DERIVED) === 0) return written;
|
|
1481
|
+
|
|
1482
|
+
var derived = /** @type {Derived} */ (signal);
|
|
1483
|
+
var cache = (teardown_context ?? push_teardown_context()).derived_cache;
|
|
1484
|
+
var record = cache.get(derived);
|
|
1485
|
+
|
|
1486
|
+
if (record !== undefined) return record;
|
|
1487
|
+
|
|
1488
|
+
// A value written to the derived — reverted, or the active view's — holds if it was assigned, like in `get_view`:
|
|
1489
|
+
// the value from before an assignment may have been computed, with values that the teardown reverts
|
|
1490
|
+
var held = written ?? batch_values?.get(derived);
|
|
1491
|
+
var assigned = held === undefined ? (derived.f & ASSIGNED) !== 0 : held.assigned;
|
|
1492
|
+
// (the versions its global run read, if that can apply)
|
|
1493
|
+
var dv = held === undefined && !assigned ? derived.dv : null;
|
|
1494
|
+
var deps = derived.deps ?? [];
|
|
1495
|
+
var holds = assigned || dv !== null;
|
|
1496
|
+
var length = dv?.length ?? deps.length;
|
|
1497
|
+
|
|
1498
|
+
held ??= {
|
|
1499
|
+
v: derived.v,
|
|
1500
|
+
wv: derived.wv,
|
|
1501
|
+
e: (derived.f & ERROR_VALUE) !== 0,
|
|
1502
|
+
assigned: (derived.f & ASSIGNED) !== 0
|
|
1503
|
+
};
|
|
1504
|
+
|
|
1505
|
+
for (var i = 0; holds && i < length; i++) {
|
|
1506
|
+
var version = teardown_version_of(deps[i]);
|
|
1507
|
+
holds = dv === null ? version <= held.wv : version === dv[i];
|
|
1508
|
+
}
|
|
741
1509
|
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
1510
|
+
if (holds) {
|
|
1511
|
+
record = held;
|
|
1512
|
+
} else {
|
|
1513
|
+
execute_derived(derived);
|
|
1514
|
+
record = teardown_run;
|
|
746
1515
|
|
|
747
|
-
if (
|
|
748
|
-
|
|
1516
|
+
if (held.wv < record.wv && !held.e && !record.e && derived.equals.call(held, record.v)) {
|
|
1517
|
+
record.wv = held.wv;
|
|
749
1518
|
}
|
|
750
1519
|
}
|
|
751
1520
|
|
|
752
|
-
|
|
1521
|
+
cache.set(derived, record);
|
|
1522
|
+
return record;
|
|
1523
|
+
}
|
|
1524
|
+
|
|
1525
|
+
/**
|
|
1526
|
+
* The version of `signal` in the view of the world of the running teardown
|
|
1527
|
+
* @param {Value} signal
|
|
1528
|
+
*/
|
|
1529
|
+
function teardown_version_of(signal) {
|
|
1530
|
+
return (teardown_view(signal) ?? batch_values?.get(signal) ?? signal).wv;
|
|
753
1531
|
}
|
|
754
1532
|
|
|
755
1533
|
/**
|