Skip to content

Commit 3eee9d5

Browse files
committed
Revert "dry out"
This reverts commit 2585516.
1 parent 2585516 commit 3eee9d5

File tree

5 files changed

+51
-53
lines changed

5 files changed

+51
-53
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/** @import { ComponentContext } from '../types' */
44
import { unwrap_optional } from '../../../../utils/ast.js';
55
import * as b from '#compiler/builders';
6+
import { create_derived } from '../utils.js';
67
import { build_expression, Memoizer } from './shared/utils.js';
78

89
/**
@@ -35,7 +36,9 @@ export function RenderTag(node, context) {
3536
memoizer.apply();
3637

3738
/** @type {Statement[]} */
38-
const statements = memoizer.deriveds();
39+
const statements = memoizer.sync.map((memo) =>
40+
b.var(memo.id, create_derived(context.state, b.thunk(memo.expression)))
41+
);
3942

4043
let snippet_function = build_expression(
4144
context,
@@ -64,16 +67,17 @@ export function RenderTag(node, context) {
6467
);
6568
}
6669

67-
const async_values = memoizer.async_values();
68-
69-
if (async_values) {
70+
if (memoizer.async.length > 0) {
7071
context.state.init.push(
7172
b.stmt(
7273
b.call(
7374
'$.async',
7475
context.state.node,
75-
async_values,
76-
b.arrow([context.state.node, ...memoizer.async_ids()], b.block(statements))
76+
b.array(memoizer.async.map((memo) => b.thunk(memo.expression, true))),
77+
b.arrow(
78+
[context.state.node, ...memoizer.async.map((memo) => memo.id)],
79+
b.block(statements)
80+
)
7781
)
7882
)
7983
);

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/** @import { AST } from '#compiler' */
33
/** @import { ComponentContext } from '../types' */
44
import * as b from '#compiler/builders';
5+
import { create_derived } from '../utils.js';
56
import { build_attribute_value } from './shared/element.js';
67
import { Memoizer } from './shared/utils.js';
78

@@ -59,7 +60,9 @@ export function SlotElement(node, context) {
5960
context.state.init.push(...lets);
6061

6162
/** @type {Statement[]} */
62-
const statements = memoizer.deriveds();
63+
const statements = memoizer.sync.map((memo) =>
64+
b.var(memo.id, create_derived(context.state, b.thunk(memo.expression)))
65+
);
6366

6467
const props_expression =
6568
spreads.length === 0 ? b.object(props) : b.call('$.spread_props', b.object(props), ...spreads);
@@ -73,16 +76,17 @@ export function SlotElement(node, context) {
7376
b.stmt(b.call('$.slot', context.state.node, b.id('$$props'), name, props_expression, fallback))
7477
);
7578

76-
const async_values = memoizer.async_values();
77-
78-
if (async_values) {
79+
if (memoizer.async.length > 0) {
7980
context.state.init.push(
8081
b.stmt(
8182
b.call(
8283
'$.async',
8384
context.state.node,
84-
async_values,
85-
b.arrow([context.state.node, ...memoizer.async_ids()], b.block(statements))
85+
b.array(memoizer.async.map((memo) => b.thunk(memo.expression, true))),
86+
b.arrow(
87+
[context.state.node, ...memoizer.async.map((memo) => memo.id)],
88+
b.block(statements)
89+
)
8690
)
8791
)
8892
);

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { build_bind_this, Memoizer, validate_binding } from '../shared/utils.js'
88
import { build_attribute_value } from '../shared/element.js';
99
import { build_event_handler } from './events.js';
1010
import { determine_slot } from '../../../../../utils/slot.js';
11+
import { create_derived } from '../../utils.js';
1112

1213
/**
1314
* @param {AST.Component | AST.SvelteComponent | AST.SvelteSelf} node
@@ -446,7 +447,12 @@ export function build_component(node, component_name, context) {
446447
};
447448
}
448449

449-
const statements = [...snippet_declarations, ...memoizer.deriveds()];
450+
const statements = [
451+
...snippet_declarations,
452+
...memoizer.sync.map((memo) =>
453+
b.let(memo.id, create_derived(context.state, b.thunk(memo.expression)))
454+
)
455+
];
450456

451457
if (is_component_dynamic) {
452458
const prev = fn;
@@ -495,15 +501,13 @@ export function build_component(node, component_name, context) {
495501

496502
memoizer.apply();
497503

498-
const async_values = memoizer.async_values();
499-
500-
if (async_values) {
504+
if (memoizer.async.length > 0) {
501505
return b.stmt(
502506
b.call(
503507
'$.async',
504508
anchor,
505-
async_values,
506-
b.arrow([b.id('$$anchor'), ...memoizer.async_ids()], b.block(statements))
509+
b.array(memoizer.async.map(({ expression }) => b.thunk(expression, true))),
510+
b.arrow([b.id('$$anchor'), ...memoizer.async.map(({ id }) => id)], b.block(statements))
507511
)
508512
);
509513
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,21 @@ export function build_attribute_effect(
7878
);
7979
}
8080

81-
memoizer.apply();
81+
const all = memoizer.apply();
8282

8383
context.state.init.push(
8484
b.stmt(
8585
b.call(
8686
'$.attribute_effect',
8787
element_id,
88-
b.arrow(memoizer.all_ids(), b.object(values)),
89-
memoizer.sync_values(),
90-
memoizer.async_values(),
88+
b.arrow(
89+
all.map(({ id }) => id),
90+
b.object(values)
91+
),
92+
memoizer.sync.length > 0 &&
93+
b.array(memoizer.sync.map(({ expression }) => b.thunk(expression))),
94+
memoizer.async.length > 0 &&
95+
b.array(memoizer.async.map(({ expression }) => b.thunk(expression, true))),
9196
element.metadata.scoped &&
9297
context.state.analysis.css.hash !== '' &&
9398
b.literal(context.state.analysis.css.hash),

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

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import { build_getter } from '../../utils.js';
1212

1313
export class Memoizer {
1414
/** @type {Array<{ id: Identifier, expression: Expression }>} */
15-
#sync = [];
15+
sync = [];
1616

1717
/** @type {Array<{ id: Identifier, expression: Expression }>} */
18-
#async = [];
18+
async = [];
1919

2020
/**
2121
* @param {Expression} expression
@@ -24,39 +24,19 @@ export class Memoizer {
2424
add(expression, has_await) {
2525
const id = b.id(`#`); // filled in later
2626

27-
(has_await ? this.#async : this.#sync).push({ id, expression });
27+
(has_await ? this.async : this.sync).push({ id, expression });
2828

2929
return id;
3030
}
3131

3232
apply() {
33-
[...this.#async, ...this.#sync].forEach((memo, i) => {
33+
const all = [...this.async, ...this.sync];
34+
35+
all.forEach((memo, i) => {
3436
memo.id.name = `$${i}`;
3537
});
36-
}
37-
38-
all_ids() {
39-
return [...this.#async, ...this.#sync].map((memo) => memo.id);
40-
}
41-
42-
async_ids() {
43-
return this.#async.map((memo) => memo.id);
44-
}
45-
46-
async_values() {
47-
if (this.#async.length === 0) return;
48-
return b.array(this.#async.map((memo) => b.thunk(memo.expression, true)));
49-
}
50-
51-
deriveds(runes = true) {
52-
return this.#sync.map((memo) =>
53-
b.let(memo.id, b.call(runes ? '$.derived' : '$.derived_safe_equal', b.thunk(memo.expression)))
54-
);
55-
}
5638

57-
sync_values() {
58-
if (this.#sync.length === 0) return;
59-
return b.array(this.#sync.map((memo) => b.thunk(memo.expression)));
39+
return all;
6040
}
6141
}
6242

@@ -163,19 +143,20 @@ export function build_template_chunk(
163143
export function build_render_statement(state) {
164144
const { memoizer } = state;
165145

166-
state.memoizer.apply();
146+
const all = state.memoizer.apply();
167147

168148
return b.stmt(
169149
b.call(
170150
'$.template_effect',
171151
b.arrow(
172-
memoizer.all_ids(),
152+
all.map(({ id }) => id),
173153
state.update.length === 1 && state.update[0].type === 'ExpressionStatement'
174154
? state.update[0].expression
175155
: b.block(state.update)
176156
),
177-
memoizer.sync_values(),
178-
memoizer.async_values()
157+
all.length > 0 && b.array(memoizer.sync.map(({ expression }) => b.thunk(expression))),
158+
memoizer.async.length > 0 &&
159+
b.array(memoizer.async.map(({ expression }) => b.thunk(expression, true)))
179160
)
180161
);
181162
}

0 commit comments

Comments
 (0)