gh-13270
Rich Harris 2 months ago
commit 77f90a7e8d

@ -27,7 +27,7 @@ export function build_set_attributes(
is_custom_element is_custom_element
) { ) {
let needs_isolation = false; let needs_isolation = false;
let is_reactive = false; let has_state = false;
/** @type {ObjectExpression['properties']} */ /** @type {ObjectExpression['properties']} */
const values = []; const values = [];
@ -38,8 +38,7 @@ export function build_set_attributes(
if ( if (
is_event_attribute(attribute) && is_event_attribute(attribute) &&
(get_attribute_expression(attribute).type === 'ArrowFunctionExpression' || (value.type === 'ArrowFunctionExpression' || value.type === 'FunctionExpression')
get_attribute_expression(attribute).type === 'FunctionExpression')
) { ) {
// Give the event handler a stable ID so it isn't removed and readded on every update // Give the event handler a stable ID so it isn't removed and readded on every update
const id = context.state.scope.generate('event_handler'); const id = context.state.scope.generate('event_handler');
@ -48,22 +47,22 @@ export function build_set_attributes(
} else { } else {
values.push(b.init(attribute.name, value)); values.push(b.init(attribute.name, value));
} }
has_state ||= attribute.metadata.expression.has_state;
} else { } else {
values.push(b.spread(/** @type {Expression} */ (context.visit(attribute)))); values.push(b.spread(/** @type {Expression} */ (context.visit(attribute))));
}
is_reactive ||=
attribute.metadata.expression.has_state ||
// objects could contain reactive getters -> play it safe and always assume spread attributes are reactive // objects could contain reactive getters -> play it safe and always assume spread attributes are reactive
attribute.type === 'SpreadAttribute'; has_state = true;
needs_isolation ||=
attribute.type === 'SpreadAttribute' && attribute.metadata.expression.has_call; needs_isolation ||= attribute.metadata.expression.has_call;
}
} }
const call = b.call( const call = b.call(
'$.set_attributes', '$.set_attributes',
element_id, element_id,
is_reactive ? attributes_id : b.literal(null), has_state ? attributes_id : b.literal(null),
b.object(values), b.object(values),
context.state.analysis.css.hash !== '' && b.literal(context.state.analysis.css.hash), context.state.analysis.css.hash !== '' && b.literal(context.state.analysis.css.hash),
preserve_attribute_case, preserve_attribute_case,
@ -71,7 +70,7 @@ export function build_set_attributes(
is_ignored(element, 'hydration_attribute_changed') && b.true is_ignored(element, 'hydration_attribute_changed') && b.true
); );
if (is_reactive) { if (has_state) {
context.state.init.push(b.let(attributes_id)); context.state.init.push(b.let(attributes_id));
const update = b.stmt(b.assignment('=', attributes_id, call)); const update = b.stmt(b.assignment('=', attributes_id, call));

Loading…
Cancel
Save