fix some each block stuff

pull/3986/head
Richard Harris 6 years ago
parent 81c2e4fdfc
commit 276bb8e3f4

@ -59,7 +59,7 @@ export default class EachBlockWrapper extends Wrapper {
data_length: string; data_length: string;
view_length: string; view_length: string;
} }
dependencies: string[];
context_props: Array<Node | Node[]>; context_props: Array<Node | Node[]>;
index_name: Identifier; index_name: Identifier;
@ -77,8 +77,8 @@ export default class EachBlockWrapper extends Wrapper {
this.cannot_use_innerhtml(); this.cannot_use_innerhtml();
this.not_static_content(); this.not_static_content();
const { dependencies } = node.expression; this.dependencies = node.expression.dynamic_dependencies();
block.add_dependencies(dependencies); block.add_dependencies(new Set(this.dependencies)); // TODO awkward
this.node.contexts.forEach(context => { this.node.contexts.forEach(context => {
renderer.add_to_context(context.key.name, true); renderer.add_to_context(context.key.name, true);
@ -203,7 +203,8 @@ export default class EachBlockWrapper extends Wrapper {
const snippet = this.node.expression.manipulate(block); const snippet = this.node.expression.manipulate(block);
block.chunks.init.push(b`let ${this.vars.each_block_value} = [];`); const initial_value = this.dependencies.length > 0 ? x`[]` : snippet;
block.chunks.init.push(b`let ${this.vars.each_block_value} = ${initial_value};`);
// TODO which is better — Object.create(array) or array.slice()? // TODO which is better — Object.create(array) or array.slice()?
renderer.blocks.push(b` renderer.blocks.push(b`
@ -392,7 +393,7 @@ export default class EachBlockWrapper extends Wrapper {
: `@destroy_block`; : `@destroy_block`;
block.chunks.update.push(b` block.chunks.update.push(b`
const ${this.vars.each_block_value} = ${snippet}; ${this.dependencies.length > 0 && b`const ${this.vars.each_block_value} = ${snippet};`}
${this.block.has_outros && b`@group_outros();`} ${this.block.has_outros && b`@group_outros();`}
${this.node.has_animation && b`for (let #i = 0; #i < ${view_length}; #i += 1) ${iterations}[#i].r();`} ${this.node.has_animation && b`for (let #i = 0; #i < ${view_length}; #i += 1) ${iterations}[#i].r();`}
@ -435,6 +436,7 @@ export default class EachBlockWrapper extends Wrapper {
}) { }) {
const { const {
create_each_block, create_each_block,
get_each_context,
iterations, iterations,
fixed_length, fixed_length,
data_length, data_length,
@ -445,6 +447,15 @@ export default class EachBlockWrapper extends Wrapper {
let ${iterations} = []; let ${iterations} = [];
`); `);
if (fixed_length) {
block.chunks.init.push(b`
for (let #i = 0; #i < ${view_length}; #i += 1) {
const child_ctx = ${get_each_context}(#ctx, ${this.vars.each_block_value}, #i);
${iterations}[#i] = ${create_each_block}(child_ctx);
}
`);
}
block.chunks.create.push(b` block.chunks.create.push(b`
for (let #i = 0; #i < ${view_length}; #i += 1) { for (let #i = 0; #i < ${view_length}; #i += 1) {
${iterations}[#i].c(); ${iterations}[#i].c();
@ -516,7 +527,7 @@ export default class EachBlockWrapper extends Wrapper {
${iterations}[i] = null; ${iterations}[i] = null;
}); });
`); `);
remove_old_blocks = b` remove_old_blocks = data_length !== view_length && b`
@group_outros(); @group_outros();
for (#i = ${data_length}; #i < ${view_length}; #i += 1) { for (#i = ${data_length}; #i < ${view_length}; #i += 1) {
${out}(#i); ${out}(#i);
@ -536,7 +547,7 @@ export default class EachBlockWrapper extends Wrapper {
// may rely on continuing where this iteration stopped. // may rely on continuing where this iteration stopped.
const update = b` const update = b`
${!this.block.has_update_method && b`const #old_length = ${this.vars.each_block_value}.length;`} ${!this.block.has_update_method && b`const #old_length = ${this.vars.each_block_value}.length;`}
${this.vars.each_block_value} = ${snippet}; ${this.dependencies.length > 0 && b`${this.vars.each_block_value} = ${snippet};`}
let #i; let #i;
for (#i = ${start}; #i < ${data_length}; #i += 1) { for (#i = ${start}; #i < ${data_length}; #i += 1) {

Loading…
Cancel
Save