refactor render_dom

pull/6898/head
Yuichiro Yamashita 5 years ago
parent d223df6c23
commit cdd7180306

@ -235,14 +235,6 @@ export default class ElementWrapper extends Wrapper {
} }
render(block: Block, parent_node: Identifier, parent_nodes: Identifier) { render(block: Block, parent_node: Identifier, parent_nodes: Identifier) {
this.render_element(block, parent_node, parent_nodes);
if (this.node.is_dynamic_element()) {
this.render_dynamic_element(block, parent_node, parent_nodes);
}
}
render_element(block: Block, parent_node: Identifier, parent_nodes: Identifier) {
const { renderer } = this; const { renderer } = this;
if (this.node.name === 'noscript') return; if (this.node.name === 'noscript') return;
@ -377,12 +369,11 @@ export default class ElementWrapper extends Wrapper {
} }
if (this.node.is_dynamic_element()) { if (this.node.is_dynamic_element()) {
const dependencies = this.node.tag_expr.dynamic_dependencies();
if (dependencies.length) {
const condition = block.renderer.dirty( const condition = block.renderer.dirty(
dependencies this.node.tag_expr.dynamic_dependencies()
); );
const render_statement = this.get_render_statement(block);
const mounted: Identifier = { type: 'Identifier', name: '#mounted' }; const mounted: Identifier = { type: 'Identifier', name: '#mounted' };
block.chunks.update.push(b` block.chunks.update.push(b`
if (${condition}) { if (${condition}) {
@ -394,24 +385,15 @@ export default class ElementWrapper extends Wrapper {
${staticChildren} ${staticChildren}
} }
`); `);
}
}
}
render_dynamic_element(block: Block, parent_node: Identifier, parent_nodes: Identifier) {
const previous_tag = block.get_unique_name('previous_tag'); const previous_tag = block.get_unique_name('previous_tag');
const snippet = this.node.tag_expr.manipulate(block); const snippet = this.node.tag_expr.manipulate(block);
block.add_variable(previous_tag, snippet); block.add_variable(previous_tag, snippet);
const has_transitions = !!( const has_transitions = !!(block.has_intro_method || block.has_outro_method);
block.has_intro_method || block.has_outro_method
);
const anchor = this.get_or_create_anchor(block, parent_node, parent_nodes); 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 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})`; const if_statement = x`${not_equal}(${previous_tag}, ${previous_tag} = ${snippet})`;
const render_statement = this.get_render_statement(block);
if (has_transitions) { if (has_transitions) {
const body = b` const body = b`
@ -424,18 +406,19 @@ export default class ElementWrapper extends Wrapper {
`; `;
block.chunks.update.push(b` block.chunks.update.push(b`
if (${condition}) { if (${if_statement}) {
${body} ${body}
} }
`); `);
} else { } else {
block.chunks.update.push(b` block.chunks.update.push(b`
if (${condition}) { if (${if_statement}) {
this.m(${this.get_update_mount_node(anchor)}, ${anchor}); this.m(${this.get_update_mount_node(anchor)}, ${anchor});
} }
`); `);
} }
} }
}
can_use_textcontent() { can_use_textcontent() {
return this.is_static_content && this.fragment.nodes.every(node => node.node.type === 'Text' || node.node.type === 'MustacheTag'); return this.is_static_content && this.fragment.nodes.every(node => node.node.type === 'Text' || node.node.type === 'MustacheTag');

Loading…
Cancel
Save