Skip to content

Commit 4f24eae

Browse files
authored
simplify updates (#10912)
1 parent 1e0054e commit 4f24eae

File tree

10 files changed

+28
-106
lines changed

10 files changed

+28
-106
lines changed

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,6 @@ export function client_component(source, analysis, options) {
6262
error(null, 'INTERNAL', 'update.push should not be called outside create_block');
6363
return a;
6464
},
65-
get update_effects() {
66-
/** @type {any[]} */
67-
const a = [];
68-
a.push = () =>
69-
error(null, 'INTERNAL', 'update_effects.push should not be called outside create_block');
70-
return a;
71-
},
7265
get after_update() {
7366
/** @type {any[]} */
7467
const a = [];

packages/svelte/src/compiler/phases/3-transform/client/types.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ export interface ComponentClientTransformState extends ClientTransformState {
3131

3232
/** Stuff that happens before the render effect(s) */
3333
readonly init: Statement[];
34-
/** Stuff that happens inside separate render effects (due to call expressions) */
35-
readonly update_effects: Statement[];
3634
/** Stuff that happens inside the render effect */
3735
readonly update: {
38-
init?: Statement;
3936
/** If the update array only contains a single entry, this singular entry will be used, if present */
4037
singular?: Statement;
4138
/** Used if condition for singular prop is false (see comment above) */

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

Lines changed: 19 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function serialize_style_directives(style_directives, element_id, context, is_at
104104
);
105105

106106
if (!is_attributes_reactive && contains_call_expression) {
107-
state.update_effects.push(singular);
107+
state.init.push(singular);
108108
} else if (is_attributes_reactive || directive.metadata.dynamic || contains_call_expression) {
109109
state.update.push({ grouped, singular });
110110
} else {
@@ -153,7 +153,7 @@ function serialize_class_directives(class_directives, element_id, context, is_at
153153
const contains_call_expression = directive.expression.type === 'CallExpression';
154154

155155
if (!is_attributes_reactive && contains_call_expression) {
156-
state.update_effects.push(singular);
156+
state.init.push(singular);
157157
} else if (is_attributes_reactive || directive.metadata.dynamic || contains_call_expression) {
158158
state.update.push({ grouped, singular });
159159
} else {
@@ -326,11 +326,11 @@ function serialize_element_spread_attributes(
326326
// objects could contain reactive getters -> play it safe and always assume spread attributes are reactive
327327
if (needs_isolation) {
328328
if (needs_select_handling) {
329-
context.state.update_effects.push(
329+
context.state.init.push(
330330
b.stmt(b.call('$.render_effect', b.arrow([], b.block([inside_effect]))))
331331
);
332332
} else {
333-
context.state.update_effects.push(standalone);
333+
context.state.init.push(standalone);
334334
}
335335
} else {
336336
context.state.update.push({
@@ -408,7 +408,7 @@ function serialize_dynamic_element_attributes(attributes, context, element_id) {
408408
);
409409

410410
if (needs_isolation) {
411-
context.state.update_effects.push(isolated);
411+
context.state.init.push(isolated);
412412
return false;
413413
} else if (is_reactive) {
414414
const id = context.state.scope.generate('spread_attributes');
@@ -710,7 +710,7 @@ function serialize_update_assignment(state, id, init, value, assignment, contain
710710
);
711711

712712
if (contains_call_expression && assignment.singular) {
713-
state.update_effects.push(assignment.singular);
713+
state.init.push(assignment.singular);
714714
} else {
715715
if (assignment.skip_condition) {
716716
if (assignment.singular) {
@@ -719,21 +719,21 @@ function serialize_update_assignment(state, id, init, value, assignment, contain
719719
grouped: assignment.grouped
720720
});
721721
} else {
722+
state.init.push(b.var(id, init));
722723
state.update.push({
723-
init: b.var(id, init),
724724
grouped
725725
});
726726
}
727727
} else {
728728
if (assignment.singular) {
729+
state.init.push(b.var(id, init));
729730
state.update.push({
730-
init: b.var(id, init),
731731
singular: assignment.singular,
732732
grouped
733733
});
734734
} else {
735+
state.init.push(b.var(id, init));
735736
state.update.push({
736-
init: b.var(id, init),
737737
grouped
738738
});
739739
}
@@ -1132,7 +1132,6 @@ function create_block(parent, name, nodes, context) {
11321132
...context.state,
11331133
init: [],
11341134
update: [],
1135-
update_effects: [],
11361135
after_update: [],
11371136
template: [],
11381137
metadata: {
@@ -1241,32 +1240,8 @@ function create_block(parent, name, nodes, context) {
12411240
body.push(...state.init);
12421241
}
12431242

1244-
if (state.update.length > 0 || state.update_effects.length > 0) {
1245-
/** @type {import('estree').Statement | undefined} */
1246-
let update;
1247-
1248-
if (state.update_effects.length > 0) {
1249-
for (const render of state.update_effects) {
1250-
if (!update) {
1251-
update = render;
1252-
}
1253-
body.push(render);
1254-
}
1255-
}
1256-
if (state.update.length > 0) {
1257-
const render = serialize_render_stmt(state, body);
1258-
if (!update) {
1259-
update = render;
1260-
}
1261-
body.push(render);
1262-
}
1263-
1264-
/** @type {import('estree').Statement} */ (update).leadingComments = [
1265-
{
1266-
type: 'Block',
1267-
value: ` Update `
1268-
}
1269-
];
1243+
if (state.update.length > 0) {
1244+
body.push(serialize_render_stmt(state));
12701245
}
12711246

12721247
body.push(...state.after_update);
@@ -1284,15 +1259,6 @@ function create_block(parent, name, nodes, context) {
12841259
body.push(close);
12851260
}
12861261

1287-
if (body[0]) {
1288-
body[0].leadingComments = [
1289-
{
1290-
type: 'Block',
1291-
value: ` Init `
1292-
}
1293-
];
1294-
}
1295-
12961262
return body;
12971263
}
12981264

@@ -1316,30 +1282,13 @@ function get_template_function(namespace, state) {
13161282
/**
13171283
*
13181284
* @param {import('../types.js').ComponentClientTransformState} state
1319-
* @param {import('estree').Statement[]} body
13201285
*/
1321-
function serialize_render_stmt(state, body) {
1322-
let render;
1286+
function serialize_render_stmt(state) {
13231287
if (state.update.length === 1 && state.update[0].singular) {
1324-
render = state.update[0].singular;
1325-
} else {
1326-
render = b.stmt(
1327-
b.call(
1328-
'$.render_effect',
1329-
b.thunk(
1330-
b.block(
1331-
state.update.map((n) => {
1332-
if (n.init) {
1333-
body.push(n.init);
1334-
}
1335-
return n.grouped;
1336-
})
1337-
)
1338-
)
1339-
)
1340-
);
1288+
return state.update[0].singular;
13411289
}
1342-
return render;
1290+
1291+
return b.stmt(b.call('$.render_effect', b.thunk(b.block(state.update.map((n) => n.grouped)))));
13431292
}
13441293

13451294
/**
@@ -1573,7 +1522,7 @@ function process_children(nodes, expression, is_element, { visit, state }) {
15731522
);
15741523

15751524
if (node.metadata.contains_call_expression && !within_bound_contenteditable) {
1576-
state.update_effects.push(singular);
1525+
state.init.push(singular);
15771526
} else if (node.metadata.dynamic && !within_bound_contenteditable) {
15781527
state.update.push({
15791528
singular,
@@ -1615,7 +1564,7 @@ function process_children(nodes, expression, is_element, { visit, state }) {
16151564
const singular = b.stmt(b.call('$.text_effect', text_id, b.thunk(assignment)));
16161565

16171566
if (contains_call_expression && !within_bound_contenteditable) {
1618-
state.update_effects.push(singular);
1567+
state.init.push(singular);
16191568
} else if (
16201569
sequence.some((node) => node.type === 'ExpressionTag' && node.metadata.dynamic) &&
16211570
!within_bound_contenteditable
@@ -2214,7 +2163,6 @@ export const template_visitors = {
22142163
node: element_id,
22152164
init: [],
22162165
update: [],
2217-
update_effects: [],
22182166
after_update: []
22192167
}
22202168
};
@@ -2259,15 +2207,8 @@ export const template_visitors = {
22592207

22602208
/** @type {import('estree').Statement[]} */
22612209
const inner = inner_context.state.init;
2262-
if (inner_context.state.update.length > 0 || inner_context.state.update_effects.length > 0) {
2263-
if (inner_context.state.update_effects.length > 0) {
2264-
for (const render of inner_context.state.update_effects) {
2265-
inner.push(render);
2266-
}
2267-
}
2268-
if (inner_context.state.update.length > 0) {
2269-
inner.push(serialize_render_stmt(inner_context.state, inner));
2270-
}
2210+
if (inner_context.state.update.length > 0) {
2211+
inner.push(serialize_render_stmt(inner_context.state));
22712212
}
22722213
inner.push(...inner_context.state.after_update);
22732214
inner.push(

packages/svelte/tests/snapshot/samples/bind-this/_expected/client/index.svelte.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export default function Bind_this($$anchor, $$props) {
77
$.push($$props, false);
88
$.init();
99

10-
/* Init */
1110
var fragment = $.comment($$anchor);
1211
var node = $.first_child(fragment);
1312

packages/svelte/tests/snapshot/samples/dynamic-attributes-casing/_expected/client/main.svelte.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,24 @@ export default function Main($$anchor, $$props) {
1111
// needs to be a snapshot test because jsdom does auto-correct the attribute casing
1212
let x = 'test';
1313
let y = () => 'test';
14-
/* Init */
1514
var fragment = $.open_frag($$anchor, frag, false);
1615
var div = $.first_child(fragment);
16+
var div_foobar;
1717
var svg = $.sibling($.sibling(div, true));
18+
var svg_viewBox;
1819
var custom_element = $.sibling($.sibling(svg, true));
20+
var custom_element_fooBar;
1921
var div_1 = $.sibling($.sibling(custom_element, true));
20-
var svg_1 = $.sibling($.sibling(div_1, true));
21-
var custom_element_1 = $.sibling($.sibling(svg_1, true));
2222

23-
/* Update */
2423
$.attr_effect(div_1, "foobar", y);
24+
25+
var svg_1 = $.sibling($.sibling(div_1, true));
26+
2527
$.attr_effect(svg_1, "viewBox", y);
26-
$.set_custom_element_data_effect(custom_element_1, "fooBar", y);
2728

28-
var div_foobar;
29-
var svg_viewBox;
30-
var custom_element_fooBar;
29+
var custom_element_1 = $.sibling($.sibling(svg_1, true));
30+
31+
$.set_custom_element_data_effect(custom_element_1, "fooBar", y);
3132

3233
$.render_effect(() => {
3334
if (div_foobar !== (div_foobar = x)) {

packages/svelte/tests/snapshot/samples/each-string-template/_expected/client/index.svelte.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export default function Each_string_template($$anchor, $$props) {
77
$.push($$props, false);
88
$.init();
99

10-
/* Init */
1110
var fragment = $.comment($$anchor);
1211
var node = $.first_child(fragment);
1312

@@ -16,10 +15,8 @@ export default function Each_string_template($$anchor, $$props) {
1615
() => ['foo', 'bar', 'baz'],
1716
1,
1817
($$anchor, thing, $$index) => {
19-
/* Init */
2018
var text = $.space_frag($$anchor);
2119

22-
/* Update */
2320
$.text_effect(text, () => `${$.stringify($.unwrap(thing))}, `);
2421
return $.close($$anchor, text);
2522
},

packages/svelte/tests/snapshot/samples/function-prop-no-getter/_expected/client/index.svelte.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export default function Function_prop_no_getter($$anchor, $$props) {
1313
}
1414

1515
const plusOne = (num) => num + 1;
16-
/* Init */
1716
var fragment = $.comment($$anchor);
1817
var node = $.first_child(fragment);
1918

@@ -22,10 +21,8 @@ export default function Function_prop_no_getter($$anchor, $$props) {
2221
onmouseup,
2322
onmouseenter: () => $.set(count, $.proxy(plusOne($.get(count)))),
2423
children: ($$anchor, $$slotProps) => {
25-
/* Init */
2624
var text = $.space_frag($$anchor);
2725

28-
/* Update */
2926
$.text_effect(text, () => `clicks: ${$.stringify($.get(count))}`);
3027
return $.close($$anchor, text);
3128
}

packages/svelte/tests/snapshot/samples/hello-world/_expected/client/index.svelte.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export default function Hello_world($$anchor, $$props) {
99
$.push($$props, false);
1010
$.init();
1111

12-
/* Init */
1312
var h1 = $.open($$anchor, frag);
1413

1514
$.close($$anchor, h1);

packages/svelte/tests/snapshot/samples/state-proxy-literal/_expected/client/index.svelte.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export default function State_proxy_literal($$anchor, $$props) {
1717

1818
let str = $.source('');
1919
let tpl = $.source(``);
20-
/* Init */
2120
var fragment = $.open_frag($$anchor, frag);
2221
var input = $.first_child(fragment);
2322

packages/svelte/tests/snapshot/samples/svelte-element/_expected/client/index.svelte.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export default function Svelte_element($$anchor, $$props) {
77
$.push($$props, true);
88

99
let tag = $.prop($$props, "tag", 3, 'hr');
10-
/* Init */
1110
var fragment = $.comment($$anchor);
1211
var node = $.first_child(fragment);
1312

0 commit comments

Comments
 (0)