Lint and format src/compiler/compile/render_dom/wrappers/KeyBlock.js

pull/8569/head
Simon Holthausen 3 years ago
parent 4adbb5439b
commit 28564df0b2

@ -5,7 +5,6 @@ import { b, x } from 'code-red';
/** @extends Wrapper */ /** @extends Wrapper */
export default class KeyBlockWrapper extends Wrapper { export default class KeyBlockWrapper extends Wrapper {
/** @type {import('../../nodes/KeyBlock.js').default} */ /** @type {import('../../nodes/KeyBlock.js').default} */
node; node;
@ -42,7 +41,14 @@ export default class KeyBlockWrapper extends Wrapper {
renderer.blocks.push(block); renderer.blocks.push(block);
} }
this.block = block; this.block = block;
this.fragment = new FragmentWrapper(renderer, this.block, node.children, this, strip_whitespace, next_sibling); this.fragment = new FragmentWrapper(
renderer,
this.block,
node.children,
this,
strip_whitespace,
next_sibling
);
} }
/** /**
@ -53,8 +59,7 @@ export default class KeyBlockWrapper extends Wrapper {
render(block, parent_node, parent_nodes) { render(block, parent_node, parent_nodes) {
if (this.dependencies.length === 0) { if (this.dependencies.length === 0) {
this.render_static_key(block, parent_node, parent_nodes); this.render_static_key(block, parent_node, parent_nodes);
} } else {
else {
this.render_dynamic_key(block, parent_node, parent_nodes); this.render_dynamic_key(block, parent_node, parent_nodes);
} }
} }
@ -74,62 +79,67 @@ export default class KeyBlockWrapper extends Wrapper {
* @param {import('estree').Identifier} parent_nodes * @param {import('estree').Identifier} parent_nodes
*/ */
render_dynamic_key(block, parent_node, parent_nodes) { render_dynamic_key(block, parent_node, parent_nodes) {
this.fragment.render(this.block, null, /** @type {unknown} */ /** @type {import('estree').Identifier} */ ((x `#nodes`))); this.fragment.render(
this.block,
null,
/** @type {unknown} */ /** @type {import('estree').Identifier} */ (x`#nodes`)
);
const has_transitions = !!(this.block.has_intro_method || this.block.has_outro_method); const has_transitions = !!(this.block.has_intro_method || this.block.has_outro_method);
const dynamic = this.block.has_update_method; const dynamic = this.block.has_update_method;
const previous_key = block.get_unique_name('previous_key'); const previous_key = block.get_unique_name('previous_key');
const snippet = this.node.expression.manipulate(block); const snippet = this.node.expression.manipulate(block);
block.add_variable(previous_key, snippet); block.add_variable(previous_key, snippet);
const not_equal = this.renderer.component.component_options.immutable const not_equal = this.renderer.component.component_options.immutable
? x `@not_equal` ? x`@not_equal`
: x `@safe_not_equal`; : x`@safe_not_equal`;
const condition = x `${this.renderer.dirty(this.dependencies)} && ${not_equal}(${previous_key}, ${previous_key} = ${snippet})`; const condition = x`${this.renderer.dirty(
block.chunks.init.push(b ` this.dependencies
)} && ${not_equal}(${previous_key}, ${previous_key} = ${snippet})`;
block.chunks.init.push(b`
let ${this.var} = ${this.block.name}(#ctx); let ${this.var} = ${this.block.name}(#ctx);
`); `);
block.chunks.create.push(b `${this.var}.c();`); block.chunks.create.push(b`${this.var}.c();`);
if (this.renderer.options.hydratable) { if (this.renderer.options.hydratable) {
block.chunks.claim.push(b `${this.var}.l(${parent_nodes});`); block.chunks.claim.push(b`${this.var}.l(${parent_nodes});`);
} }
block.chunks.mount.push(b `${this.var}.m(${parent_node || '#target'}, ${parent_node ? 'null' : '#anchor'});`); block.chunks.mount.push(
b`${this.var}.m(${parent_node || '#target'}, ${parent_node ? 'null' : '#anchor'});`
);
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 body = b ` const body = b`
${has_transitions ${
? b ` has_transitions
? b`
@group_outros(); @group_outros();
@transition_out(${this.var}, 1, 1, @noop); @transition_out(${this.var}, 1, 1, @noop);
@check_outros(); @check_outros();
` `
: b `${this.var}.d(1);`} : b`${this.var}.d(1);`
}
${this.var} = ${this.block.name}(#ctx); ${this.var} = ${this.block.name}(#ctx);
${this.var}.c(); ${this.var}.c();
${has_transitions && b `@transition_in(${this.var}, 1)`} ${has_transitions && b`@transition_in(${this.var}, 1)`}
${this.var}.m(${this.get_update_mount_node(anchor)}, ${anchor}); ${this.var}.m(${this.get_update_mount_node(anchor)}, ${anchor});
`; `;
if (dynamic) { if (dynamic) {
block.chunks.update.push(b ` block.chunks.update.push(b`
if (${condition}) { if (${condition}) {
${body} ${body}
} else { } else {
${this.var}.p(#ctx, #dirty); ${this.var}.p(#ctx, #dirty);
} }
`); `);
} } else {
else { block.chunks.update.push(b`
block.chunks.update.push(b `
if (${condition}) { if (${condition}) {
${body} ${body}
} }
`); `);
} }
if (has_transitions) { if (has_transitions) {
block.chunks.intro.push(b `@transition_in(${this.var})`); block.chunks.intro.push(b`@transition_in(${this.var})`);
block.chunks.outro.push(b `@transition_out(${this.var})`); block.chunks.outro.push(b`@transition_out(${this.var})`);
} }
block.chunks.destroy.push(b `${this.var}.d(detaching)`); block.chunks.destroy.push(b`${this.var}.d(detaching)`);
} }
} }

Loading…
Cancel
Save