Reuse DOM elements if possible.

pull/6898/head
Yuichiro Yamashita 5 years ago
parent f8cc52a0c3
commit e2a36a4a48

@ -389,6 +389,19 @@ export default class ElementWrapper extends Wrapper {
this.add_classes(block);
this.add_manual_style_scoping(block);
if (nodes && this.renderer.options.hydratable && !this.void) {
block.chunks.claim.push(
b`${this.node.children.length > 0 ? nodes : children}.forEach(@detach);`
);
}
if (renderer.options.dev) {
const loc = renderer.locate(this.node.start);
block.chunks.hydrate.push(
b`@add_location(${this.var}, ${renderer.file_var}, ${loc.line - 1}, ${loc.column}, ${this.node.start});`
);
}
if (this.node.dynamic_tag_expr) {
const dependencies = this.node.dynamic_tag_expr.dynamic_dependencies();
if (dependencies.length) {
@ -396,28 +409,19 @@ export default class ElementWrapper extends Wrapper {
dependencies
);
const mounted: Identifier = { type: 'Identifier', name: '#mounted' };
block.chunks.update.push(b`
if (${condition}) {
@detach(${node});
${node} = ${render_statement};
@validate_dynamic_element(${this.node.dynamic_tag_expr.manipulate(block)});
${block.chunks.hydrate}
${block.event_listeners.length && b`${mounted} = false`};
${staticChildren}
}
`);
}
}
if (nodes && this.renderer.options.hydratable && !this.void) {
block.chunks.claim.push(
b`${this.node.children.length > 0 ? nodes : children}.forEach(@detach);`
);
}
if (renderer.options.dev) {
const loc = renderer.locate(this.node.start);
block.chunks.hydrate.push(
b`@add_location(${this.var}, ${renderer.file_var}, ${loc.line - 1}, ${loc.column}, ${this.node.start});`
);
}
}
render_dynamic_element(block: Block, parent_node: Identifier, parent_nodes: Identifier) {
@ -450,25 +454,20 @@ export default class ElementWrapper extends Wrapper {
this.child_dynamic_element_block.has_intro_method || this.child_dynamic_element_block.has_outro_method
);
const anchor = this.get_or_create_anchor(block, parent_node, parent_nodes);
const not_equal = this.renderer.component.component_options.immutable ? x`@not_equal` : x`@safe_not_equal`;
const condition = x`${not_equal}(${previous_tag}, ${previous_tag} = ${snippet})`;
if (has_transitions) {
const body = b`
${has_transitions
? b`
@group_outros();
@transition_out(${this.var}, 1, 1, @noop);
@check_outros();
`
: b`${this.var}.d(1);`
}
${this.var} = ${this.child_dynamic_element_block.name}(#ctx);
${this.var}.c();
${has_transitions && b`@transition_in(${this.var})`}
@transition_in(${this.var});
${this.var}.m(${this.get_update_mount_node(anchor)}, ${anchor});
`;
const not_equal = this.renderer.component.component_options.immutable ? x`@not_equal` : x`@safe_not_equal`;
const condition = x`${not_equal}(${previous_tag}, ${previous_tag} = ${snippet})`;
block.chunks.update.push(b`
if (${condition}) {
${body}
@ -476,6 +475,14 @@ export default class ElementWrapper extends Wrapper {
${this.var}.p(#ctx, #dirty);
}
`);
} else {
block.chunks.update.push(b`
${this.var}.p(#ctx, #dirty);
if (${condition}) {
${this.var}.m(${this.get_update_mount_node(anchor)}, ${anchor});
}
`);
}
if (has_transitions) {
block.chunks.intro.push(b`@transition_in(${this.var})`);

Loading…
Cancel
Save