Skip to content

Commit 2585516

Browse files
committed
dry out
1 parent 2b44100 commit 2585516

File tree

5 files changed

+53
-51
lines changed

5 files changed

+53
-51
lines changed

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
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';
76
import { build_expression, Memoizer } from './shared/utils.js';
87

98
/**
@@ -36,9 +35,7 @@ export function RenderTag(node, context) {
3635
memoizer.apply();
3736

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

4340
let snippet_function = build_expression(
4441
context,
@@ -67,17 +64,16 @@ export function RenderTag(node, context) {
6764
);
6865
}
6966

70-
if (memoizer.async.length > 0) {
67+
const async_values = memoizer.async_values();
68+
69+
if (async_values) {
7170
context.state.init.push(
7271
b.stmt(
7372
b.call(
7473
'$.async',
7574
context.state.node,
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-
)
75+
async_values,
76+
b.arrow([context.state.node, ...memoizer.async_ids()], b.block(statements))
8177
)
8278
)
8379
);

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

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

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

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

6764
const props_expression =
6865
spreads.length === 0 ? b.object(props) : b.call('$.spread_props', b.object(props), ...spreads);
@@ -76,17 +73,16 @@ export function SlotElement(node, context) {
7673
b.stmt(b.call('$.slot', context.state.node, b.id('$$props'), name, props_expression, fallback))
7774
);
7875

79-
if (memoizer.async.length > 0) {
76+
const async_values = memoizer.async_values();
77+
78+
if (async_values) {
8079
context.state.init.push(
8180
b.stmt(
8281
b.call(
8382
'$.async',
8483
context.state.node,
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-
)
84+
async_values,
85+
b.arrow([context.state.node, ...memoizer.async_ids()], b.block(statements))
9086
)
9187
)
9288
);

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ 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';
1211

1312
/**
1413
* @param {AST.Component | AST.SvelteComponent | AST.SvelteSelf} node
@@ -447,12 +446,7 @@ export function build_component(node, component_name, context) {
447446
};
448447
}
449448

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-
];
449+
const statements = [...snippet_declarations, ...memoizer.deriveds()];
456450

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

502496
memoizer.apply();
503497

504-
if (memoizer.async.length > 0) {
498+
const async_values = memoizer.async_values();
499+
500+
if (async_values) {
505501
return b.stmt(
506502
b.call(
507503
'$.async',
508504
anchor,
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))
505+
async_values,
506+
b.arrow([b.id('$$anchor'), ...memoizer.async_ids()], b.block(statements))
511507
)
512508
);
513509
}

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

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

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

8383
context.state.init.push(
8484
b.stmt(
8585
b.call(
8686
'$.attribute_effect',
8787
element_id,
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))),
88+
b.arrow(memoizer.all_ids(), b.object(values)),
89+
memoizer.sync_values(),
90+
memoizer.async_values(),
9691
element.metadata.scoped &&
9792
context.state.analysis.css.hash !== '' &&
9893
b.literal(context.state.analysis.css.hash),

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

Lines changed: 31 additions & 12 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,19 +24,39 @@ 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-
const all = [...this.async, ...this.sync];
34-
35-
all.forEach((memo, i) => {
33+
[...this.#async, ...this.#sync].forEach((memo, i) => {
3634
memo.id.name = `$${i}`;
3735
});
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+
}
3856

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

@@ -143,20 +163,19 @@ export function build_template_chunk(
143163
export function build_render_statement(state) {
144164
const { memoizer } = state;
145165

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

148168
return b.stmt(
149169
b.call(
150170
'$.template_effect',
151171
b.arrow(
152-
all.map(({ id }) => id),
172+
memoizer.all_ids(),
153173
state.update.length === 1 && state.update[0].type === 'ExpressionStatement'
154174
? state.update[0].expression
155175
: b.block(state.update)
156176
),
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)))
177+
memoizer.sync_values(),
178+
memoizer.async_values()
160179
)
161180
);
162181
}

0 commit comments

Comments
 (0)