svelte 5.57.3 → 5.58.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/compiler/index.js +1 -1
- package/elements.d.ts +1 -2
- package/package.json +2 -2
- package/src/compiler/phases/1-parse/acorn.js +4 -14
- package/src/compiler/phases/1-parse/index.js +8 -1
- package/src/compiler/phases/1-parse/read/context.js +3 -2
- package/src/compiler/phases/1-parse/read/expression.js +2 -4
- package/src/compiler/phases/1-parse/utils/bracket.js +6 -0
- package/src/compiler/phases/2-analyze/css/css-prune.js +0 -8
- package/src/compiler/phases/2-analyze/index.js +2 -1
- package/src/compiler/phases/2-analyze/visitors/AwaitExpression.js +17 -15
- package/src/compiler/phases/3-transform/client/transform-template/index.js +27 -9
- package/src/compiler/phases/3-transform/client/utils.js +3 -2
- package/src/compiler/phases/3-transform/client/visitors/EachBlock.js +1 -1
- package/src/compiler/phases/3-transform/client/visitors/Fragment.js +13 -1
- package/src/compiler/phases/3-transform/client/visitors/HtmlTag.js +10 -2
- package/src/compiler/phases/3-transform/client/visitors/RegularElement.js +11 -2
- package/src/compiler/phases/3-transform/client/visitors/RenderTag.js +15 -7
- package/src/compiler/phases/3-transform/client/visitors/SlotElement.js +19 -1
- package/src/compiler/phases/3-transform/client/visitors/SvelteComponent.js +0 -1
- package/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js +1 -1
- package/src/compiler/phases/3-transform/client/visitors/shared/element.js +1 -1
- package/src/compiler/phases/3-transform/client/visitors/shared/events.js +1 -1
- package/src/compiler/phases/3-transform/client/visitors/shared/utils.js +1 -1
- package/src/compiler/phases/3-transform/server/visitors/ExpressionStatement.js +8 -0
- package/src/compiler/phases/3-transform/server/visitors/Fragment.js +6 -1
- package/src/compiler/phases/3-transform/server/visitors/TitleElement.js +1 -1
- package/src/compiler/phases/3-transform/server/visitors/shared/element.js +2 -1
- package/src/compiler/phases/3-transform/server/visitors/shared/utils.js +1 -1
- package/src/compiler/phases/patterns.js +0 -1
- package/src/compiler/print/index.js +49 -73
- package/src/compiler/state.js +1 -1
- package/src/compiler/utils/builders.js +0 -1
- package/src/compiler/utils/mapped_code.js +1 -12
- package/src/constants.js +0 -8
- package/src/index-client.js +3 -3
- package/src/internal/client/constants.js +8 -1
- package/src/internal/client/dev/debug.js +6 -6
- package/src/internal/client/dev/elements.js +25 -9
- package/src/internal/client/dev/tracing.js +67 -11
- package/src/internal/client/dom/blocks/async.js +2 -1
- package/src/internal/client/dom/blocks/await.js +109 -20
- package/src/internal/client/dom/blocks/boundary.js +107 -70
- package/src/internal/client/dom/blocks/branches.js +17 -23
- package/src/internal/client/dom/blocks/each.js +88 -13
- package/src/internal/client/dom/blocks/html.js +44 -10
- package/src/internal/client/dom/blocks/slot.js +19 -1
- package/src/internal/client/dom/blocks/snippet.js +13 -0
- package/src/internal/client/dom/blocks/svelte-component.js +1 -1
- package/src/internal/client/dom/blocks/svelte-element.js +2 -1
- package/src/internal/client/dom/elements/attributes.js +4 -3
- package/src/internal/client/dom/elements/bindings/input.js +19 -10
- package/src/internal/client/dom/elements/bindings/select.js +3 -7
- package/src/internal/client/dom/elements/events.js +27 -2
- package/src/internal/client/dom/hydration.js +4 -1
- package/src/internal/client/dom/operations.js +2 -3
- package/src/internal/client/dom/template.js +0 -1
- package/src/internal/client/error-handling.js +4 -12
- package/src/internal/client/errors.js +18 -2
- package/src/internal/client/index.js +0 -1
- package/src/internal/client/reactivity/async.js +249 -123
- package/src/internal/client/reactivity/batch.js +1109 -538
- package/src/internal/client/reactivity/deriveds.js +281 -177
- package/src/internal/client/reactivity/effects.js +44 -28
- package/src/internal/client/reactivity/equality.js +0 -9
- package/src/internal/client/reactivity/props.js +0 -1
- package/src/internal/client/reactivity/sources.js +33 -125
- package/src/internal/client/reactivity/status.js +1 -1
- package/src/internal/client/render.js +11 -0
- package/src/internal/client/runtime.js +925 -152
- package/src/internal/client/validate.js +2 -2
- package/src/internal/server/renderer.js +52 -118
- package/src/internal/shared/utils.js +12 -0
- package/src/motion/spring.js +1 -1
- package/src/motion/tweened.js +1 -1
- package/src/reactivity/create-subscriber.js +1 -1
- package/src/version.js +1 -1
- package/types/index.d.ts +3 -3
- package/types/index.d.ts.map +1 -1
- package/src/internal/client/reactivity/utils.js +0 -18
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/** @import { Fork } from 'svelte' */
|
|
2
|
-
/** @import { Derived, Effect, Reaction, Source, Value } from '#client' */
|
|
2
|
+
/** @import { BatchRecord, BatchValue, Derived, DerivedRecord, Effect, Reaction, Source, Value } from '#client' */
|
|
3
|
+
/** @import { Boundary } from '../dom/blocks/boundary.js' */
|
|
3
4
|
import {
|
|
4
5
|
BLOCK_EFFECT,
|
|
5
6
|
BRANCH_EFFECT,
|
|
@@ -7,7 +8,6 @@ import {
|
|
|
7
8
|
DESTROYED,
|
|
8
9
|
DIRTY,
|
|
9
10
|
EFFECT,
|
|
10
|
-
ASYNC,
|
|
11
11
|
INERT,
|
|
12
12
|
RENDER_EFFECT,
|
|
13
13
|
ROOT_EFFECT,
|
|
@@ -17,32 +17,51 @@ import {
|
|
|
17
17
|
ERROR_VALUE,
|
|
18
18
|
MANAGED_EFFECT,
|
|
19
19
|
REACTION_RAN,
|
|
20
|
-
DESTROYING
|
|
20
|
+
DESTROYING,
|
|
21
|
+
EFFECT_OFFSCREEN,
|
|
22
|
+
ASSIGNED,
|
|
23
|
+
ASYNC,
|
|
24
|
+
REACTION_IS_UPDATING,
|
|
25
|
+
BOUNDARY_EFFECT
|
|
21
26
|
} from '#client/constants';
|
|
22
27
|
import { async_mode_flag } from '../../flags/index.js';
|
|
23
|
-
import { deferred, define_property
|
|
28
|
+
import { deferred, define_property } from '../../shared/utils.js';
|
|
24
29
|
import {
|
|
30
|
+
active_effect,
|
|
25
31
|
active_reaction,
|
|
26
32
|
get,
|
|
27
33
|
increment_write_version,
|
|
34
|
+
is_destroying_effect,
|
|
28
35
|
is_dirty,
|
|
29
|
-
|
|
36
|
+
is_outdated,
|
|
37
|
+
promote_record,
|
|
38
|
+
prune_edges,
|
|
39
|
+
untrack,
|
|
40
|
+
update_effect,
|
|
41
|
+
write_version,
|
|
42
|
+
abort_record
|
|
30
43
|
} from '../runtime.js';
|
|
31
44
|
import * as e from '../errors.js';
|
|
32
45
|
import { flush_tasks, queue_micro_task } from '../dom/task.js';
|
|
33
46
|
import { DEV } from 'esm-env';
|
|
34
47
|
import { invoke_error_boundary } from '../error-handling.js';
|
|
35
|
-
import {
|
|
48
|
+
import {
|
|
49
|
+
eager_effects,
|
|
50
|
+
eager_effects_deferred,
|
|
51
|
+
flush_eager_effects,
|
|
52
|
+
source,
|
|
53
|
+
update
|
|
54
|
+
} from './sources.js';
|
|
36
55
|
import { eager_effect, teardown, unlink_effect } from './effects.js';
|
|
37
|
-
import { defer_effect } from './utils.js';
|
|
38
56
|
import { UNINITIALIZED } from '../../../constants.js';
|
|
39
|
-
import { set_signal_status } from './status.js';
|
|
57
|
+
import { set_signal_status, update_derived_status } from './status.js';
|
|
40
58
|
import { invariant } from '../../shared/dev.js';
|
|
41
|
-
import { log_effect_tree } from '../dev/debug.js';
|
|
42
59
|
import { OBSOLETE } from './deriveds.js';
|
|
60
|
+
import { is_runes } from '../context.js';
|
|
61
|
+
import { merge_target } from './async.js';
|
|
43
62
|
|
|
44
63
|
/** @type {Batch | null} */
|
|
45
|
-
let first_batch = null;
|
|
64
|
+
export let first_batch = null;
|
|
46
65
|
|
|
47
66
|
/** @type {Batch | null} */
|
|
48
67
|
let last_batch = null;
|
|
@@ -51,24 +70,99 @@ let last_batch = null;
|
|
|
51
70
|
export let current_batch = null;
|
|
52
71
|
|
|
53
72
|
/**
|
|
54
|
-
*
|
|
73
|
+
* The batch currently being processed (see `#process`)
|
|
55
74
|
* @type {Batch | null}
|
|
56
75
|
*/
|
|
57
|
-
export let
|
|
76
|
+
export let processing_batch = null;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @typedef {Object} TeardownContext
|
|
80
|
+
* @property {Map<Value, BatchValue>} reverted_values What teardowns see instead of the values sources hold: the values
|
|
81
|
+
* from before the writes that caused the teardowns (the `previous` of the batch being processed, see `flush`). For
|
|
82
|
+
* block effects that re-run right away because of a user effect's writes, also the writes of the effects that ran
|
|
83
|
+
* before it in the same commit, which apply (see `flush_queued_effects`)
|
|
84
|
+
* @property {Map<Derived, BatchValue>} derived_cache The values of deriveds in this view (see `runtime.js#teardown_view`),
|
|
85
|
+
* until it changes
|
|
86
|
+
* @property {TeardownContext | null} parent The context to restore after a nested flush or eager block
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Effect teardowns revert the writes that caused them (see `runtime.js#teardown_view`).
|
|
91
|
+
* (A teardown outside of a flush reverts nothing, but gets a context to cache its deriveds in, for as long as it
|
|
92
|
+
* runs — see `execute_effect_teardown`)
|
|
93
|
+
* @type {TeardownContext | null}
|
|
94
|
+
*/
|
|
95
|
+
export let teardown_context = null;
|
|
96
|
+
|
|
97
|
+
/** @param {TeardownContext | null} context */
|
|
98
|
+
export function set_teardown_context(context) {
|
|
99
|
+
teardown_context = context;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** @param {Map<Value, BatchValue>} [values] */
|
|
103
|
+
export function push_teardown_context(values) {
|
|
104
|
+
var context = (teardown_context = {
|
|
105
|
+
reverted_values: new Map(teardown_context?.reverted_values),
|
|
106
|
+
derived_cache: new Map(),
|
|
107
|
+
parent: teardown_context
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
for (const [source, value] of values ?? []) context.reverted_values.set(source, value);
|
|
111
|
+
|
|
112
|
+
return context;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* A write in a teardown remains visible after returning to an enclosing teardown view,
|
|
117
|
+
* and the deriveds that teardowns evaluated may depend on it
|
|
118
|
+
* @param {Value} source
|
|
119
|
+
*/
|
|
120
|
+
export function teardown_write(source) {
|
|
121
|
+
for (var context = teardown_context; context !== null; context = context.parent) {
|
|
122
|
+
context.reverted_values.delete(source);
|
|
123
|
+
context.derived_cache.clear();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
58
126
|
|
|
59
127
|
/**
|
|
60
128
|
* When time travelling (i.e. working in one batch, while other batches
|
|
61
129
|
* still have ongoing work), we ignore the real values of affected
|
|
62
|
-
* signals in favour of their values within the batch
|
|
63
|
-
*
|
|
130
|
+
* signals in favour of their values within the batch — along with their
|
|
131
|
+
* versions: the batch's writes, and the values from before the writes of
|
|
132
|
+
* other batches, which its view of the world undoes (see `activate`).
|
|
133
|
+
* `null` means that the real world (i.e. the latest values) applies.
|
|
134
|
+
* Doesn't contain computed deriveds, these are in {@link view_cache}.
|
|
135
|
+
* @type {Map<Value, BatchValue> | null}
|
|
64
136
|
*/
|
|
65
137
|
export let batch_values = null;
|
|
66
138
|
|
|
139
|
+
/**
|
|
140
|
+
* The values of deriveds in the active view of the world, for as long as it is active (see `get_view`):
|
|
141
|
+
* a record of a run (in which case `e` tells whether its value is an error), the derived itself if its
|
|
142
|
+
* global state applies, or the write to it that holds (an assignment). Exists whenever `batch_values` does
|
|
143
|
+
* @type {Map<Derived, Derived | DerivedRecord | BatchValue> | null}
|
|
144
|
+
*/
|
|
145
|
+
export let view_cache = null;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* The batch whose view of the world is active (see `activate`). Block and async
|
|
149
|
+
* effects that run while it is processed, and deriveds evaluated with values
|
|
150
|
+
* that are not the latest, are recorded in this batch
|
|
151
|
+
* @type {Batch | null}
|
|
152
|
+
*/
|
|
153
|
+
export let view_batch = null;
|
|
154
|
+
|
|
67
155
|
/** @type {Effect | null} */
|
|
68
156
|
let last_scheduled_effect = null;
|
|
69
157
|
|
|
158
|
+
/**
|
|
159
|
+
* Batches whose view of the world changed because another batch committed, and that
|
|
160
|
+
* therefore need to be processed again once the current flush is done (see `#commit`)
|
|
161
|
+
* @type {Set<Batch>}
|
|
162
|
+
*/
|
|
163
|
+
var rebases = new Set();
|
|
164
|
+
|
|
70
165
|
export let is_flushing_sync = false;
|
|
71
|
-
let is_processing = false;
|
|
72
166
|
|
|
73
167
|
/**
|
|
74
168
|
* During traversal, this is an array. Newly created effects are (if not immediately
|
|
@@ -89,11 +183,25 @@ export let legacy_updates = null;
|
|
|
89
183
|
|
|
90
184
|
var flush_count = 0;
|
|
91
185
|
|
|
92
|
-
/** @type {Set<Value>} */
|
|
93
|
-
|
|
186
|
+
/** Sources whose DEV update counts and stack traces need resetting after the flush. @type {Set<Value>} */
|
|
187
|
+
export const source_stacks = new Set();
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* A set of signals we have already seen while traversing in `batch.mark`.
|
|
191
|
+
* Not always set to balance the common case of sources only having a couple
|
|
192
|
+
* of (transitive) dependencies (where always creating a Set would be bad for perf)
|
|
193
|
+
* with the edge case of extremely deep or wide dependency arrays with cycles.
|
|
194
|
+
* @type {Set<any> | null}
|
|
195
|
+
*/
|
|
196
|
+
var seen = null;
|
|
197
|
+
/** Number of transitive dependencies, see {@link seen} for more info */
|
|
198
|
+
var count_deps = 0;
|
|
94
199
|
|
|
95
200
|
let uid = 1;
|
|
96
201
|
|
|
202
|
+
/** @type {number | null} */
|
|
203
|
+
let last_processed = null;
|
|
204
|
+
|
|
97
205
|
export class Batch {
|
|
98
206
|
id = uid++;
|
|
99
207
|
|
|
@@ -112,43 +220,60 @@ export class Batch {
|
|
|
112
220
|
async_deriveds = new Map();
|
|
113
221
|
|
|
114
222
|
/**
|
|
115
|
-
* The current values of any
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
223
|
+
* The current values (and their versions) of any sources that are updated in this batch,
|
|
224
|
+
* including deriveds that were overridden via assignment — until it commits them (see `#commit`).
|
|
225
|
+
* Its keys are those of `this.previous`, plus the sources this batch gave their first value. `e` says whether
|
|
226
|
+
* the value is an error
|
|
227
|
+
* @type {Map<Value, BatchValue>}
|
|
119
228
|
*/
|
|
120
229
|
current = new Map();
|
|
121
230
|
|
|
122
231
|
/**
|
|
123
|
-
* The values of any
|
|
124
|
-
*
|
|
125
|
-
*
|
|
232
|
+
* The values (and their versions) of any sources that are updated in this batch _before_ those updates took place.
|
|
233
|
+
* For a derived, `assigned` says whether its value before was assigned, too (otherwise it was computed).
|
|
234
|
+
* A source that this batch gave its first value (e.g. an async expression that resolved for the first time) has
|
|
235
|
+
* no entry: there's no value from before it, so other views can't undo the write, and teardowns don't revert it.
|
|
236
|
+
* @type {Map<Value, BatchValue>}
|
|
126
237
|
*/
|
|
127
238
|
previous = new Map();
|
|
128
239
|
|
|
129
240
|
/**
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
241
|
+
* Results of reactions that belong to this batch rather than to the real world:
|
|
242
|
+
* deriveds evaluated in this batch's view of the world with values that are not the
|
|
243
|
+
* latest ones, and block/async effects that ran while this batch was processed.
|
|
244
|
+
* Effect records become global state when the batch commits (see `promote_record`)
|
|
245
|
+
* @type {Map<Reaction, BatchRecord> | null}
|
|
133
246
|
*/
|
|
134
|
-
|
|
247
|
+
records = null;
|
|
135
248
|
|
|
136
249
|
/**
|
|
137
|
-
*
|
|
138
|
-
*
|
|
250
|
+
* Earlier batches whose changes are part of this batch's view of the world, because
|
|
251
|
+
* they intersect with it (this batch will be merged into them, see `#merge`)
|
|
252
|
+
* @type {Batch[]}
|
|
139
253
|
*/
|
|
140
|
-
|
|
254
|
+
folded = [];
|
|
141
255
|
|
|
142
256
|
/**
|
|
143
|
-
*
|
|
257
|
+
* When the batch is committed (and the DOM is updated), we need to remove old branches
|
|
258
|
+
* and append new ones by calling the functions added inside (if/each/key/etc) blocks.
|
|
259
|
+
* Each callback maps to the writes that apply in the teardowns it causes, if it was added
|
|
260
|
+
* while block effects re-ran eagerly (see `eager_block_writes`)
|
|
261
|
+
* @type {Map<(batch: Batch) => void, Map<Value, BatchValue> | null>}
|
|
262
|
+
*/
|
|
263
|
+
#commit_callbacks = new Map();
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* If a fork is discarded, we need to destroy any effects that are no longer needed
|
|
267
|
+
* (see `#commit_callbacks`)
|
|
268
|
+
* @type {Map<(batch: Batch) => void, Map<Value, BatchValue> | null>}
|
|
144
269
|
*/
|
|
145
|
-
#
|
|
270
|
+
#discard_callbacks = new Map();
|
|
146
271
|
|
|
147
272
|
/**
|
|
148
|
-
* Async effects that are currently in flight
|
|
273
|
+
* Async effects that are currently in flight
|
|
149
274
|
* @type {Map<Effect, number>}
|
|
150
275
|
*/
|
|
151
|
-
#
|
|
276
|
+
#pending = new Map();
|
|
152
277
|
|
|
153
278
|
/**
|
|
154
279
|
* A deferred that resolves when the batch is committed, used with `settled()`
|
|
@@ -157,6 +282,14 @@ export class Batch {
|
|
|
157
282
|
*/
|
|
158
283
|
#deferred = null;
|
|
159
284
|
|
|
285
|
+
/**
|
|
286
|
+
* Effects to (re-)validate when this batch is processed next, and their status (DIRTY or MAYBE_DIRTY):
|
|
287
|
+
* those that were scheduled while it wasn't being processed (see {@link schedule}), and those whose run
|
|
288
|
+
* was deferred because it has pending work (see `#defer_effects`)
|
|
289
|
+
* @type {Map<Effect, number>}
|
|
290
|
+
*/
|
|
291
|
+
#unprocessed = new Map();
|
|
292
|
+
|
|
160
293
|
/**
|
|
161
294
|
* Effects that were scheduled in this batch but not yet 'resolved' into the
|
|
162
295
|
* root effects that need to be flushed. Resolving — the upwards traversal that
|
|
@@ -171,33 +304,35 @@ export class Batch {
|
|
|
171
304
|
#scheduled = [];
|
|
172
305
|
|
|
173
306
|
/**
|
|
174
|
-
*
|
|
175
|
-
*
|
|
307
|
+
* Branches that still exist, but will be destroyed when this batch
|
|
308
|
+
* is committed — we skip over these during `#process`
|
|
309
|
+
* @type {Set<Effect>}
|
|
176
310
|
*/
|
|
177
|
-
#
|
|
311
|
+
#skipped_branches = new Set();
|
|
178
312
|
|
|
179
313
|
/**
|
|
180
|
-
*
|
|
181
|
-
*
|
|
314
|
+
* Branches that this batch selected (see `unskip_effect`). A branch that is offscreen until
|
|
315
|
+
* it is committed (see `EFFECT_OFFSCREEN`) only exists in the views of the batches that selected it
|
|
316
|
+
* @type {Set<Effect>}
|
|
182
317
|
*/
|
|
183
|
-
#
|
|
318
|
+
#unskipped_branches = new Set();
|
|
319
|
+
|
|
320
|
+
is_fork = false;
|
|
184
321
|
|
|
185
322
|
/**
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
* so they can be rescheduled if the branch survives.
|
|
190
|
-
* @type {Map<Effect, { d: Effect[], m: Effect[] }>}
|
|
323
|
+
* The batch that this one was merged into, if any (see `#merge`). Its async work that is still in flight
|
|
324
|
+
* continues as part of that one
|
|
325
|
+
* @type {Batch | null}
|
|
191
326
|
*/
|
|
192
|
-
|
|
327
|
+
merged = null;
|
|
193
328
|
|
|
194
329
|
/**
|
|
195
|
-
*
|
|
196
|
-
*
|
|
330
|
+
* Batches whose runs this batch aborted. Merge into them before waiting for pending work.
|
|
331
|
+
* Requests belong to this batch because a synchronous derived can abort outside traversal,
|
|
332
|
+
* before an unrelated batch is processed (e.g. a fork being committed).
|
|
333
|
+
* @type {Set<Batch>}
|
|
197
334
|
*/
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
is_fork = false;
|
|
335
|
+
merge_requests = new Set();
|
|
201
336
|
|
|
202
337
|
#decrement_queued = false;
|
|
203
338
|
|
|
@@ -215,9 +350,8 @@ export class Batch {
|
|
|
215
350
|
#is_deferred() {
|
|
216
351
|
if (this.is_fork) return true;
|
|
217
352
|
|
|
218
|
-
for (const effect of this.#
|
|
219
|
-
|
|
220
|
-
if (!this.#is_skipped(effect)) {
|
|
353
|
+
for (const effect of this.#pending.keys()) {
|
|
354
|
+
if (this.is_blocked_by(effect)) {
|
|
221
355
|
return true;
|
|
222
356
|
}
|
|
223
357
|
}
|
|
@@ -226,50 +360,107 @@ export class Batch {
|
|
|
226
360
|
}
|
|
227
361
|
|
|
228
362
|
/**
|
|
229
|
-
* Whether `effect` is
|
|
363
|
+
* Whether `effect` is a branch that this batch skips, or that only exists offscreen for other batches
|
|
364
|
+
* @param {Effect} effect
|
|
365
|
+
*/
|
|
366
|
+
#skips(effect) {
|
|
367
|
+
return (
|
|
368
|
+
this.#skipped_branches.has(effect) ||
|
|
369
|
+
((effect.f & EFFECT_OFFSCREEN) !== 0 && !this.#unskipped_branches.has(effect))
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Whether `effect` is inside a branch that this batch skips, i.e. doesn't exist in its view of the world
|
|
230
375
|
* @param {Effect} effect
|
|
231
376
|
*/
|
|
232
|
-
|
|
377
|
+
is_skipped(effect) {
|
|
233
378
|
for (var e = /** @type {Effect | null} */ (effect); e !== null; e = e.parent) {
|
|
234
|
-
if (this.#
|
|
379
|
+
if (this.#skips(e)) return true;
|
|
235
380
|
}
|
|
236
381
|
|
|
237
382
|
return false;
|
|
238
383
|
}
|
|
239
384
|
|
|
240
385
|
/**
|
|
241
|
-
*
|
|
386
|
+
* Whether async work in `effect` holds up this batch — or, with `root`, the boundary whose content `root` is.
|
|
387
|
+
* It doesn't if the effect is inside a branch that this batch skips (the branch is going to be destroyed),
|
|
388
|
+
* or inside a boundary that is pending (which shows its pending snippet in the meantime).
|
|
389
|
+
* Unlike a skipped branch, the content of a pending boundary does exist in the view of the batch: its
|
|
390
|
+
* effects are still re-validated (see `#revalidate`), they just don't keep the batch from committing
|
|
242
391
|
* @param {Effect} effect
|
|
392
|
+
* @param {Effect | null} [root]
|
|
243
393
|
*/
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
this.#
|
|
394
|
+
is_blocked_by(effect, root = null) {
|
|
395
|
+
for (var e = /** @type {Effect | null} */ (effect); e !== null && e !== root; e = e.parent) {
|
|
396
|
+
if (this.#skips(e)) return false;
|
|
397
|
+
|
|
398
|
+
if (
|
|
399
|
+
(e.f & (BOUNDARY_EFFECT | DESTROYED)) === BOUNDARY_EFFECT &&
|
|
400
|
+
/** @type {Boundary} */ (e.b).is_pending
|
|
401
|
+
) {
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
247
404
|
}
|
|
405
|
+
|
|
406
|
+
return true;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* @param {Effect} effect
|
|
411
|
+
*/
|
|
412
|
+
skip_effect(effect) {
|
|
413
|
+
this.#skipped_branches.add(effect);
|
|
248
414
|
this.#unskipped_branches.delete(effect);
|
|
249
415
|
}
|
|
250
416
|
|
|
251
417
|
/**
|
|
252
|
-
* Remove an effect from the #skipped_branches map and reschedule
|
|
253
|
-
* any tracked dirty/maybe_dirty child effects
|
|
254
418
|
* @param {Effect} effect
|
|
255
|
-
* @param {(e: Effect) => void} callback
|
|
256
419
|
*/
|
|
257
|
-
unskip_effect(effect
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
this.#
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
420
|
+
unskip_effect(effect) {
|
|
421
|
+
if (
|
|
422
|
+
this.#skipped_branches.delete(effect) ||
|
|
423
|
+
(!this.#unskipped_branches.has(effect) &&
|
|
424
|
+
((effect.f & EFFECT_OFFSCREEN) !== 0 ||
|
|
425
|
+
this.folded.some((batch) => batch.#skipped_branches.has(effect))))
|
|
426
|
+
) {
|
|
427
|
+
// This batch adopts a branch that it skipped (whose effects may have been reset, see `reset_branch`),
|
|
428
|
+
// that so far only existed offscreen (possibly for another batch, whose view of the world its
|
|
429
|
+
// effects ran in), or that a batch whose writes are part of its view skips (and so didn't run the
|
|
430
|
+
// effects that its writes affect, or reset them) — re-validate its effects in this batch's view
|
|
431
|
+
this.revalidate_branch(effect);
|
|
432
|
+
}
|
|
266
433
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
434
|
+
this.#unskipped_branches.add(effect);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Like `unskip_effect`, for a branch that was just created offscreen while this batch is processed.
|
|
439
|
+
* Its effects ran in this batch's view of the world when they were created, so there's nothing to re-validate
|
|
440
|
+
* TODO can we skip revalidation in `unskip_effect` in more cases, by detecting whether things changed
|
|
441
|
+
* inside a branch while it was skipped?
|
|
442
|
+
* @param {Effect} effect
|
|
443
|
+
*/
|
|
444
|
+
unskip_new_effect(effect) {
|
|
445
|
+
this.#unskipped_branches.add(effect);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* @param {Effect} effect
|
|
450
|
+
*/
|
|
451
|
+
revalidate_branch(effect) {
|
|
452
|
+
for (var e = effect.first; e !== null; e = e.next) {
|
|
453
|
+
var flags = e.f;
|
|
454
|
+
|
|
455
|
+
// (branches nested inside that exist offscreen for other batches only are not adopted)
|
|
456
|
+
if ((flags & (DESTROYED | INERT)) === 0 && !this.#skips(e)) {
|
|
457
|
+
if ((flags & (BRANCH_EFFECT | ROOT_EFFECT | EAGER_EFFECT)) === 0 && e.fn !== null) {
|
|
458
|
+
this.schedule(e, MAYBE_DIRTY);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
this.revalidate_branch(e);
|
|
270
462
|
}
|
|
271
463
|
}
|
|
272
|
-
this.#unskipped_branches.add(effect);
|
|
273
464
|
}
|
|
274
465
|
|
|
275
466
|
/**
|
|
@@ -321,7 +512,28 @@ export class Batch {
|
|
|
321
512
|
}
|
|
322
513
|
|
|
323
514
|
#process() {
|
|
515
|
+
current_batch = processing_batch = this;
|
|
516
|
+
|
|
517
|
+
// A different batch view may apply, even when processing continues within the same flush.
|
|
518
|
+
teardown_context?.derived_cache.clear();
|
|
519
|
+
|
|
520
|
+
if (async_mode_flag && this.started && last_processed !== this.id) {
|
|
521
|
+
// Since this batch last ran, reactions may have come to depend on its writes without it
|
|
522
|
+
// knowing (e.g. effects created by other batches), or have run in the view of other batches.
|
|
523
|
+
// Re-validate them in this batch's view — this only re-runs those that are out of date
|
|
524
|
+
this.#revalidate(this.current.keys());
|
|
525
|
+
}
|
|
526
|
+
|
|
324
527
|
this.started = true;
|
|
528
|
+
last_processed = this.id;
|
|
529
|
+
|
|
530
|
+
// Deferred effects are rescheduled even if the batch still has pending work, because traversing the
|
|
531
|
+
// tree could make an if block that contains the last blocking pending effect falsy, causing the batch
|
|
532
|
+
// to no longer be deferred. If it still is, they are deferred again
|
|
533
|
+
if (this.#unprocessed.size > 0) {
|
|
534
|
+
for (const [e, status] of this.#unprocessed) this.#add(e, status);
|
|
535
|
+
this.#unprocessed.clear();
|
|
536
|
+
}
|
|
325
537
|
|
|
326
538
|
if (DEV) {
|
|
327
539
|
// track all the values that were updated during this flush,
|
|
@@ -331,16 +543,7 @@ export class Batch {
|
|
|
331
543
|
}
|
|
332
544
|
}
|
|
333
545
|
|
|
334
|
-
|
|
335
|
-
// #is_deferred() is true, because traversing the tree could make
|
|
336
|
-
// an if block that contains the last blocking pending effect falsy,
|
|
337
|
-
// causing the block to no longer be deferred.
|
|
338
|
-
for (const [e, status] of this.#dirty_reactions) {
|
|
339
|
-
set_signal_status(e, status);
|
|
340
|
-
this.schedule(e);
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
this.apply();
|
|
546
|
+
this.activate();
|
|
344
547
|
|
|
345
548
|
/** @type {Effect[]} */
|
|
346
549
|
var effects = (collected_effects = []);
|
|
@@ -348,6 +551,9 @@ export class Batch {
|
|
|
348
551
|
/** @type {Effect[]} */
|
|
349
552
|
var render_effects = [];
|
|
350
553
|
|
|
554
|
+
/** @type {Boundary[]} */
|
|
555
|
+
var revealed_boundaries = [];
|
|
556
|
+
|
|
351
557
|
/**
|
|
352
558
|
* @type {Effect[]}
|
|
353
559
|
* @deprecated when we get rid of legacy mode and stores, we can get rid of this
|
|
@@ -360,14 +566,14 @@ export class Batch {
|
|
|
360
566
|
while (this.#scheduled.length > 0) {
|
|
361
567
|
if (flush_count++ > 1000) {
|
|
362
568
|
this.#unlink();
|
|
363
|
-
infinite_loop_guard(); // TODO try to
|
|
569
|
+
infinite_loop_guard(); // TODO try to reset the tree here?
|
|
364
570
|
}
|
|
365
571
|
|
|
366
572
|
for (const root of this.#resolve()) {
|
|
367
573
|
try {
|
|
368
|
-
this.#traverse(root, effects, render_effects);
|
|
574
|
+
this.#traverse(root, effects, render_effects, revealed_boundaries);
|
|
369
575
|
} catch (e) {
|
|
370
|
-
|
|
576
|
+
reset_branch(root, true);
|
|
371
577
|
// If there's no async work left, this branch is now dead and needs
|
|
372
578
|
// to be discarded to not become a zombie that is never cleaned up.
|
|
373
579
|
// See https://github.com/sveltejs/svelte/issues/18221#issuecomment-4497918414
|
|
@@ -377,6 +583,16 @@ export class Batch {
|
|
|
377
583
|
throw e;
|
|
378
584
|
}
|
|
379
585
|
}
|
|
586
|
+
|
|
587
|
+
if (this.#scheduled.length > 0) {
|
|
588
|
+
// A later traversal may discover effects before ones we already collected.
|
|
589
|
+
// Recollect them together so they are flushed in tree order, rather than
|
|
590
|
+
// appending earlier effects after their later siblings.
|
|
591
|
+
for (const effect of render_effects) this.#scheduled.push(effect);
|
|
592
|
+
for (const effect of effects) this.#scheduled.push(effect);
|
|
593
|
+
render_effects.length = 0;
|
|
594
|
+
effects.length = 0;
|
|
595
|
+
}
|
|
380
596
|
}
|
|
381
597
|
|
|
382
598
|
// any writes should take effect in a subsequent batch
|
|
@@ -385,20 +601,48 @@ export class Batch {
|
|
|
385
601
|
if (updates.length > 0) {
|
|
386
602
|
var batch = Batch.ensure();
|
|
387
603
|
for (const e of updates) {
|
|
388
|
-
|
|
604
|
+
// these were marked during traversal already (see `mark_reactions`), unless they ran since
|
|
605
|
+
if ((e.f & CLEAN) === 0) batch.schedule(e, MAYBE_DIRTY);
|
|
389
606
|
}
|
|
390
607
|
}
|
|
391
608
|
|
|
392
609
|
collected_effects = null;
|
|
393
610
|
legacy_updates = null;
|
|
394
611
|
|
|
612
|
+
if (this.merge_requests.size > 0) {
|
|
613
|
+
const sorted = Array.from(this.merge_requests, merge_target)
|
|
614
|
+
.filter((batch) => batch !== this && batch.linked && !batch.is_fork)
|
|
615
|
+
.sort((a, b) => a.id - b.id);
|
|
616
|
+
this.merge_requests.clear();
|
|
617
|
+
|
|
618
|
+
if (sorted.length > 0) {
|
|
619
|
+
this.#defer_effects(render_effects);
|
|
620
|
+
this.#defer_effects(effects);
|
|
621
|
+
|
|
622
|
+
/** @type {Batch} */
|
|
623
|
+
var target = this;
|
|
624
|
+
var i = sorted.length;
|
|
625
|
+
|
|
626
|
+
while (i--) {
|
|
627
|
+
var earlier = sorted[i];
|
|
628
|
+
if (earlier === target) continue;
|
|
629
|
+
earlier.#merge(target);
|
|
630
|
+
target = earlier;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
// Revalidate once all the writes and pending work belong to the same batch.
|
|
634
|
+
target.#process();
|
|
635
|
+
return;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
395
639
|
// if the batch has outstanding pending work, stash effects and bail
|
|
396
640
|
if (this.#is_deferred()) {
|
|
397
641
|
this.#defer_effects(render_effects);
|
|
398
642
|
this.#defer_effects(effects);
|
|
399
643
|
|
|
400
|
-
for (const
|
|
401
|
-
reset_branch(e
|
|
644
|
+
for (const e of this.#skipped_branches) {
|
|
645
|
+
reset_branch(e);
|
|
402
646
|
}
|
|
403
647
|
|
|
404
648
|
if (updates.length > 0) {
|
|
@@ -416,53 +660,111 @@ export class Batch {
|
|
|
416
660
|
this.#defer_effects(render_effects);
|
|
417
661
|
this.#defer_effects(effects);
|
|
418
662
|
earlier_batch.#merge(this);
|
|
663
|
+
earlier_batch.#process();
|
|
664
|
+
|
|
419
665
|
return;
|
|
420
666
|
}
|
|
421
667
|
|
|
422
|
-
|
|
423
|
-
|
|
668
|
+
for (const boundary of revealed_boundaries) {
|
|
669
|
+
// A later traversal may have restored an unresolved branch since this boundary was queued.
|
|
670
|
+
if (boundary.is_pending && boundary.is_ready(this)) {
|
|
671
|
+
boundary.reveal(effects);
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// The results of this batch are about to become the real-world state
|
|
676
|
+
this.#release_records(true);
|
|
424
677
|
|
|
425
678
|
// append/remove branches
|
|
426
|
-
|
|
427
|
-
this.#commit_callbacks.clear();
|
|
679
|
+
this.#run_callbacks(this.#commit_callbacks);
|
|
428
680
|
|
|
429
|
-
|
|
430
|
-
flush_queued_effects(
|
|
431
|
-
flush_queued_effects(effects);
|
|
432
|
-
previous_batch = null;
|
|
681
|
+
var earlier_writes = flush_queued_effects(render_effects);
|
|
682
|
+
flush_queued_effects(effects, earlier_writes);
|
|
433
683
|
|
|
434
684
|
this.#deferred?.resolve();
|
|
435
685
|
|
|
436
686
|
var next_batch = /** @type {Batch | null} */ (/** @type {unknown} */ (current_batch));
|
|
437
687
|
|
|
438
|
-
if (this.#pending === 0 && (this.#scheduled.length === 0 || next_batch !== null)) {
|
|
688
|
+
if (this.#pending.size === 0 && (this.#scheduled.length === 0 || next_batch !== null)) {
|
|
439
689
|
this.#unlink();
|
|
690
|
+
}
|
|
440
691
|
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
// Rebases can activate other batches or null it out, therefore restore the new one here
|
|
448
|
-
current_batch = next_batch;
|
|
449
|
-
}
|
|
692
|
+
// Order matters here - we need to commit and THEN continue flushing new batches, not the other way around,
|
|
693
|
+
// else we could start flushing a new batch and then, if it has pending work, rebase it right afterwards, which is wrong.
|
|
694
|
+
// In sync mode flushSync can cause #commit to wrongfully think that there needs to be a rebase, so we only do it in async mode
|
|
695
|
+
// TODO fix the underlying cause, otherwise this will likely regress when non-async mode is removed
|
|
696
|
+
if (async_mode_flag) {
|
|
697
|
+
this.commit_writes();
|
|
450
698
|
}
|
|
451
699
|
|
|
452
700
|
if (next_batch !== null) {
|
|
453
|
-
|
|
701
|
+
// The next batch holds the writes made while this one committed, which cause the teardowns that run
|
|
702
|
+
// when it's processed. So teardowns now revert those, and no longer this batch's: of the reverted
|
|
703
|
+
// values, keep those that are the next batch's values from before its writes. (Writes made in teardowns
|
|
704
|
+
// aren't among them, see `teardown_write`, so they stay visible)
|
|
705
|
+
if (teardown_context !== null) {
|
|
706
|
+
for (const [source, snapshot] of teardown_context.reverted_values) {
|
|
707
|
+
if (next_batch.previous.get(source)?.wv !== snapshot.wv) {
|
|
708
|
+
teardown_context.reverted_values.delete(source);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
|
|
454
713
|
next_batch.#process();
|
|
455
714
|
}
|
|
456
715
|
}
|
|
457
716
|
|
|
717
|
+
/**
|
|
718
|
+
* Schedule the reactions that (transitively) depend on `sources` for re-validation in this batch.
|
|
719
|
+
* They are marked MAYBE_DIRTY, which means that they are only re-run if the exact versions
|
|
720
|
+
* of their dependencies differ from the ones they last saw (in this batch's view of the world)
|
|
721
|
+
* @param {Iterable<Source>} sources
|
|
722
|
+
*/
|
|
723
|
+
#revalidate(sources) {
|
|
724
|
+
// deriveds added during iteration are visited too, so this doubles as a worklist
|
|
725
|
+
/** @type {Set<Value>} */
|
|
726
|
+
var values = new Set(sources);
|
|
727
|
+
|
|
728
|
+
/** @type {Set<Effect>} */
|
|
729
|
+
var effects = new Set();
|
|
730
|
+
|
|
731
|
+
for (const value of values) {
|
|
732
|
+
var reactions = value.reactions;
|
|
733
|
+
if (reactions === null) continue;
|
|
734
|
+
|
|
735
|
+
for (const reaction of reactions) {
|
|
736
|
+
var flags = reaction.f;
|
|
737
|
+
|
|
738
|
+
if ((flags & DERIVED) !== 0) {
|
|
739
|
+
if ((flags & CLEAN) !== 0) set_signal_status(reaction, MAYBE_DIRTY);
|
|
740
|
+
values.add(/** @type {Derived} */ (reaction));
|
|
741
|
+
continue;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
var effect = /** @type {Effect} */ (reaction);
|
|
745
|
+
if (effects.has(effect)) continue;
|
|
746
|
+
effects.add(effect);
|
|
747
|
+
|
|
748
|
+
if (
|
|
749
|
+
(flags & (EAGER_EFFECT | INERT | DESTROYED)) === 0 &&
|
|
750
|
+
// branches that this batch skips don't exist in its view of the world
|
|
751
|
+
!this.is_skipped(effect)
|
|
752
|
+
) {
|
|
753
|
+
this.schedule(effect, MAYBE_DIRTY);
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
|
|
458
759
|
/**
|
|
459
760
|
* Traverse the effect tree, executing effects or stashing
|
|
460
761
|
* them for later execution as appropriate
|
|
461
762
|
* @param {Effect} root
|
|
462
763
|
* @param {Effect[]} effects
|
|
463
764
|
* @param {Effect[]} render_effects
|
|
765
|
+
* @param {Boundary[]} revealed_boundaries
|
|
464
766
|
*/
|
|
465
|
-
#traverse(root, effects, render_effects) {
|
|
767
|
+
#traverse(root, effects, render_effects, revealed_boundaries) {
|
|
466
768
|
root.f ^= CLEAN;
|
|
467
769
|
|
|
468
770
|
var effect = root.first;
|
|
@@ -472,7 +774,9 @@ export class Batch {
|
|
|
472
774
|
var is_branch = (flags & (BRANCH_EFFECT | ROOT_EFFECT)) !== 0;
|
|
473
775
|
var is_skippable_branch = is_branch && (flags & CLEAN) !== 0;
|
|
474
776
|
|
|
475
|
-
|
|
777
|
+
// (eager effects run in the real world, see `flush_eager_effects`, not in the view of the batch)
|
|
778
|
+
var skip =
|
|
779
|
+
is_skippable_branch || (flags & (INERT | EAGER_EFFECT)) !== 0 || this.#skips(effect);
|
|
476
780
|
|
|
477
781
|
if (!skip && effect.fn !== null) {
|
|
478
782
|
if (is_branch) {
|
|
@@ -482,7 +786,6 @@ export class Batch {
|
|
|
482
786
|
} else if (async_mode_flag && (flags & (RENDER_EFFECT | MANAGED_EFFECT)) !== 0) {
|
|
483
787
|
render_effects.push(effect);
|
|
484
788
|
} else if (is_dirty(effect)) {
|
|
485
|
-
if ((flags & BLOCK_EFFECT) !== 0) this.add_dirty_reaction(effect, MAYBE_DIRTY);
|
|
486
789
|
update_effect(effect);
|
|
487
790
|
}
|
|
488
791
|
|
|
@@ -495,6 +798,15 @@ export class Batch {
|
|
|
495
798
|
}
|
|
496
799
|
|
|
497
800
|
while (effect !== null) {
|
|
801
|
+
// if a pending boundary is now resolved, reveal it
|
|
802
|
+
if ((effect.f & (BOUNDARY_EFFECT | DESTROYED)) === BOUNDARY_EFFECT) {
|
|
803
|
+
var boundary = /** @type {Boundary} */ (effect.b);
|
|
804
|
+
|
|
805
|
+
if (boundary.is_pending && boundary.is_ready(this)) {
|
|
806
|
+
revealed_boundaries.push(boundary);
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
|
|
498
810
|
var next = effect.next;
|
|
499
811
|
|
|
500
812
|
if (next !== null) {
|
|
@@ -517,8 +829,7 @@ export class Batch {
|
|
|
517
829
|
|
|
518
830
|
/** @param {Batch} batch */
|
|
519
831
|
#intersects(batch) {
|
|
520
|
-
for (const
|
|
521
|
-
if (is_derived) continue;
|
|
832
|
+
for (const source of this.current.keys()) {
|
|
522
833
|
if (batch.current.has(source)) return true;
|
|
523
834
|
}
|
|
524
835
|
|
|
@@ -526,73 +837,86 @@ export class Batch {
|
|
|
526
837
|
}
|
|
527
838
|
|
|
528
839
|
/**
|
|
840
|
+
* Take over the writes of a later batch that depends on this one, because it finished first
|
|
841
|
+
* or aborted one of this batch's runs.
|
|
842
|
+
* Values it produced (async results, deriveds) remain valid; block effects select their
|
|
843
|
+
* branches again for this batch, if needed (their selections are keyed by batch)
|
|
529
844
|
* @param {Batch} batch
|
|
530
845
|
*/
|
|
531
846
|
#merge(batch) {
|
|
532
|
-
for (const
|
|
533
|
-
|
|
534
|
-
|
|
847
|
+
for (const target of batch.merge_requests) this.merge_requests.add(target);
|
|
848
|
+
batch.merge_requests.clear();
|
|
849
|
+
|
|
850
|
+
for (const [source, entry] of batch.current) {
|
|
851
|
+
if (!this.previous.has(source)) {
|
|
852
|
+
const previous = batch.previous.get(source);
|
|
853
|
+
if (previous) this.previous.set(source, previous);
|
|
535
854
|
}
|
|
536
855
|
|
|
537
|
-
this.current.set(source,
|
|
856
|
+
this.current.set(source, entry);
|
|
538
857
|
}
|
|
539
858
|
|
|
540
859
|
for (const [effect, deferred] of batch.async_deriveds) {
|
|
541
860
|
const d = this.async_deriveds.get(effect);
|
|
542
|
-
if (d) deferred.promise.then(d.resolve
|
|
861
|
+
if (d && d !== deferred) deferred.promise.then(d.resolve, d.reject);
|
|
862
|
+
// The incoming run can still be pending. A subsequent replacement must reach
|
|
863
|
+
// this deferred too, not just the older waiters to which it forwards its result.
|
|
864
|
+
this.async_deriveds.set(effect, deferred);
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
for (const [reaction, record] of batch.records ?? []) {
|
|
868
|
+
if ((reaction.f & BLOCK_EFFECT) === 0) {
|
|
869
|
+
var replaced = this.records?.get(reaction);
|
|
870
|
+
(this.records ??= new Map()).set(reaction, record);
|
|
871
|
+
|
|
872
|
+
// the run moves to this batch
|
|
873
|
+
batch.records?.delete(reaction);
|
|
874
|
+
|
|
875
|
+
// (once it's replaced, as both batches may hold the same record, see `keep_global_run`)
|
|
876
|
+
abort_record(reaction, replaced);
|
|
877
|
+
}
|
|
543
878
|
}
|
|
544
879
|
|
|
545
880
|
// Clear them or else those that are still pending might get rejected on discard (after merged-into batch is done).
|
|
546
881
|
// This can happen when batch Y merged into X and Y has a pending boundary and therefore still-pending async deriveds inside.
|
|
547
882
|
batch.async_deriveds.clear();
|
|
548
883
|
|
|
549
|
-
//
|
|
550
|
-
this
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
* async effects that we just resolved (TODO unless they depend
|
|
555
|
-
* on values in this batch that are NOT in the later batch?).
|
|
556
|
-
* Through this we also will populate the correct #skipped_branches,
|
|
557
|
-
* oncommit callbacks etc, so we don't need to merge them separately.
|
|
558
|
-
* @param {Value} value
|
|
559
|
-
*/
|
|
560
|
-
const mark = (value) => {
|
|
561
|
-
var reactions = value.reactions;
|
|
562
|
-
if (reactions === null) return;
|
|
563
|
-
// skip if value is derived and is neither dirty nor maybe dirty. transitive
|
|
564
|
-
// deriveds (a derived depending on another derived) are only MAYBE_DIRTY, so
|
|
565
|
-
// we must continue traversing them to reach the effects that depend on them
|
|
566
|
-
if ((value.f & DERIVED) !== 0 && (value.f & (DIRTY | MAYBE_DIRTY)) === 0) {
|
|
567
|
-
return;
|
|
884
|
+
// That pending work is this batch's from now on: it decrements this batch's counts (see `increment_pending`),
|
|
885
|
+
// so this batch stays linked until the work is done, and receives the effects that a boundary defers until then
|
|
886
|
+
if (batch.#pending.size > 0) {
|
|
887
|
+
for (const [effect, count] of batch.#pending) {
|
|
888
|
+
this.#pending.set(effect, (this.#pending.get(effect) ?? 0) + count);
|
|
568
889
|
}
|
|
569
890
|
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
if ((flags & DERIVED) !== 0) {
|
|
574
|
-
mark(/** @type {Derived} */ (reaction));
|
|
575
|
-
} else {
|
|
576
|
-
var effect = /** @type {Effect} */ (reaction);
|
|
891
|
+
batch.#pending.clear();
|
|
892
|
+
}
|
|
577
893
|
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
this.schedule(effect);
|
|
582
|
-
}
|
|
583
|
-
}
|
|
894
|
+
if (batch.#unprocessed.size > 0) {
|
|
895
|
+
for (const [effect, status] of batch.#unprocessed) {
|
|
896
|
+
this.#add_dirty_effect(effect, status);
|
|
584
897
|
}
|
|
585
|
-
};
|
|
586
898
|
|
|
587
|
-
|
|
588
|
-
mark(source);
|
|
899
|
+
batch.#unprocessed.clear();
|
|
589
900
|
}
|
|
590
901
|
|
|
591
|
-
|
|
902
|
+
// What the merged batch did on its own (e.g. branches it selected) is obsolete once this batch is
|
|
903
|
+
// done — whether it commits or is discarded (e.g. because of an error)
|
|
904
|
+
const discard = () => batch.discard();
|
|
905
|
+
this.oncommit(discard);
|
|
906
|
+
this.ondiscard(discard);
|
|
907
|
+
batch.merged = this;
|
|
592
908
|
batch.#unlink();
|
|
909
|
+
}
|
|
593
910
|
|
|
594
|
-
|
|
595
|
-
|
|
911
|
+
/**
|
|
912
|
+
* Note that `effect` needs to be (re-)validated, without downgrading a DIRTY effect to MAYBE_DIRTY
|
|
913
|
+
* @param {Effect} effect
|
|
914
|
+
* @param {number} status DIRTY or MAYBE_DIRTY
|
|
915
|
+
*/
|
|
916
|
+
#add_dirty_effect(effect, status) {
|
|
917
|
+
if (status === DIRTY || this.#unprocessed.get(effect) !== DIRTY) {
|
|
918
|
+
this.#unprocessed.set(effect, status);
|
|
919
|
+
}
|
|
596
920
|
}
|
|
597
921
|
|
|
598
922
|
/**
|
|
@@ -600,75 +924,146 @@ export class Batch {
|
|
|
600
924
|
*/
|
|
601
925
|
#defer_effects(effects) {
|
|
602
926
|
for (var i = 0; i < effects.length; i += 1) {
|
|
603
|
-
|
|
927
|
+
var effect = effects[i];
|
|
928
|
+
var status = effect.f & (DIRTY | MAYBE_DIRTY);
|
|
929
|
+
|
|
930
|
+
if (status !== 0) {
|
|
931
|
+
this.#add_dirty_effect(effect, effect.dv === null ? MAYBE_DIRTY : status);
|
|
932
|
+
|
|
933
|
+
// mark as clean so they get scheduled if they depend on pending async state
|
|
934
|
+
set_signal_status(effect, CLEAN);
|
|
935
|
+
}
|
|
604
936
|
}
|
|
605
937
|
}
|
|
606
938
|
|
|
607
939
|
/**
|
|
608
940
|
* Associate a change to a given source with the current
|
|
609
|
-
* batch, noting its previous and current values
|
|
941
|
+
* batch, noting its previous and current values (and versions).
|
|
942
|
+
* The source itself is only updated if this is its latest value, i.e. unless this
|
|
943
|
+
* is a fork (whose writes only exist in its own view of the world) or a later batch
|
|
944
|
+
* already wrote to it (e.g. because an async expression resolved in an earlier batch).
|
|
610
945
|
* @param {Value} source
|
|
611
946
|
* @param {any} value
|
|
612
|
-
* @param {boolean}
|
|
947
|
+
* @param {boolean} same whether the value equals the real one (it can, if this batch sees another one, see
|
|
948
|
+
* `internal_set`) — in which case the source isn't updated, and the value gets the same version
|
|
949
|
+
* @param {boolean} [error] whether the value is an error (thrown when read, see `runtime.js#get`)
|
|
950
|
+
* @param {Effect[] | null} [updated_during_traversal]
|
|
613
951
|
*/
|
|
614
|
-
capture(source, value,
|
|
615
|
-
|
|
616
|
-
|
|
952
|
+
capture(source, value, same, error = false, updated_during_traversal = null) {
|
|
953
|
+
var previous = this.previous.get(source);
|
|
954
|
+
var later = later_writer(this, source);
|
|
955
|
+
|
|
956
|
+
if (previous === undefined) {
|
|
957
|
+
// the value before this batch's change is the one before the later batch's change, if any
|
|
958
|
+
previous = later?.previous.get(source);
|
|
959
|
+
|
|
960
|
+
// (A first value has no value from before it, see `previous`. If the later batch's write is the first value,
|
|
961
|
+
// the value before this batch's write is taken to be that one: in the views of other batches and in
|
|
962
|
+
// teardowns, undoing this write then gives the real value, just like not undoing it would)
|
|
963
|
+
if (previous === undefined && source.v !== UNINITIALIZED) {
|
|
964
|
+
previous = {
|
|
965
|
+
v: source.v,
|
|
966
|
+
wv: source.wv,
|
|
967
|
+
e: (source.f & ERROR_VALUE) !== 0,
|
|
968
|
+
assigned: (source.f & ASSIGNED) !== 0
|
|
969
|
+
};
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
if (previous !== undefined) this.previous.set(source, previous);
|
|
617
973
|
}
|
|
618
974
|
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
975
|
+
if (is_destroying_effect) {
|
|
976
|
+
teardown_write(source);
|
|
977
|
+
} else if (previous !== undefined) {
|
|
978
|
+
teardown_context?.reverted_values.set(source, previous);
|
|
979
|
+
teardown_context?.derived_cache.clear();
|
|
623
980
|
}
|
|
624
981
|
|
|
625
|
-
|
|
626
|
-
|
|
982
|
+
/** @type {BatchValue} */
|
|
983
|
+
var entry = {
|
|
984
|
+
v: value,
|
|
985
|
+
wv: same ? source.wv : increment_write_version(),
|
|
986
|
+
e: error,
|
|
987
|
+
// a write to a derived is an assignment
|
|
988
|
+
assigned: (source.f & DERIVED) !== 0,
|
|
989
|
+
async: (source.f & ASYNC) !== 0
|
|
990
|
+
};
|
|
991
|
+
|
|
992
|
+
this.current.set(source, entry);
|
|
993
|
+
|
|
994
|
+
// The active view of the world sees writes made while it's active, even if they don't belong to the batch whose
|
|
995
|
+
// view it is — e.g. those of effects that run while a batch commits, which go to a new batch. (Async results
|
|
996
|
+
// are only captured while no view is active, see `async_derived`)
|
|
997
|
+
batch_values?.set(source, entry);
|
|
998
|
+
|
|
999
|
+
// an assignment replaces the value of a derived that the active view of the world holds
|
|
1000
|
+
view_cache?.delete(/** @type {Derived} */ (source));
|
|
1001
|
+
|
|
1002
|
+
// in order, this batch's change comes before that of the later batch, which replaces it (this happens when a
|
|
1003
|
+
// result arrives in a batch that committed while it was pending, see `#move_to_front`)
|
|
1004
|
+
if (!this.is_fork && later !== null) later.previous.set(source, entry);
|
|
1005
|
+
|
|
1006
|
+
if (!this.is_fork && !same && later === null) {
|
|
1007
|
+
apply(source, entry);
|
|
627
1008
|
}
|
|
628
|
-
}
|
|
629
1009
|
|
|
630
|
-
|
|
631
|
-
|
|
1010
|
+
// A value that equals the real one, with the same version, changes nothing that reactions
|
|
1011
|
+
// could read outside this batch's view — which is revalidated when the batch is processed
|
|
1012
|
+
if (!same) {
|
|
1013
|
+
seen = null;
|
|
1014
|
+
count_deps = 0;
|
|
1015
|
+
this.mark(source, DIRTY, updated_during_traversal);
|
|
1016
|
+
seen = null;
|
|
1017
|
+
|
|
1018
|
+
if (eager_effects.size > 0 && !eager_effects_deferred) {
|
|
1019
|
+
flush_eager_effects();
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
632
1022
|
}
|
|
633
1023
|
|
|
634
1024
|
deactivate() {
|
|
635
|
-
current_batch = null;
|
|
636
|
-
batch_values = null;
|
|
1025
|
+
current_batch = processing_batch = batch_values = view_cache = view_batch = null;
|
|
637
1026
|
}
|
|
638
1027
|
|
|
639
1028
|
flush() {
|
|
1029
|
+
// flushes can be nested, e.g. if an effect calls `flushSync`
|
|
1030
|
+
var context = push_teardown_context(this.previous);
|
|
1031
|
+
|
|
640
1032
|
try {
|
|
641
|
-
|
|
642
|
-
source_stacks.clear();
|
|
643
|
-
}
|
|
1033
|
+
this.#process();
|
|
644
1034
|
|
|
645
|
-
|
|
646
|
-
|
|
1035
|
+
// (the size check avoids allocating an iterator in the common case)
|
|
1036
|
+
if (rebases.size > 0) {
|
|
1037
|
+
for (const batch of rebases) {
|
|
1038
|
+
rebases.delete(batch);
|
|
647
1039
|
|
|
648
|
-
|
|
1040
|
+
// the batch is processed again anyway if a flush is queued
|
|
1041
|
+
if (batch.linked && !batch.#decrement_queued) {
|
|
1042
|
+
batch.#process();
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
649
1046
|
} finally {
|
|
650
1047
|
flush_count = 0;
|
|
651
1048
|
last_scheduled_effect = null;
|
|
652
1049
|
collected_effects = null;
|
|
653
1050
|
legacy_updates = null;
|
|
654
|
-
is_processing = false;
|
|
655
|
-
|
|
656
|
-
current_batch = null;
|
|
657
|
-
batch_values = null;
|
|
658
1051
|
|
|
659
|
-
|
|
1052
|
+
teardown_context = context.parent;
|
|
1053
|
+
this.deactivate();
|
|
660
1054
|
|
|
661
1055
|
if (DEV) {
|
|
662
1056
|
for (const source of source_stacks) {
|
|
663
1057
|
source.updated = null;
|
|
664
1058
|
}
|
|
1059
|
+
|
|
1060
|
+
source_stacks.clear();
|
|
665
1061
|
}
|
|
666
1062
|
}
|
|
667
1063
|
}
|
|
668
1064
|
|
|
669
1065
|
discard() {
|
|
670
|
-
|
|
671
|
-
this.#discard_callbacks.clear();
|
|
1066
|
+
this.#run_callbacks(this.#discard_callbacks);
|
|
672
1067
|
|
|
673
1068
|
for (const deferred of this.async_deriveds.values()) {
|
|
674
1069
|
deferred.reject(OBSOLETE);
|
|
@@ -678,34 +1073,29 @@ export class Batch {
|
|
|
678
1073
|
this.#deferred?.resolve();
|
|
679
1074
|
}
|
|
680
1075
|
|
|
681
|
-
/**
|
|
682
|
-
* @param {Effect} effect
|
|
683
|
-
*/
|
|
684
|
-
register_created_effect(effect) {
|
|
685
|
-
this.#new_effects.push(effect);
|
|
686
|
-
}
|
|
687
|
-
|
|
688
1076
|
#commit() {
|
|
689
1077
|
// If there are other pending batches, they now need to be 'rebased' —
|
|
690
|
-
// in other words,
|
|
691
|
-
//
|
|
692
|
-
//
|
|
1078
|
+
// in other words, their view of the world now includes the newly committed
|
|
1079
|
+
// state (unless the batch in question has a more recent value for a given source),
|
|
1080
|
+
// so everything that depends on it needs to be re-validated in their view
|
|
693
1081
|
for (let batch = first_batch; batch !== null; batch = batch.next) {
|
|
1082
|
+
if (batch === this) continue;
|
|
1083
|
+
|
|
694
1084
|
var is_earlier = batch.id < this.id;
|
|
695
1085
|
|
|
696
1086
|
/** @type {Source[]} */
|
|
697
1087
|
var sources = [];
|
|
698
1088
|
|
|
699
|
-
for (const [source,
|
|
700
|
-
|
|
701
|
-
var batch_value = /** @type {[any, boolean]} */ (batch.current.get(source))[0]; // faster than destructuring
|
|
1089
|
+
for (const [source, entry] of this.current) {
|
|
1090
|
+
var own = batch.current.get(source);
|
|
702
1091
|
|
|
703
|
-
|
|
1092
|
+
if (own !== undefined) {
|
|
1093
|
+
if (is_earlier && entry.v !== own.v && !own.async) {
|
|
704
1094
|
// bring the value up to date
|
|
705
|
-
batch.current.set(source,
|
|
1095
|
+
batch.current.set(source, entry);
|
|
706
1096
|
} else {
|
|
707
|
-
// same value
|
|
708
|
-
// no need to re-run these effects
|
|
1097
|
+
// same value, later batch has more recent value, or the batch's own result
|
|
1098
|
+
// of an async expression — no need to re-run these effects
|
|
709
1099
|
continue;
|
|
710
1100
|
}
|
|
711
1101
|
}
|
|
@@ -713,30 +1103,21 @@ export class Batch {
|
|
|
713
1103
|
sources.push(source);
|
|
714
1104
|
}
|
|
715
1105
|
|
|
716
|
-
if (is_earlier) {
|
|
717
|
-
// TODO do we need to restart these in some cases, instead of
|
|
718
|
-
// immediately resolving them? Likely not because of how this.apply() works.
|
|
719
|
-
for (const [effect, deferred] of this.async_deriveds) {
|
|
720
|
-
const d = batch.async_deriveds.get(effect);
|
|
721
|
-
if (d) deferred.promise.then(d.resolve).catch(d.reject);
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
var current = [...batch.current.keys()].filter(
|
|
726
|
-
(source) => !(/** @type {[any, boolean]} */ (batch.current.get(source))[1])
|
|
727
|
-
);
|
|
728
|
-
|
|
729
1106
|
// If not started yet or no sources to update (which is e.g. possible for the very first batch) then bail
|
|
730
|
-
if (!batch.started || current.
|
|
1107
|
+
if (!batch.started || batch.current.size === 0) continue;
|
|
731
1108
|
|
|
732
|
-
|
|
733
|
-
var others = current.filter((source) => !this.current.has(source));
|
|
1109
|
+
var obsolete = true;
|
|
734
1110
|
|
|
735
|
-
|
|
736
|
-
if (
|
|
737
|
-
|
|
738
|
-
|
|
1111
|
+
for (const source of batch.current.keys()) {
|
|
1112
|
+
if (!this.current.has(source)) {
|
|
1113
|
+
obsolete = false;
|
|
1114
|
+
break;
|
|
739
1115
|
}
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
if (obsolete && is_earlier) {
|
|
1119
|
+
// this batch is now obsolete and can be discarded
|
|
1120
|
+
batch.discard();
|
|
740
1121
|
} else if (sources.length > 0) {
|
|
741
1122
|
// The microtask queue can contain the batch already scheduled to run right
|
|
742
1123
|
// after this one is finished, so throwing the invariant would be wrong here.
|
|
@@ -744,100 +1125,49 @@ export class Batch {
|
|
|
744
1125
|
invariant(batch.#scheduled.length === 0, 'Batch has scheduled effects');
|
|
745
1126
|
}
|
|
746
1127
|
|
|
747
|
-
//
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
batch.#defer_effects([e]);
|
|
755
|
-
}
|
|
756
|
-
});
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
|
|
760
|
-
batch.activate();
|
|
761
|
-
|
|
762
|
-
/** @type {Set<Value>} */
|
|
763
|
-
var marked = new Set();
|
|
764
|
-
|
|
765
|
-
/** @type {Map<Reaction, boolean>} */
|
|
766
|
-
var checked = new Map();
|
|
767
|
-
|
|
768
|
-
for (var source of sources) {
|
|
769
|
-
mark_effects(source, others, marked, checked);
|
|
770
|
-
}
|
|
771
|
-
|
|
772
|
-
checked = new Map();
|
|
773
|
-
var current_unequal = [...batch.current]
|
|
774
|
-
.filter(([c, v1]) => {
|
|
775
|
-
const v2 = this.current.get(c);
|
|
776
|
-
if (!v2) return true;
|
|
777
|
-
// Either their values are different or one is a derived but not the other
|
|
778
|
-
return v2[0] !== v1[0] || v2[1] !== v1[1];
|
|
779
|
-
})
|
|
780
|
-
.map(([c]) => c);
|
|
781
|
-
|
|
782
|
-
if (current_unequal.length > 0) {
|
|
783
|
-
for (const effect of this.#new_effects) {
|
|
784
|
-
if (
|
|
785
|
-
(effect.f & (DESTROYED | INERT | EAGER_EFFECT)) === 0 &&
|
|
786
|
-
depends_on(effect, current_unequal, checked)
|
|
787
|
-
) {
|
|
788
|
-
if ((effect.f & (ASYNC | BLOCK_EFFECT)) !== 0) {
|
|
789
|
-
set_signal_status(effect, DIRTY);
|
|
790
|
-
batch.schedule(effect);
|
|
791
|
-
} else {
|
|
792
|
-
batch.add_dirty_reaction(effect, DIRTY);
|
|
793
|
-
}
|
|
794
|
-
}
|
|
795
|
-
}
|
|
796
|
-
}
|
|
797
|
-
|
|
798
|
-
// Only apply and traverse when we know we triggered async work with marking the effects
|
|
799
|
-
// and know this won't run anyway right afterwards
|
|
800
|
-
if (batch.#scheduled.length > 0 && !batch.#decrement_queued) {
|
|
801
|
-
batch.apply();
|
|
802
|
-
|
|
803
|
-
for (var root of batch.#resolve()) {
|
|
804
|
-
batch.#traverse(root, [], []);
|
|
805
|
-
}
|
|
806
|
-
}
|
|
807
|
-
|
|
808
|
-
batch.deactivate();
|
|
1128
|
+
// Everything that depends on the committed values needs to be re-validated in the
|
|
1129
|
+
// batch's view. The batch is processed again once the current flush is done (which
|
|
1130
|
+
// also re-validates what depends on its own writes, as the committed batch may have
|
|
1131
|
+
// created reactions that read them), so that block effects select their branches
|
|
1132
|
+
// and async work restarts in the new view right away
|
|
1133
|
+
batch.#revalidate(sources);
|
|
1134
|
+
rebases.add(batch);
|
|
809
1135
|
}
|
|
810
1136
|
}
|
|
1137
|
+
|
|
1138
|
+
// The writes are part of the real world now. A batch that is still linked (because of pending work)
|
|
1139
|
+
// only has writes again once that work resolves, which it then commits
|
|
1140
|
+
this.current.clear();
|
|
1141
|
+
this.previous.clear();
|
|
811
1142
|
}
|
|
812
1143
|
|
|
813
1144
|
/**
|
|
814
|
-
*
|
|
815
|
-
*
|
|
1145
|
+
* Make this batch's writes the real world, part of every other batch's view (see `#commit`).
|
|
1146
|
+
* If it has pending work it stays linked, coming first from now on (see `#move_to_front`)
|
|
816
1147
|
*/
|
|
817
|
-
|
|
818
|
-
this.#
|
|
819
|
-
|
|
820
|
-
if (blocking) {
|
|
821
|
-
let blocking_pending_count = this.#blocking_pending.get(effect) ?? 0;
|
|
822
|
-
this.#blocking_pending.set(effect, blocking_pending_count + 1);
|
|
823
|
-
}
|
|
1148
|
+
commit_writes() {
|
|
1149
|
+
this.#commit();
|
|
1150
|
+
if (this.linked && this.prev !== null) this.#move_to_front();
|
|
824
1151
|
}
|
|
825
1152
|
|
|
826
1153
|
/**
|
|
827
|
-
* @param {boolean} blocking
|
|
828
1154
|
* @param {Effect} effect
|
|
829
1155
|
*/
|
|
830
|
-
|
|
831
|
-
this.#pending
|
|
1156
|
+
increment(effect) {
|
|
1157
|
+
var count = this.#pending.get(effect) ?? 0;
|
|
1158
|
+
this.#pending.set(effect, count + 1);
|
|
1159
|
+
}
|
|
832
1160
|
|
|
833
|
-
|
|
834
|
-
|
|
1161
|
+
/**
|
|
1162
|
+
* @param {Effect} effect
|
|
1163
|
+
*/
|
|
1164
|
+
decrement(effect) {
|
|
1165
|
+
var count = this.#pending.get(effect) ?? 0;
|
|
835
1166
|
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
}
|
|
1167
|
+
if (count === 1) {
|
|
1168
|
+
this.#pending.delete(effect);
|
|
1169
|
+
} else {
|
|
1170
|
+
this.#pending.set(effect, count - 1);
|
|
841
1171
|
}
|
|
842
1172
|
|
|
843
1173
|
if (this.#decrement_queued) return;
|
|
@@ -852,35 +1182,48 @@ export class Batch {
|
|
|
852
1182
|
});
|
|
853
1183
|
}
|
|
854
1184
|
|
|
855
|
-
/**
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
for (const [effect, status] of dirty_reactions) {
|
|
860
|
-
this.add_dirty_reaction(effect, status);
|
|
861
|
-
}
|
|
1185
|
+
/** @param {(batch: Batch) => void} fn */
|
|
1186
|
+
oncommit(fn) {
|
|
1187
|
+
this.#commit_callbacks.set(fn, eager_block_writes?.size ? eager_block_writes : null);
|
|
1188
|
+
}
|
|
862
1189
|
|
|
863
|
-
|
|
1190
|
+
/** @param {(batch: Batch) => void} fn */
|
|
1191
|
+
ondiscard(fn) {
|
|
1192
|
+
this.#discard_callbacks.set(fn, eager_block_writes?.size ? eager_block_writes : null);
|
|
864
1193
|
}
|
|
865
1194
|
|
|
866
1195
|
/**
|
|
867
|
-
*
|
|
868
|
-
*
|
|
1196
|
+
* Runs the commit/discard callbacks.
|
|
1197
|
+
* A callback that was added while block effects re-ran eagerly detaches the branches that such a run
|
|
1198
|
+
* deselected (see `flush_queued_effects`). The teardowns it causes see the writes that applied in the
|
|
1199
|
+
* run's view, like they would have during the run: of the values that the current teardown context
|
|
1200
|
+
* reverts, the writes of the effects that ran before the one whose writes caused the run are restored.
|
|
1201
|
+
* Callbacks run untracked, because a batch can commit while an effect runs (e.g. one that calls `flushSync`)
|
|
1202
|
+
* @param {Map<(batch: Batch) => void, Map<Value, BatchValue> | null>} callbacks
|
|
869
1203
|
*/
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
1204
|
+
#run_callbacks(callbacks) {
|
|
1205
|
+
untrack(() => {
|
|
1206
|
+
for (const [fn, writes] of callbacks) {
|
|
1207
|
+
if (writes === null) {
|
|
1208
|
+
fn(this);
|
|
1209
|
+
continue;
|
|
1210
|
+
}
|
|
875
1211
|
|
|
876
|
-
|
|
877
|
-
oncommit(fn) {
|
|
878
|
-
this.#commit_callbacks.add(fn);
|
|
879
|
-
}
|
|
1212
|
+
var context = push_teardown_context();
|
|
880
1213
|
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
1214
|
+
for (const [source, write] of writes) {
|
|
1215
|
+
if (context.reverted_values.has(source)) context.reverted_values.set(source, write);
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
try {
|
|
1219
|
+
fn(this);
|
|
1220
|
+
} finally {
|
|
1221
|
+
teardown_context = context.parent;
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
});
|
|
1225
|
+
|
|
1226
|
+
callbacks.clear();
|
|
884
1227
|
}
|
|
885
1228
|
|
|
886
1229
|
settled() {
|
|
@@ -891,7 +1234,7 @@ export class Batch {
|
|
|
891
1234
|
if (current_batch === null) {
|
|
892
1235
|
const batch = (current_batch = new Batch());
|
|
893
1236
|
|
|
894
|
-
if (
|
|
1237
|
+
if (processing_batch === null && !is_flushing_sync) {
|
|
895
1238
|
queue_micro_task(() => {
|
|
896
1239
|
if (!batch.started) {
|
|
897
1240
|
batch.flush();
|
|
@@ -903,18 +1246,34 @@ export class Batch {
|
|
|
903
1246
|
return current_batch;
|
|
904
1247
|
}
|
|
905
1248
|
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
1249
|
+
/**
|
|
1250
|
+
* Make this the current batch, and its view of the world the active one
|
|
1251
|
+
*/
|
|
1252
|
+
activate() {
|
|
1253
|
+
current_batch = this;
|
|
1254
|
+
view_batch = this;
|
|
1255
|
+
|
|
1256
|
+
// Async continuations can create or run reactions in another batch's view without
|
|
1257
|
+
// processing it. The next flush must re-validate those reactions in its own view.
|
|
1258
|
+
if (last_processed !== this.id) last_processed = null;
|
|
1259
|
+
|
|
1260
|
+
if (!async_mode_flag) {
|
|
1261
|
+
batch_values = view_cache = null;
|
|
1262
|
+
return;
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
this.folded = [];
|
|
1266
|
+
|
|
1267
|
+
if (!this.is_fork && this.prev === null && this.next === null) {
|
|
1268
|
+
// this batch's view is the real world
|
|
1269
|
+
batch_values = view_cache = null;
|
|
909
1270
|
return;
|
|
910
1271
|
}
|
|
911
1272
|
|
|
912
1273
|
// if there are multiple batches, we are 'time travelling' —
|
|
913
1274
|
// we need to override values with the ones in this batch...
|
|
914
|
-
batch_values = new Map();
|
|
915
|
-
|
|
916
|
-
batch_values.set(source, value);
|
|
917
|
-
}
|
|
1275
|
+
batch_values = new Map(this.current);
|
|
1276
|
+
view_cache = new Map();
|
|
918
1277
|
|
|
919
1278
|
// ...and undo changes belonging to other batches unless they intersect
|
|
920
1279
|
for (let batch = first_batch; batch !== null; batch = batch.next) {
|
|
@@ -925,9 +1284,16 @@ export class Batch {
|
|
|
925
1284
|
// effects of the earlier batch in case they are merged.
|
|
926
1285
|
// As a result you can think of batch_values as having the latest values of all intersecting
|
|
927
1286
|
// batches up until this batch.
|
|
928
|
-
if (batch.id
|
|
1287
|
+
if (batch.id < this.id && batch.#intersects(this)) {
|
|
1288
|
+
this.folded.push(batch);
|
|
1289
|
+
} else {
|
|
929
1290
|
for (const [source, previous] of batch.previous) {
|
|
930
|
-
|
|
1291
|
+
// (where the views agree on the value, the real one — and its version — applies. Not for an
|
|
1292
|
+
// assigned derived: its value before may have been computed in another view, see `get_view`)
|
|
1293
|
+
if (
|
|
1294
|
+
!batch_values.has(source) &&
|
|
1295
|
+
((source.f & DERIVED) !== 0 || !source.equals(previous.v))
|
|
1296
|
+
) {
|
|
931
1297
|
batch_values.set(source, previous);
|
|
932
1298
|
}
|
|
933
1299
|
}
|
|
@@ -936,26 +1302,212 @@ export class Batch {
|
|
|
936
1302
|
}
|
|
937
1303
|
|
|
938
1304
|
/**
|
|
939
|
-
*
|
|
1305
|
+
* The record of `reaction` in this batch's view of the world, i.e. that of this batch
|
|
1306
|
+
* or of the (earlier) batches whose changes it includes, if any
|
|
1307
|
+
* @param {Reaction} reaction
|
|
1308
|
+
* @returns {BatchRecord | undefined}
|
|
1309
|
+
*/
|
|
1310
|
+
get_record(reaction) {
|
|
1311
|
+
var record = this.records?.get(reaction);
|
|
1312
|
+
var i = this.folded.length;
|
|
1313
|
+
|
|
1314
|
+
while (record === undefined && i--) {
|
|
1315
|
+
record = this.folded[i].records?.get(reaction);
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
return record;
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
/**
|
|
1322
|
+
* @param {Value} signal
|
|
1323
|
+
* @param {number} status should be DIRTY or MAYBE_DIRTY
|
|
1324
|
+
* @param {Effect[] | null} [updated_during_traversal]
|
|
1325
|
+
* @returns {void}
|
|
1326
|
+
*/
|
|
1327
|
+
mark(signal, status, updated_during_traversal = null) {
|
|
1328
|
+
var reactions = signal.reactions;
|
|
1329
|
+
if (reactions === null) return;
|
|
1330
|
+
|
|
1331
|
+
var runes = is_runes();
|
|
1332
|
+
var length = reactions.length;
|
|
1333
|
+
|
|
1334
|
+
count_deps += length;
|
|
1335
|
+
// Activate the `seen` Set if we think from the unusually high number of deps that
|
|
1336
|
+
// there might be cycles in the graph, to avoid repeated lookups for reactions
|
|
1337
|
+
// Example: https://github.com/sveltejs/svelte/issues/16658 has a graph with one source
|
|
1338
|
+
// reaching ~10000 distinct deriveds/effects each, resulting in 65 million walks through repeated visits.
|
|
1339
|
+
if (count_deps > 100000 && seen === null) seen = new Set();
|
|
1340
|
+
|
|
1341
|
+
if (seen !== null) {
|
|
1342
|
+
if (seen.has(signal)) return;
|
|
1343
|
+
seen.add(signal);
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
for (var i = 0; i < length; i++) {
|
|
1347
|
+
var reaction = reactions[i];
|
|
1348
|
+
var flags = reaction.f;
|
|
1349
|
+
|
|
1350
|
+
// In legacy mode, skip the current effect to prevent infinite loops
|
|
1351
|
+
if (!runes && reaction === active_effect) continue;
|
|
1352
|
+
|
|
1353
|
+
var not_dirty = (flags & DIRTY) === 0;
|
|
1354
|
+
|
|
1355
|
+
if ((flags & EAGER_EFFECT) !== 0) {
|
|
1356
|
+
if (!this.is_fork) {
|
|
1357
|
+
// don't set a DIRTY reaction to MAYBE_DIRTY
|
|
1358
|
+
if (not_dirty) {
|
|
1359
|
+
set_signal_status(reaction, status);
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
// Eager effects need to run immediately:
|
|
1363
|
+
// - for $inspect so that the stack trace makes sense
|
|
1364
|
+
// - for $state.eager because they might be without an effect parent
|
|
1365
|
+
eager_effects.add(/** @type {Effect} */ (reaction));
|
|
1366
|
+
}
|
|
1367
|
+
} else if ((flags & DERIVED) !== 0) {
|
|
1368
|
+
// don't set a DIRTY reaction to MAYBE_DIRTY
|
|
1369
|
+
if (not_dirty) {
|
|
1370
|
+
set_signal_status(reaction, status);
|
|
1371
|
+
}
|
|
1372
|
+
|
|
1373
|
+
var derived = /** @type {Derived} */ (reaction);
|
|
1374
|
+
|
|
1375
|
+
view_cache?.delete(derived);
|
|
1376
|
+
this.mark(derived, MAYBE_DIRTY, updated_during_traversal);
|
|
1377
|
+
} else if (not_dirty) {
|
|
1378
|
+
var effect = /** @type {Effect} */ (reaction);
|
|
1379
|
+
|
|
1380
|
+
if ((flags & BLOCK_EFFECT) !== 0 && eager_block_effects !== null) {
|
|
1381
|
+
// these run right away to find out which branches the rest of the flush must skip
|
|
1382
|
+
// (see `flush_queued_effects`); the DOM changes when this batch commits
|
|
1383
|
+
set_signal_status(effect, status);
|
|
1384
|
+
eager_block_effects.add(effect);
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
if (updated_during_traversal !== null) {
|
|
1388
|
+
set_signal_status(effect, status);
|
|
1389
|
+
updated_during_traversal.push(effect);
|
|
1390
|
+
} else {
|
|
1391
|
+
// the status is applied when the batch is processed
|
|
1392
|
+
this.schedule(effect, status);
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1398
|
+
/**
|
|
1399
|
+
* Schedule `effect` to be (re-)validated when this batch is processed. Its status only applies right
|
|
1400
|
+
* away if this is the batch that the current flush processes (next), or if a reaction is running (which
|
|
1401
|
+
* may have read what changed). Otherwise, whether an effect is dirty depends on this batch's view of the
|
|
1402
|
+
* world, so other batches (which may be processed first) mustn't see it: it is kept with this batch until
|
|
1403
|
+
* it is processed, and then checked against the exact versions of its dependencies (i.e. MAYBE_DIRTY)
|
|
940
1404
|
* @param {Effect} effect
|
|
1405
|
+
* @param {number} status DIRTY or MAYBE_DIRTY
|
|
941
1406
|
*/
|
|
942
|
-
schedule(effect) {
|
|
1407
|
+
schedule(effect, status) {
|
|
943
1408
|
last_scheduled_effect = effect;
|
|
944
1409
|
|
|
945
|
-
//
|
|
946
|
-
//
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
(
|
|
951
|
-
|
|
952
|
-
|
|
1410
|
+
// While another batch is committed, effects that this batch's writes affect (other than the one
|
|
1411
|
+
// that is running, i.e. writing) wait for it to be processed, rather than running as part of that commit.
|
|
1412
|
+
// They may still run as part of it (for that batch's reasons) and see the write, so versions decide — unless
|
|
1413
|
+
// the effect is running, in which case it would record the version it didn't see (see `update_dependencies`)
|
|
1414
|
+
if (view_batch !== null && view_batch !== this && effect !== active_reaction) {
|
|
1415
|
+
this.#add_dirty_effect(
|
|
1416
|
+
effect,
|
|
1417
|
+
(effect.f & REACTION_IS_UPDATING) !== 0 ? status : MAYBE_DIRTY
|
|
1418
|
+
);
|
|
953
1419
|
return;
|
|
954
1420
|
}
|
|
955
1421
|
|
|
1422
|
+
if (processing_batch !== this && active_reaction === null) {
|
|
1423
|
+
if (this.prev !== null || this.next !== null || this.is_fork) {
|
|
1424
|
+
this.#add_dirty_effect(effect, MAYBE_DIRTY);
|
|
1425
|
+
return;
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
status = MAYBE_DIRTY;
|
|
1429
|
+
}
|
|
1430
|
+
|
|
1431
|
+
this.#add(effect, status);
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1434
|
+
/**
|
|
1435
|
+
* @param {Effect} effect
|
|
1436
|
+
* @param {number} status
|
|
1437
|
+
*/
|
|
1438
|
+
#add(effect, status) {
|
|
1439
|
+
// don't set a DIRTY effect to MAYBE_DIRTY
|
|
1440
|
+
if ((effect.f & DIRTY) === 0) {
|
|
1441
|
+
set_signal_status(effect, status);
|
|
1442
|
+
}
|
|
1443
|
+
|
|
956
1444
|
this.#scheduled.push(effect);
|
|
957
1445
|
}
|
|
958
1446
|
|
|
1447
|
+
/** @param {boolean} commit whether the effects' runs become global state */
|
|
1448
|
+
#release_records(commit = false) {
|
|
1449
|
+
var records = this.records;
|
|
1450
|
+
if (records === null) return;
|
|
1451
|
+
|
|
1452
|
+
// Release ownership before pruning. Merged runs are retained by the receiving batch
|
|
1453
|
+
this.records = null;
|
|
1454
|
+
for (const [reaction, record] of records) {
|
|
1455
|
+
// A derived's record is a run in this batch's view: it is only the real-world value if its
|
|
1456
|
+
// dependencies still hold what it read, which is checked where a record is promoted on read
|
|
1457
|
+
// (see `update_derived`) and cannot be checked here. So it is dropped rather than promoted,
|
|
1458
|
+
// and its edges are left for its next global run to prune (see `prune_edges`). A destroyed
|
|
1459
|
+
// reaction has nothing left to update either
|
|
1460
|
+
if ((reaction.f & (DERIVED | DESTROYED)) === 0) {
|
|
1461
|
+
// Keep a newer committed async run instead of promoting an outdated record.
|
|
1462
|
+
// Otherwise, the effect would rerun and restart its async work in every batch.
|
|
1463
|
+
var replaced = (reaction.f & ASYNC) !== 0 && is_outdated(record) && !is_outdated(reaction);
|
|
1464
|
+
|
|
1465
|
+
if (commit && !replaced) {
|
|
1466
|
+
promote_record(reaction, record);
|
|
1467
|
+
|
|
1468
|
+
// The committed run is now what applies in the view of every batch without a run of the async
|
|
1469
|
+
// effect of its own (typically because the batch committed while its run was pending, inside a
|
|
1470
|
+
// pending boundary). Their in-flight runs are superseded: their results would overwrite this one
|
|
1471
|
+
// TODO feels pretty hacky and indication of an X-Y problem: This only works because !b.records?.has(reaction)
|
|
1472
|
+
// means "this would re-use the real world result, i.e. the one we just committed, so we can reject this old run".
|
|
1473
|
+
// This can reject runs too early in pending batches. The superseding batch eventually settles them,
|
|
1474
|
+
// but async/if blocks inside pending boundaries should be excluded.
|
|
1475
|
+
if ((reaction.f & ASYNC) !== 0) {
|
|
1476
|
+
for (let b = first_batch; b !== null; b = b.next) {
|
|
1477
|
+
if (b !== this && !b.records?.has(reaction)) {
|
|
1478
|
+
b.async_deriveds.get(/** @type {Effect} */ (reaction))?.reject(OBSOLETE);
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
}
|
|
1482
|
+
}
|
|
1483
|
+
|
|
1484
|
+
prune_edges(reaction);
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1487
|
+
// abort this run - if it was promoted to global, or another batch holds it, too, this is a noop
|
|
1488
|
+
abort_record(reaction, record);
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
/**
|
|
1493
|
+
* A batch that committed while work inside a pending boundary was in flight stays linked, so that the results
|
|
1494
|
+
* arrive in it. Its writes are the real world now, part of every other batch's view, so it comes first from now
|
|
1495
|
+
* on: This ensures logic about who sees what values in intersects/merge/etc work correctly.
|
|
1496
|
+
*/
|
|
1497
|
+
#move_to_front() {
|
|
1498
|
+
var prev = /** @type {Batch} */ (this.prev);
|
|
1499
|
+
var first = /** @type {Batch} */ (first_batch);
|
|
1500
|
+
|
|
1501
|
+
prev.next = this.next;
|
|
1502
|
+
if (this.next === null) last_batch = prev;
|
|
1503
|
+
else this.next.prev = prev;
|
|
1504
|
+
|
|
1505
|
+
this.prev = null;
|
|
1506
|
+
this.next = first;
|
|
1507
|
+
first.prev = first_batch = this;
|
|
1508
|
+
this.id = first.id - 1;
|
|
1509
|
+
}
|
|
1510
|
+
|
|
959
1511
|
#unlink() {
|
|
960
1512
|
// #merge calls #unlink, discard later on does it again - prevent
|
|
961
1513
|
// running it multiple times to not corrupt the linked list
|
|
@@ -977,6 +1529,7 @@ export class Batch {
|
|
|
977
1529
|
}
|
|
978
1530
|
|
|
979
1531
|
this.linked = false;
|
|
1532
|
+
this.#release_records();
|
|
980
1533
|
}
|
|
981
1534
|
}
|
|
982
1535
|
|
|
@@ -990,8 +1543,8 @@ export class Batch {
|
|
|
990
1543
|
*/
|
|
991
1544
|
export function flushSync(fn) {
|
|
992
1545
|
var was_flushing_sync = is_flushing_sync;
|
|
993
|
-
|
|
994
|
-
|
|
1546
|
+
// e.g. flushSync during effect flushing phase - we gotta restore the view_batch afterwards in that case
|
|
1547
|
+
var previous_view_batch = view_batch !== current_batch ? view_batch : null;
|
|
995
1548
|
is_flushing_sync = true;
|
|
996
1549
|
|
|
997
1550
|
try {
|
|
@@ -1014,7 +1567,7 @@ export function flushSync(fn) {
|
|
|
1014
1567
|
}
|
|
1015
1568
|
} finally {
|
|
1016
1569
|
is_flushing_sync = was_flushing_sync;
|
|
1017
|
-
|
|
1570
|
+
view_batch = previous_view_batch;
|
|
1018
1571
|
}
|
|
1019
1572
|
}
|
|
1020
1573
|
|
|
@@ -1061,21 +1614,64 @@ function infinite_loop_guard() {
|
|
|
1061
1614
|
export let eager_block_effects = null;
|
|
1062
1615
|
|
|
1063
1616
|
/**
|
|
1617
|
+
* While block effects re-run eagerly (see `flush_queued_effects`), the writes of the effects that ran before the one
|
|
1618
|
+
* whose writes caused the re-runs, which apply in their view (possibly none). `null` otherwise. The branches that
|
|
1619
|
+
* these runs select/unskip are only appended (and those they skip detached) when the batch with the writes commits
|
|
1620
|
+
* (see `should_defer_append`), and the teardowns that this causes see these writes, too (see `#run_callbacks`)
|
|
1621
|
+
* @type {Map<Value, BatchValue> | null}
|
|
1622
|
+
*/
|
|
1623
|
+
export let eager_block_writes = null;
|
|
1624
|
+
|
|
1625
|
+
/**
|
|
1626
|
+
* An effect (as opposed to a render effect, which keeps the offscreen content up to date) inside a boundary
|
|
1627
|
+
* that is still pending doesn't run when the batch commits. The boundary keeps it until it is revealed,
|
|
1628
|
+
* and hands it to the batch that reveals it (see `Boundary#reveal`)
|
|
1629
|
+
* TODO I feel like there's probably a less roundabout way to achieve this
|
|
1630
|
+
* @param {Effect} effect
|
|
1631
|
+
* @returns {boolean} whether the effect was deferred
|
|
1632
|
+
*/
|
|
1633
|
+
function defer_if_pending(effect) {
|
|
1634
|
+
// A boundary is only pending while its content is new, so an effect that
|
|
1635
|
+
// has run before can't be inside one, and we can skip the lookup
|
|
1636
|
+
if ((effect.f & (EFFECT | REACTION_RAN)) !== EFFECT) return false;
|
|
1637
|
+
|
|
1638
|
+
for (var boundary = effect.b; boundary !== null; boundary = boundary.parent) {
|
|
1639
|
+
if (boundary.is_pending) {
|
|
1640
|
+
boundary.defer_effect(effect);
|
|
1641
|
+
return true;
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
|
|
1645
|
+
return false;
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
/**
|
|
1649
|
+
* `earlier_writes` includes writes from the pre-effect phase. Eager block teardowns
|
|
1650
|
+
* revert the running effect's writes, but not these earlier writes.
|
|
1064
1651
|
* @param {Array<Effect>} effects
|
|
1065
|
-
* @
|
|
1652
|
+
* @param {Map<Value, BatchValue> | null} [earlier_writes]
|
|
1653
|
+
* @returns {Map<Value, BatchValue> | null}
|
|
1066
1654
|
*/
|
|
1067
|
-
function flush_queued_effects(effects) {
|
|
1655
|
+
function flush_queued_effects(effects, earlier_writes = null) {
|
|
1068
1656
|
var length = effects.length;
|
|
1069
|
-
if (length === 0) return;
|
|
1657
|
+
if (length === 0) return earlier_writes;
|
|
1070
1658
|
|
|
1071
1659
|
var i = 0;
|
|
1072
1660
|
|
|
1073
1661
|
while (i < length) {
|
|
1074
1662
|
var effect = effects[i++];
|
|
1075
1663
|
|
|
1076
|
-
if (
|
|
1664
|
+
if (
|
|
1665
|
+
(effect.f & (DESTROYED | INERT)) === 0 &&
|
|
1666
|
+
// don't run if already marked for destruction in the next batch
|
|
1667
|
+
// (this can happen if an effect writes a value which e.g. makes an if block falsy)
|
|
1668
|
+
!current_batch?.is_skipped(effect) &&
|
|
1669
|
+
!defer_if_pending(effect) &&
|
|
1670
|
+
is_dirty(effect)
|
|
1671
|
+
) {
|
|
1077
1672
|
eager_block_effects = new Set();
|
|
1078
1673
|
|
|
1674
|
+
var version = write_version;
|
|
1079
1675
|
update_effect(effect);
|
|
1080
1676
|
|
|
1081
1677
|
// Effects with no dependencies or teardown do not get added to the effect tree.
|
|
@@ -1084,7 +1680,7 @@ function flush_queued_effects(effects) {
|
|
|
1084
1680
|
// here (rather than in `update_effect`) allows us to skip the work for
|
|
1085
1681
|
// immediate effects.
|
|
1086
1682
|
if (
|
|
1087
|
-
effect.
|
|
1683
|
+
effect.edges === null &&
|
|
1088
1684
|
effect.first === null &&
|
|
1089
1685
|
effect.nodes === null &&
|
|
1090
1686
|
effect.teardown === null &&
|
|
@@ -1097,126 +1693,96 @@ function flush_queued_effects(effects) {
|
|
|
1097
1693
|
// If update_effect() has a flushSync() in it, we may have flushed another flush_queued_effects(),
|
|
1098
1694
|
// which already handled this logic and did set eager_block_effects to null.
|
|
1099
1695
|
if (eager_block_effects?.size > 0) {
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1696
|
+
// these run because of the writes of the effect, so their teardowns revert those
|
|
1697
|
+
var previous_teardown_context = teardown_context;
|
|
1698
|
+
|
|
1699
|
+
// if state was written by an effect before the one that caused the eager
|
|
1700
|
+
// block rerun, its writes apply in this view. Deriveds must be evaluated
|
|
1701
|
+
// afresh rather than reusing results cached before those writes.
|
|
1702
|
+
if (earlier_writes !== null) push_teardown_context(earlier_writes);
|
|
1703
|
+
|
|
1704
|
+
// (a copy, as `earlier_writes` grows while the flush continues)
|
|
1705
|
+
var previous_eager_block_writes = eager_block_writes;
|
|
1706
|
+
eager_block_writes = new Map(earlier_writes);
|
|
1707
|
+
|
|
1708
|
+
try {
|
|
1709
|
+
for (const e of eager_block_effects) {
|
|
1710
|
+
// Skip eager effects that have already been unmounted, or that are inside a
|
|
1711
|
+
// branch that an earlier eager run (e.g. of an ancestor block) skips
|
|
1712
|
+
if ((e.f & (DESTROYED | INERT)) !== 0 || current_batch?.is_skipped(e)) continue;
|
|
1713
|
+
|
|
1714
|
+
// Run effects in order from ancestor to descendant, else we could run into nullpointers
|
|
1715
|
+
/** @type {Effect[]} */
|
|
1716
|
+
const ordered_effects = [e];
|
|
1717
|
+
let ancestor = e.parent;
|
|
1718
|
+
while (ancestor !== null) {
|
|
1719
|
+
if (eager_block_effects.has(ancestor)) {
|
|
1720
|
+
eager_block_effects.delete(ancestor);
|
|
1721
|
+
ordered_effects.push(ancestor);
|
|
1722
|
+
}
|
|
1723
|
+
ancestor = ancestor.parent;
|
|
1114
1724
|
}
|
|
1115
|
-
ancestor = ancestor.parent;
|
|
1116
|
-
}
|
|
1117
1725
|
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1726
|
+
for (let j = ordered_effects.length - 1; j >= 0; j--) {
|
|
1727
|
+
const e = ordered_effects[j];
|
|
1728
|
+
// a block run may have skipped/destroyed the branch that a later one is in
|
|
1729
|
+
if ((e.f & (DESTROYED | INERT)) !== 0 || current_batch?.is_skipped(e)) continue;
|
|
1730
|
+
update_effect(e);
|
|
1731
|
+
}
|
|
1123
1732
|
}
|
|
1733
|
+
} finally {
|
|
1734
|
+
eager_block_writes = previous_eager_block_writes;
|
|
1735
|
+
eager_block_effects?.clear();
|
|
1736
|
+
teardown_context = previous_teardown_context;
|
|
1124
1737
|
}
|
|
1125
|
-
|
|
1126
|
-
eager_block_effects.clear();
|
|
1127
1738
|
}
|
|
1128
|
-
}
|
|
1129
|
-
}
|
|
1130
|
-
|
|
1131
|
-
eager_block_effects = null;
|
|
1132
|
-
}
|
|
1133
1739
|
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
* @param {Map<Reaction, boolean>} checked
|
|
1142
|
-
*/
|
|
1143
|
-
function mark_effects(value, sources, marked, checked) {
|
|
1144
|
-
if (marked.has(value)) return;
|
|
1145
|
-
marked.add(value);
|
|
1146
|
-
|
|
1147
|
-
if (value.reactions !== null) {
|
|
1148
|
-
for (const reaction of value.reactions) {
|
|
1149
|
-
const flags = reaction.f;
|
|
1150
|
-
|
|
1151
|
-
if ((flags & DERIVED) !== 0) {
|
|
1152
|
-
mark_effects(/** @type {Derived} */ (reaction), sources, marked, checked);
|
|
1153
|
-
} else if (
|
|
1154
|
-
(flags & (ASYNC | BLOCK_EFFECT)) !== 0 &&
|
|
1155
|
-
(flags & DIRTY) === 0 &&
|
|
1156
|
-
depends_on(reaction, sources, checked)
|
|
1157
|
-
) {
|
|
1158
|
-
set_signal_status(reaction, DIRTY);
|
|
1159
|
-
schedule_effect(/** @type {Effect} */ (reaction));
|
|
1740
|
+
// if the effect — or the block effects — wrote anything
|
|
1741
|
+
if (write_version !== version && current_batch !== null) {
|
|
1742
|
+
for (const [value, write] of current_batch.current) {
|
|
1743
|
+
if (write.wv > version) {
|
|
1744
|
+
(earlier_writes ??= new Map()).set(value, write);
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1160
1747
|
}
|
|
1161
1748
|
}
|
|
1162
1749
|
}
|
|
1163
|
-
}
|
|
1164
|
-
|
|
1165
|
-
/**
|
|
1166
|
-
* When committing a fork, we need to trigger eager effects so that
|
|
1167
|
-
* any `$state.eager(...)` expressions update immediately. This
|
|
1168
|
-
* function allows us to discover them
|
|
1169
|
-
* @param {Value} value
|
|
1170
|
-
* @param {Set<Effect>} effects
|
|
1171
|
-
*/
|
|
1172
|
-
function mark_eager_effects(value, effects) {
|
|
1173
|
-
if (value.reactions === null) return;
|
|
1174
|
-
|
|
1175
|
-
for (const reaction of value.reactions) {
|
|
1176
|
-
const flags = reaction.f;
|
|
1177
1750
|
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
} else if ((flags & EAGER_EFFECT) !== 0) {
|
|
1181
|
-
set_signal_status(reaction, DIRTY);
|
|
1182
|
-
effects.add(/** @type {Effect} */ (reaction));
|
|
1183
|
-
}
|
|
1184
|
-
}
|
|
1751
|
+
eager_block_effects = null;
|
|
1752
|
+
return earlier_writes;
|
|
1185
1753
|
}
|
|
1186
1754
|
|
|
1187
1755
|
/**
|
|
1188
|
-
*
|
|
1189
|
-
*
|
|
1190
|
-
* @param {
|
|
1756
|
+
* The next batch after `batch` that wrote `source`, if any. Its write comes after one of `batch` in order, so
|
|
1757
|
+
* that one isn't the real-world value. Forks are skipped: their writes only exist in their own view of the world
|
|
1758
|
+
* @param {Batch} batch
|
|
1759
|
+
* @param {Value} source
|
|
1191
1760
|
*/
|
|
1192
|
-
function
|
|
1193
|
-
|
|
1194
|
-
if (depends !== undefined) return depends;
|
|
1195
|
-
|
|
1196
|
-
if (reaction.deps !== null) {
|
|
1197
|
-
for (const dep of reaction.deps) {
|
|
1198
|
-
if (includes.call(sources, dep)) {
|
|
1199
|
-
return true;
|
|
1200
|
-
}
|
|
1761
|
+
function later_writer(batch, source) {
|
|
1762
|
+
var later = batch.next;
|
|
1201
1763
|
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
return true;
|
|
1205
|
-
}
|
|
1206
|
-
}
|
|
1764
|
+
while (later !== null && (later.is_fork || !later.current.has(source))) {
|
|
1765
|
+
later = later.next;
|
|
1207
1766
|
}
|
|
1208
1767
|
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
return false;
|
|
1768
|
+
return later;
|
|
1212
1769
|
}
|
|
1213
1770
|
|
|
1214
1771
|
/**
|
|
1215
|
-
*
|
|
1216
|
-
*
|
|
1772
|
+
* Make a write the real-world value of `source`. A write to a derived is an assignment, whose
|
|
1773
|
+
* value holds until one of the derived's dependencies is written after it (see `changed_since`)
|
|
1774
|
+
* @param {Value} source
|
|
1775
|
+
* @param {BatchValue} entry
|
|
1217
1776
|
*/
|
|
1218
|
-
|
|
1219
|
-
|
|
1777
|
+
function apply(source, entry) {
|
|
1778
|
+
source.v = entry.v;
|
|
1779
|
+
source.wv = entry.wv;
|
|
1780
|
+
source.f = entry.e ? source.f | ERROR_VALUE : source.f & ~ERROR_VALUE;
|
|
1781
|
+
|
|
1782
|
+
if ((source.f & DERIVED) !== 0) {
|
|
1783
|
+
source.f |= ASSIGNED;
|
|
1784
|
+
update_derived_status(/** @type {Derived} */ (source));
|
|
1785
|
+
}
|
|
1220
1786
|
}
|
|
1221
1787
|
|
|
1222
1788
|
/** @type {Source<number>[]} */
|
|
@@ -1264,30 +1830,26 @@ export function eager(fn) {
|
|
|
1264
1830
|
|
|
1265
1831
|
get(version);
|
|
1266
1832
|
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
batch_values = null;
|
|
1833
|
+
// evaluate in the real world — including recording the versions of the dependencies,
|
|
1834
|
+
// so that the eager effect is dirty-checked correctly later (see `flush_eager_effects`)
|
|
1835
|
+
in_real_world(() => {
|
|
1836
|
+
eager_effect(() => {
|
|
1837
|
+
if (initial) {
|
|
1838
|
+
// the first time this runs, we create an eager effect
|
|
1839
|
+
// that will run eagerly whenever the expression changes
|
|
1275
1840
|
value = fn();
|
|
1276
|
-
|
|
1277
|
-
batch_values = previous_batch_values;
|
|
1841
|
+
return;
|
|
1278
1842
|
}
|
|
1279
1843
|
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
if (eager_versions.length === 0) {
|
|
1287
|
-
queue_micro_task(eager_flush);
|
|
1288
|
-
}
|
|
1844
|
+
// the second time this effect runs, it's to schedule a
|
|
1845
|
+
// `version` update. since this will recreate the effect,
|
|
1846
|
+
// we don't need to evaluate the expression here
|
|
1847
|
+
if (eager_versions.length === 0) {
|
|
1848
|
+
queue_micro_task(eager_flush);
|
|
1849
|
+
}
|
|
1289
1850
|
|
|
1290
|
-
|
|
1851
|
+
eager_versions.push(version);
|
|
1852
|
+
});
|
|
1291
1853
|
});
|
|
1292
1854
|
|
|
1293
1855
|
initial = false;
|
|
@@ -1296,43 +1858,43 @@ export function eager(fn) {
|
|
|
1296
1858
|
}
|
|
1297
1859
|
|
|
1298
1860
|
/**
|
|
1299
|
-
*
|
|
1300
|
-
*
|
|
1301
|
-
* effects so they can be rescheduled if the branch survives.
|
|
1302
|
-
* @param {Effect} effect
|
|
1303
|
-
* @param {{ d: Effect[], m: Effect[] }} tracked
|
|
1861
|
+
* Run `fn` with the real world (i.e. the latest values) as the active view, regardless of which batch is active
|
|
1862
|
+
* @param {() => void} fn
|
|
1304
1863
|
*/
|
|
1305
|
-
function
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
}
|
|
1310
|
-
|
|
1311
|
-
if ((effect.f & DIRTY) !== 0) {
|
|
1312
|
-
tracked.d.push(effect);
|
|
1313
|
-
} else if ((effect.f & MAYBE_DIRTY) !== 0) {
|
|
1314
|
-
tracked.m.push(effect);
|
|
1315
|
-
}
|
|
1864
|
+
export function in_real_world(fn) {
|
|
1865
|
+
var previous_batch_values = batch_values;
|
|
1866
|
+
var previous_view_cache = view_cache;
|
|
1867
|
+
var previous_view_batch = view_batch;
|
|
1316
1868
|
|
|
1317
|
-
|
|
1869
|
+
// the real world doesn't belong to a batch, so what runs in it doesn't touch the active batch's records
|
|
1870
|
+
batch_values = view_cache = view_batch = null;
|
|
1318
1871
|
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1872
|
+
try {
|
|
1873
|
+
fn();
|
|
1874
|
+
} finally {
|
|
1875
|
+
batch_values = previous_batch_values;
|
|
1876
|
+
view_cache = previous_view_cache;
|
|
1877
|
+
view_batch = previous_view_batch;
|
|
1323
1878
|
}
|
|
1324
1879
|
}
|
|
1325
1880
|
|
|
1326
1881
|
/**
|
|
1327
|
-
* Mark
|
|
1882
|
+
* Mark all the effects inside a skipped branch CLEAN, so that they can be correctly
|
|
1883
|
+
* rescheduled later (if the branch survives, its effects are re-validated, see `unskip_effect`)
|
|
1328
1884
|
* @param {Effect} effect
|
|
1885
|
+
* @param {boolean} force reset even clean branches following an error
|
|
1329
1886
|
*/
|
|
1330
|
-
function
|
|
1887
|
+
function reset_branch(effect, force = false) {
|
|
1888
|
+
// clean branch = nothing dirty inside, no need to traverse further
|
|
1889
|
+
if (!force && (effect.f & BRANCH_EFFECT) !== 0 && (effect.f & CLEAN) !== 0) {
|
|
1890
|
+
return;
|
|
1891
|
+
}
|
|
1892
|
+
|
|
1331
1893
|
set_signal_status(effect, CLEAN);
|
|
1332
1894
|
|
|
1333
1895
|
var e = effect.first;
|
|
1334
1896
|
while (e !== null) {
|
|
1335
|
-
|
|
1897
|
+
reset_branch(e, force);
|
|
1336
1898
|
e = e.next;
|
|
1337
1899
|
}
|
|
1338
1900
|
}
|
|
@@ -1361,21 +1923,34 @@ export function fork(fn) {
|
|
|
1361
1923
|
e.experimental_async_required('fork');
|
|
1362
1924
|
}
|
|
1363
1925
|
|
|
1364
|
-
if (
|
|
1926
|
+
if (active_effect !== null || current_batch?.is_fork) {
|
|
1365
1927
|
e.fork_timing();
|
|
1366
1928
|
}
|
|
1367
1929
|
|
|
1368
|
-
flushSync()
|
|
1930
|
+
var batch = flushSync(() => {
|
|
1931
|
+
var batch = Batch.ensure();
|
|
1932
|
+
batch.is_fork = true;
|
|
1369
1933
|
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1934
|
+
try {
|
|
1935
|
+
in_real_world(() => {
|
|
1936
|
+
batch_values = new Map();
|
|
1937
|
+
view_cache = new Map();
|
|
1938
|
+
view_batch = batch;
|
|
1373
1939
|
|
|
1374
|
-
|
|
1375
|
-
|
|
1940
|
+
fn();
|
|
1941
|
+
});
|
|
1942
|
+
} catch (error) {
|
|
1943
|
+
batch.discard();
|
|
1944
|
+
batch.deactivate();
|
|
1945
|
+
throw error;
|
|
1946
|
+
}
|
|
1376
1947
|
|
|
1377
|
-
|
|
1378
|
-
|
|
1948
|
+
return batch;
|
|
1949
|
+
});
|
|
1950
|
+
|
|
1951
|
+
var settled = batch.settled();
|
|
1952
|
+
var committed = false;
|
|
1953
|
+
var discarded = false;
|
|
1379
1954
|
|
|
1380
1955
|
return {
|
|
1381
1956
|
commit: async () => {
|
|
@@ -1384,51 +1959,47 @@ export function fork(fn) {
|
|
|
1384
1959
|
return;
|
|
1385
1960
|
}
|
|
1386
1961
|
|
|
1387
|
-
if (
|
|
1962
|
+
if (discarded) {
|
|
1388
1963
|
e.fork_discarded();
|
|
1389
1964
|
}
|
|
1390
1965
|
|
|
1391
|
-
|
|
1966
|
+
if (!batch.linked) {
|
|
1967
|
+
// batch was made obsolete by later changes
|
|
1968
|
+
committed = true;
|
|
1969
|
+
return;
|
|
1970
|
+
}
|
|
1392
1971
|
|
|
1972
|
+
committed = true;
|
|
1393
1973
|
batch.is_fork = false;
|
|
1394
1974
|
|
|
1395
|
-
//
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1975
|
+
// Apply the changes to the real world. They keep the versions they had in the fork,
|
|
1976
|
+
// so that everything the fork computed remains valid (processing the batch
|
|
1977
|
+
// re-validates everything that depends on them, see `#process`)
|
|
1978
|
+
for (const [source, entry] of batch.current) {
|
|
1979
|
+
const later = later_writer(batch, source);
|
|
1400
1980
|
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
/** @type {Set<Effect>} */
|
|
1408
|
-
var eager_effects = new Set();
|
|
1409
|
-
|
|
1410
|
-
for (var source of batch.current.keys()) {
|
|
1411
|
-
mark_eager_effects(source, eager_effects);
|
|
1981
|
+
if (later !== null) {
|
|
1982
|
+
// in order, the fork's write comes before the later batch's, which replaces it
|
|
1983
|
+
later.previous.set(source, entry);
|
|
1984
|
+
} else {
|
|
1985
|
+
batch.mark(source, MAYBE_DIRTY);
|
|
1986
|
+
apply(source, entry);
|
|
1412
1987
|
}
|
|
1988
|
+
}
|
|
1413
1989
|
|
|
1414
|
-
|
|
1990
|
+
if (eager_effects.size > 0) {
|
|
1415
1991
|
flush_eager_effects();
|
|
1416
|
-
}
|
|
1992
|
+
}
|
|
1417
1993
|
|
|
1418
1994
|
batch.flush();
|
|
1419
1995
|
await settled;
|
|
1420
1996
|
},
|
|
1421
1997
|
discard: () => {
|
|
1422
|
-
// cause any MAYBE_DIRTY deriveds to update
|
|
1423
|
-
// if they depend on things that changed
|
|
1424
|
-
// inside the discarded fork
|
|
1425
|
-
for (var source of batch.current.keys()) {
|
|
1426
|
-
source.wv = increment_write_version();
|
|
1427
|
-
}
|
|
1428
|
-
|
|
1429
1998
|
if (!committed && batch.linked) {
|
|
1430
1999
|
batch.discard();
|
|
1431
2000
|
}
|
|
2001
|
+
|
|
2002
|
+
discarded = true;
|
|
1432
2003
|
}
|
|
1433
2004
|
};
|
|
1434
2005
|
}
|