Skip to content

Commit 1ae3e09

Browse files
committed
fix
1 parent 7c09c16 commit 1ae3e09

File tree

5 files changed

+83
-70
lines changed

5 files changed

+83
-70
lines changed

packages/svelte/src/compiler/phases/2-analyze/visitors/EachBlock.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @import { AST } from '#compiler' */
1+
/** @import { AST, Binding } from '#compiler' */
22
/** @import { Context } from '../types' */
33
/** @import { Scope } from '../../scope' */
44
import * as e from '../../../errors.js';
@@ -38,5 +38,38 @@ export function EachBlock(node, context) {
3838
if (node.key) context.visit(node.key);
3939
if (node.fallback) context.visit(node.fallback);
4040

41+
// collect transitive dependencies...
42+
for (const binding of node.metadata.expression.dependencies) {
43+
collect_transitive_dependencies(binding, node.metadata.transitive_deps);
44+
}
45+
46+
// ...and ensure they are marked as state, so they can be turned
47+
// into mutable sources and invalidated
48+
for (const binding of node.metadata.transitive_deps) {
49+
if (
50+
binding.kind === 'normal' &&
51+
(binding.declaration_kind === 'const' ||
52+
binding.declaration_kind === 'let' ||
53+
binding.declaration_kind === 'var')
54+
) {
55+
binding.kind = 'state';
56+
}
57+
}
58+
4159
mark_subtree_dynamic(context.path);
4260
}
61+
62+
/**
63+
* @param {Binding} binding
64+
* @param {Set<Binding>} bindings
65+
* @returns {void}
66+
*/
67+
function collect_transitive_dependencies(binding, bindings) {
68+
bindings.add(binding);
69+
70+
if (binding.kind === 'legacy_reactive') {
71+
for (const dep of binding.legacy_dependencies) {
72+
collect_transitive_dependencies(dep, bindings);
73+
}
74+
}
75+
}

packages/svelte/src/compiler/phases/3-transform/client/visitors/EachBlock.js

Lines changed: 40 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @import { BlockStatement, Expression, Identifier, Pattern, Statement } from 'estree' */
1+
/** @import { BlockStatement, Expression, Identifier, Pattern, SequenceExpression, Statement } from 'estree' */
22
/** @import { AST, Binding } from '#compiler' */
33
/** @import { ComponentContext } from '../types' */
44
/** @import { Scope } from '../../../scope' */
@@ -106,16 +106,6 @@ export function EachBlock(node, context) {
106106
}
107107
}
108108

109-
// Legacy mode: find the parent each blocks which contain the arrays to invalidate
110-
const indirect_dependencies = collect_parent_each_blocks(context).flatMap((block) => {
111-
const array = /** @type {Expression} */ (context.visit(block.expression));
112-
const transitive_dependencies = build_transitive_dependencies(
113-
block.metadata.expression.dependencies,
114-
context
115-
);
116-
return [array, ...transitive_dependencies];
117-
});
118-
119109
/** @type {Identifier | null} */
120110
let collection_id = null;
121111

@@ -129,18 +119,6 @@ export function EachBlock(node, context) {
129119
}
130120
}
131121

132-
if (collection_id) {
133-
indirect_dependencies.push(b.call(collection_id));
134-
} else {
135-
indirect_dependencies.push(collection);
136-
137-
const transitive_dependencies = build_transitive_dependencies(
138-
each_node_meta.expression.dependencies,
139-
context
140-
);
141-
indirect_dependencies.push(...transitive_dependencies);
142-
}
143-
144122
const child_state = {
145123
...context.state,
146124
transform: { ...context.state.transform },
@@ -181,19 +159,51 @@ export function EachBlock(node, context) {
181159
/** @type {Statement[]} */
182160
const declarations = [];
183161

184-
const invalidate = b.call(
185-
'$.invalidate_inner_signals',
186-
b.thunk(b.sequence(indirect_dependencies))
187-
);
188-
189162
const invalidate_store = store_to_invalidate
190163
? b.call('$.invalidate_store', b.id('$$stores'), b.literal(store_to_invalidate))
191164
: undefined;
192165

193166
/** @type {Expression[]} */
194167
const sequence = [];
195-
if (!context.state.analysis.runes) sequence.push(invalidate);
196-
if (invalidate_store) sequence.push(invalidate_store);
168+
169+
if (!context.state.analysis.runes) {
170+
/** @type {Set<Identifier>} */
171+
const transitive_deps = new Set();
172+
173+
if (collection_id) {
174+
transitive_deps.add(collection_id);
175+
child_state.transform[collection_id.name] = { read: b.call };
176+
} else {
177+
for (const binding of each_node_meta.transitive_deps) {
178+
transitive_deps.add(binding.node);
179+
}
180+
}
181+
182+
for (const block of collect_parent_each_blocks(context)) {
183+
for (const binding of block.metadata.transitive_deps) {
184+
transitive_deps.add(binding.node);
185+
}
186+
}
187+
188+
if (transitive_deps.size > 0) {
189+
const invalidate = b.call(
190+
'$.invalidate_inner_signals',
191+
b.thunk(
192+
b.sequence(
193+
[...transitive_deps].map(
194+
(node) => /** @type {Expression} */ (context.visit({ ...node }, child_state))
195+
)
196+
)
197+
)
198+
);
199+
200+
sequence.push(invalidate);
201+
}
202+
}
203+
204+
if (invalidate_store) {
205+
sequence.push(invalidate_store);
206+
}
197207

198208
if (node.context?.type === 'Identifier') {
199209
const binding = /** @type {Binding} */ (context.state.scope.get(node.context.name));
@@ -329,41 +339,3 @@ export function EachBlock(node, context) {
329339
function collect_parent_each_blocks(context) {
330340
return /** @type {AST.EachBlock[]} */ (context.path.filter((node) => node.type === 'EachBlock'));
331341
}
332-
333-
/**
334-
* @param {Set<Binding>} references
335-
* @param {ComponentContext} context
336-
*/
337-
function build_transitive_dependencies(references, context) {
338-
/** @type {Set<Binding>} */
339-
const dependencies = new Set();
340-
341-
for (const ref of references) {
342-
const deps = collect_transitive_dependencies(ref);
343-
for (const dep of deps) {
344-
dependencies.add(dep);
345-
}
346-
}
347-
348-
return [...dependencies].map((dep) => build_getter({ ...dep.node }, context.state));
349-
}
350-
351-
/**
352-
* @param {Binding} binding
353-
* @param {Set<Binding>} seen
354-
* @returns {Binding[]}
355-
*/
356-
function collect_transitive_dependencies(binding, seen = new Set()) {
357-
if (binding.kind !== 'legacy_reactive') return [];
358-
359-
for (const dep of binding.legacy_dependencies) {
360-
if (!seen.has(dep)) {
361-
seen.add(dep);
362-
for (const transitive_dep of collect_transitive_dependencies(dep, seen)) {
363-
seen.add(transitive_dep);
364-
}
365-
}
366-
}
367-
368-
return [...seen];
369-
}

packages/svelte/src/compiler/phases/scope.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,9 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
11691169
contains_group_binding: false,
11701170
index: scope.root.unique('$$index'),
11711171
declarations: scope.declarations,
1172-
is_controlled: false
1172+
is_controlled: false,
1173+
// filled in during analysis
1174+
transitive_deps: new Set()
11731175
};
11741176
},
11751177

packages/svelte/src/compiler/types/template.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,11 @@ export namespace AST {
432432
* This saves us from creating an extra comment and insertion being faster.
433433
*/
434434
is_controlled: boolean;
435+
/**
436+
* Bindings this each block transitively depends on. In legacy mode, we
437+
* invalidate these bindings when mutations happen to each block items
438+
*/
439+
transitive_deps: Set<Binding>;
435440
};
436441
}
437442

packages/svelte/src/internal/client/reactivity/props.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ export function prop(props, key, flags, fallback) {
335335
}
336336

337337
// easy mode — prop is never written to
338+
// TODO i think the `&& runes` might now be superfluous
338339
if ((flags & PROPS_IS_UPDATED) === 0 && runes) {
339340
return getter;
340341
}

0 commit comments

Comments
 (0)