pull/15844/head
Rich Harris 3 months ago
parent 53137a1336
commit b3bcdaf333

@ -78,17 +78,14 @@ export function build_attribute_effect(
);
}
const all = memoizer.apply();
const ids = memoizer.apply();
context.state.init.push(
b.stmt(
b.call(
'$.attribute_effect',
element_id,
b.arrow(
all.map(({ id }) => id),
b.object(values)
),
b.arrow(ids, b.object(values)),
memoizer.sync_values(),
memoizer.async_values(),
element.metadata.scoped &&

@ -12,51 +12,48 @@ import { build_getter } from '../../utils.js';
export class Memoizer {
/** @type {Array<{ id: Identifier, expression: Expression }>} */
sync = [];
#sync = [];
/** @type {Array<{ id: Identifier, expression: Expression }>} */
async = [];
#async = [];
/**
* @param {Expression} expression
* @param {boolean} has_await
*/
add(expression, has_await) {
const id = b.id(`#`); // filled in later
const id = b.id('#'); // filled in later
(has_await ? this.async : this.sync).push({ id, expression });
(has_await ? this.#async : this.#sync).push({ id, expression });
return id;
}
apply() {
const all = [...this.async, ...this.sync];
all.forEach((memo, i) => {
return [...this.#async, ...this.#sync].map((memo, i) => {
memo.id.name = `$${i}`;
return memo.id;
});
return all;
}
deriveds(runes = true) {
return this.sync.map((memo) =>
return this.#sync.map((memo) =>
b.let(memo.id, b.call(runes ? '$.derived' : '$.derived_safe_equal', b.thunk(memo.expression)))
);
}
async_ids() {
return this.async.map((memo) => memo.id);
return this.#async.map((memo) => memo.id);
}
async_values() {
if (this.async.length === 0) return;
return b.array(this.async.map((memo) => b.thunk(memo.expression, true)));
if (this.#async.length === 0) return;
return b.array(this.#async.map((memo) => b.thunk(memo.expression, true)));
}
sync_values() {
if (this.sync.length === 0) return;
return b.array(this.sync.map((memo) => b.thunk(memo.expression)));
if (this.#sync.length === 0) return;
return b.array(this.#sync.map((memo) => b.thunk(memo.expression)));
}
}
@ -163,13 +160,13 @@ export function build_template_chunk(
export function build_render_statement(state) {
const { memoizer } = state;
const all = state.memoizer.apply();
const ids = state.memoizer.apply();
return b.stmt(
b.call(
'$.template_effect',
b.arrow(
all.map(({ id }) => id),
ids,
state.update.length === 1 && state.update[0].type === 'ExpressionStatement'
? state.update[0].expression
: b.block(state.update)

Loading…
Cancel
Save