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

pull/8569/head
Simon Holthausen 3 years ago
parent cb0d76e264
commit 788902480c

@ -9,7 +9,6 @@ import Expression from '../../nodes/shared/Expression.js';
/** @extends Wrapper */ /** @extends Wrapper */
class AwaitBlockBranch extends Wrapper { class AwaitBlockBranch extends Wrapper {
/** @type {AwaitBlockWrapper} */ /** @type {AwaitBlockWrapper} */
parent; parent;
@ -60,7 +59,14 @@ class AwaitBlockBranch extends Wrapper {
type: status type: status
}); });
this.add_context(parent.node[status + '_node'], parent.node[status + '_contexts']); this.add_context(parent.node[status + '_node'], parent.node[status + '_contexts']);
this.fragment = new FragmentWrapper(renderer, this.block, this.node.children, parent, strip_whitespace, next_sibling); this.fragment = new FragmentWrapper(
renderer,
this.block,
this.node.children,
parent,
strip_whitespace,
next_sibling
);
this.is_dynamic = this.block.dependencies.size > 0; this.is_dynamic = this.block.dependencies.size > 0;
} }
@ -69,16 +75,13 @@ class AwaitBlockBranch extends Wrapper {
* @param {import('../../nodes/shared/Context.js').Context[]} contexts * @param {import('../../nodes/shared/Context.js').Context[]} contexts
*/ */
add_context(node, contexts) { add_context(node, contexts) {
if (!node) if (!node) return;
return;
if (node.type === 'Identifier') { if (node.type === 'Identifier') {
this.value = node.name; this.value = node.name;
this.renderer.add_to_context(this.value, true); this.renderer.add_to_context(this.value, true);
} } else {
else {
contexts.forEach((context) => { contexts.forEach((context) => {
if (context.type !== 'DestructuredVariable') if (context.type !== 'DestructuredVariable') return;
return;
this.renderer.add_to_context(context.key.name, true); this.renderer.add_to_context(context.key.name, true);
}); });
this.value = this.block.parent.get_unique_name('value').name; this.value = this.block.parent.get_unique_name('value').name;
@ -115,14 +118,19 @@ class AwaitBlockBranch extends Wrapper {
const props = this.is_destructured const props = this.is_destructured
? this.value_contexts.map((prop) => { ? this.value_contexts.map((prop) => {
if (prop.type === 'ComputedProperty') { if (prop.type === 'ComputedProperty') {
const expression = new Expression(this.renderer.component, this.node, this.has_consts(this.node) ? this.node.scope : null, prop.key); const expression = new Expression(
return b `const ${prop.property_name} = ${expression.manipulate(this.block, '#ctx')};`; this.renderer.component,
} this.node,
else { this.has_consts(this.node) ? this.node.scope : null,
prop.key
);
return b`const ${prop.property_name} = ${expression.manipulate(this.block, '#ctx')};`;
} else {
/** @param {any} name */ /** @param {any} name */
const to_ctx = (name) => this.renderer.reference(name); const to_ctx = (name) => this.renderer.reference(name);
return b `#ctx[${this.block.renderer.context_lookup.get(prop.key.name).index}] = ${prop.default_modifier(prop.modifier(x `#ctx[${this.value_index}]`), to_ctx)};`; return b`#ctx[${
this.block.renderer.context_lookup.get(prop.key.name).index
}] = ${prop.default_modifier(prop.modifier(x`#ctx[${this.value_index}]`), to_ctx)};`;
} }
}) })
: null; : null;
@ -130,22 +138,21 @@ class AwaitBlockBranch extends Wrapper {
? add_const_tags(this.block, this.node.const_tags, '#ctx') ? add_const_tags(this.block, this.node.const_tags, '#ctx')
: null; : null;
const get_context = this.block.renderer.component.get_unique_name(`get_${this.status}_context`); const get_context = this.block.renderer.component.get_unique_name(`get_${this.status}_context`);
this.block.renderer.blocks.push(b ` this.block.renderer.blocks.push(b`
function ${get_context}(#ctx) { function ${get_context}(#ctx) {
${props} ${props}
${const_tags_props} ${const_tags_props}
} }
`); `);
this.block.chunks.declarations.push(b `${get_context}(#ctx)`); this.block.chunks.declarations.push(b`${get_context}(#ctx)`);
if (this.block.has_update_method) { if (this.block.has_update_method) {
this.block.chunks.update.unshift(b `${get_context}(#ctx)`); this.block.chunks.update.unshift(b`${get_context}(#ctx)`);
} }
} }
} }
/** @extends Wrapper */ /** @extends Wrapper */
export default class AwaitBlockWrapper extends Wrapper { export default class AwaitBlockWrapper extends Wrapper {
/** @type {import('../../nodes/AwaitBlock.js').default} */ /** @type {import('../../nodes/AwaitBlock.js').default} */
node; node;
@ -177,17 +184,23 @@ export default class AwaitBlockWrapper extends Wrapper {
let has_outros = false; let has_outros = false;
['pending', 'then', 'catch'].forEach((status) => { ['pending', 'then', 'catch'].forEach((status) => {
const child = this.node[status]; const child = this.node[status];
const branch = new AwaitBlockBranch(status, renderer, block, this, child, strip_whitespace, next_sibling); const branch = new AwaitBlockBranch(
status,
renderer,
block,
this,
child,
strip_whitespace,
next_sibling
);
renderer.blocks.push(branch.block); renderer.blocks.push(branch.block);
if (branch.is_dynamic) { if (branch.is_dynamic) {
is_dynamic = true; is_dynamic = true;
// TODO should blocks update their own parents? // TODO should blocks update their own parents?
block.add_dependencies(branch.block.dependencies); block.add_dependencies(branch.block.dependencies);
} }
if (branch.block.has_intros) if (branch.block.has_intros) has_intros = true;
has_intros = true; if (branch.block.has_outros) has_outros = true;
if (branch.block.has_outros)
has_outros = true;
this[status] = branch; this[status] = branch;
}); });
['pending', 'then', 'catch'].forEach((status) => { ['pending', 'then', 'catch'].forEach((status) => {
@ -215,7 +228,7 @@ export default class AwaitBlockWrapper extends Wrapper {
block.maintain_context = true; block.maintain_context = true;
/** @type {any} */ /** @type {any} */
const info_props = x `{ const info_props = x`{
ctx: #ctx, ctx: #ctx,
current: null, current: null,
token: null, token: null,
@ -225,82 +238,79 @@ export default class AwaitBlockWrapper extends Wrapper {
catch: ${this.catch.block.name}, catch: ${this.catch.block.name},
value: ${this.then.value_index}, value: ${this.then.value_index},
error: ${this.catch.value_index}, error: ${this.catch.value_index},
blocks: ${this.pending.block.has_outro_method && x `[,,,]`} blocks: ${this.pending.block.has_outro_method && x`[,,,]`}
}`; }`;
block.chunks.init.push(b ` block.chunks.init.push(b`
let ${info} = ${info_props}; let ${info} = ${info_props};
`); `);
block.chunks.init.push(b ` block.chunks.init.push(b`
@handle_promise(${promise} = ${snippet}, ${info}); @handle_promise(${promise} = ${snippet}, ${info});
`); `);
block.chunks.create.push(b ` block.chunks.create.push(b`
${info}.block.c(); ${info}.block.c();
`); `);
if (parent_nodes && this.renderer.options.hydratable) { if (parent_nodes && this.renderer.options.hydratable) {
block.chunks.claim.push(b ` block.chunks.claim.push(b`
${info}.block.l(${parent_nodes}); ${info}.block.l(${parent_nodes});
`); `);
} }
const initial_mount_node = parent_node || '#target'; const initial_mount_node = parent_node || '#target';
const anchor_node = parent_node ? 'null' : '#anchor'; const anchor_node = parent_node ? 'null' : '#anchor';
const has_transitions = this.pending.block.has_intro_method || this.pending.block.has_outro_method; const has_transitions =
block.chunks.mount.push(b ` this.pending.block.has_intro_method || this.pending.block.has_outro_method;
block.chunks.mount.push(b`
${info}.block.m(${initial_mount_node}, ${info}.anchor = ${anchor_node}); ${info}.block.m(${initial_mount_node}, ${info}.anchor = ${anchor_node});
${info}.mount = () => ${update_mount_node}; ${info}.mount = () => ${update_mount_node};
${info}.anchor = ${anchor}; ${info}.anchor = ${anchor};
`); `);
if (has_transitions) { if (has_transitions) {
block.chunks.intro.push(b `@transition_in(${info}.block);`); block.chunks.intro.push(b`@transition_in(${info}.block);`);
} }
const dependencies = this.node.expression.dynamic_dependencies(); const dependencies = this.node.expression.dynamic_dependencies();
const update_await_block_branch = b `@update_await_block_branch(${info}, #ctx, #dirty)`; const update_await_block_branch = b`@update_await_block_branch(${info}, #ctx, #dirty)`;
if (dependencies.length > 0) { if (dependencies.length > 0) {
const condition = x ` const condition = x`
${block.renderer.dirty(dependencies)} && ${block.renderer.dirty(dependencies)} &&
${promise} !== (${promise} = ${snippet}) && ${promise} !== (${promise} = ${snippet}) &&
@handle_promise(${promise}, ${info})`; @handle_promise(${promise}, ${info})`;
block.chunks.update.push(b `${info}.ctx = #ctx;`); block.chunks.update.push(b`${info}.ctx = #ctx;`);
if (this.pending.block.has_update_method) { if (this.pending.block.has_update_method) {
block.chunks.update.push(b ` block.chunks.update.push(b`
if (${condition}) { if (${condition}) {
} else { } else {
${update_await_block_branch} ${update_await_block_branch}
} }
`); `);
} } else {
else { block.chunks.update.push(b`
block.chunks.update.push(b `
${condition} ${condition}
`); `);
} }
} } else {
else {
if (this.pending.block.has_update_method) { if (this.pending.block.has_update_method) {
block.chunks.update.push(b ` block.chunks.update.push(b`
${update_await_block_branch} ${update_await_block_branch}
`); `);
} }
} }
if (this.pending.block.has_outro_method) { if (this.pending.block.has_outro_method) {
block.chunks.outro.push(b ` block.chunks.outro.push(b`
for (let #i = 0; #i < 3; #i += 1) { for (let #i = 0; #i < 3; #i += 1) {
const block = ${info}.blocks[#i]; const block = ${info}.blocks[#i];
@transition_out(block); @transition_out(block);
} }
`); `);
} }
block.chunks.destroy.push(b ` block.chunks.destroy.push(b`
${info}.block.d(${parent_node ? null : 'detaching'}); ${info}.block.d(${parent_node ? null : 'detaching'});
${info}.token = null; ${info}.token = null;
${info} = null; ${info} = null;
`); `);
[this.pending, this.then, this.catch].forEach((branch) => { [this.pending, this.then, this.catch].forEach((branch) => {
branch.render(branch.block, null, /** @type {import('estree').Identifier} */ (x `#nodes`)); branch.render(branch.block, null, /** @type {import('estree').Identifier} */ (x`#nodes`));
}); });
} }
} }
/** @typedef {'pending' | 'then' | 'catch'} Status */ /** @typedef {'pending' | 'then' | 'catch'} Status */

Loading…
Cancel
Save