|
|
|
@ -27,17 +27,21 @@ export function build_set_attributes(
|
|
|
|
|
element_id,
|
|
|
|
|
attributes_id
|
|
|
|
|
) {
|
|
|
|
|
let is_dynamic = false;
|
|
|
|
|
|
|
|
|
|
/** @type {ObjectExpression['properties']} */
|
|
|
|
|
const values = [];
|
|
|
|
|
|
|
|
|
|
/** @type {Expression[]} */
|
|
|
|
|
const expressions = [];
|
|
|
|
|
|
|
|
|
|
/** @param {Expression} value */
|
|
|
|
|
function memoize(value) {
|
|
|
|
|
return b.id(`$${expressions.push(value) - 1}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const attribute of attributes) {
|
|
|
|
|
if (attribute.type === 'Attribute') {
|
|
|
|
|
const { value, has_state } = build_attribute_value(
|
|
|
|
|
attribute.value,
|
|
|
|
|
context,
|
|
|
|
|
(value, metadata) => (metadata.has_call ? get_expression_id(context.state, value) : value)
|
|
|
|
|
const { value } = build_attribute_value(attribute.value, context, (value, metadata) =>
|
|
|
|
|
metadata.has_call ? memoize(value) : value
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
@ -51,16 +55,11 @@ export function build_set_attributes(
|
|
|
|
|
} else {
|
|
|
|
|
values.push(b.init(attribute.name, value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
is_dynamic ||= has_state;
|
|
|
|
|
} else {
|
|
|
|
|
// objects could contain reactive getters -> play it safe and always assume spread attributes are reactive
|
|
|
|
|
is_dynamic = true;
|
|
|
|
|
|
|
|
|
|
let value = /** @type {Expression} */ (context.visit(attribute));
|
|
|
|
|
|
|
|
|
|
if (attribute.metadata.expression.has_call) {
|
|
|
|
|
value = get_expression_id(context.state, value);
|
|
|
|
|
value = memoize(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
values.push(b.spread(value));
|
|
|
|
@ -75,9 +74,6 @@ export function build_set_attributes(
|
|
|
|
|
build_class_directives_object(class_directives, context)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
is_dynamic ||=
|
|
|
|
|
class_directives.find((directive) => directive.metadata.expression.has_state) !== null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (style_directives.length) {
|
|
|
|
@ -88,29 +84,25 @@ export function build_set_attributes(
|
|
|
|
|
build_style_directives_object(style_directives, context)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
is_dynamic ||= style_directives.some((directive) => directive.metadata.expression.has_state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const call = b.call(
|
|
|
|
|
'$.set_attributes',
|
|
|
|
|
element_id,
|
|
|
|
|
is_dynamic ? attributes_id : b.null,
|
|
|
|
|
b.object(values),
|
|
|
|
|
element.metadata.scoped &&
|
|
|
|
|
context.state.analysis.css.hash !== '' &&
|
|
|
|
|
b.literal(context.state.analysis.css.hash),
|
|
|
|
|
is_ignored(element, 'hydration_attribute_changed') && b.true
|
|
|
|
|
context.state.init.push(
|
|
|
|
|
b.stmt(
|
|
|
|
|
b.call(
|
|
|
|
|
'$.set_attribute_effect',
|
|
|
|
|
element_id,
|
|
|
|
|
b.arrow(
|
|
|
|
|
expressions.map((_, i) => b.id(`$${i}`)),
|
|
|
|
|
b.object(values)
|
|
|
|
|
),
|
|
|
|
|
expressions.length > 0 && b.array(expressions.map((expression) => b.thunk(expression))),
|
|
|
|
|
element.metadata.scoped &&
|
|
|
|
|
context.state.analysis.css.hash !== '' &&
|
|
|
|
|
b.literal(context.state.analysis.css.hash),
|
|
|
|
|
is_ignored(element, 'hydration_attribute_changed') && b.true
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (is_dynamic) {
|
|
|
|
|
context.state.init.push(
|
|
|
|
|
b.let(attributes_id),
|
|
|
|
|
b.stmt(b.call('$.set_attribute_effect', element_id, b.thunk(b.object(values))))
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
context.state.init.push(b.stmt(call));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|