0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00

remove linked list stuff

This commit is contained in:
Rich-Harris 2018-03-24 10:19:03 -04:00
parent 174975c93e
commit f4145099f4
2 changed files with 17 additions and 57 deletions

View File

@ -246,14 +246,11 @@ export default class EachBlock extends Node {
}
) {
const key = block.getUniqueName('key');
const blocks = block.getUniqueName(`${each}_blocks`);
const lookup = block.getUniqueName(`${each}_lookup`);
const iteration = block.getUniqueName(`${each}_iteration`);
const head = block.getUniqueName(`${each}_head`);
const last = block.getUniqueName(`${each}_last`);
block.addVariable(blocks, '[]');
block.addVariable(lookup, `@blankObject()`);
block.addVariable(head);
block.addVariable(last);
if (this.children[0].isDomNode()) {
this.block.first = this.children[0].var;
@ -270,14 +267,9 @@ export default class EachBlock extends Node {
block.builders.init.addBlock(deindent`
for (var #i = 0; #i < ${each_block_value}.${length}; #i += 1) {
var ${key} = ${each_block_value}[#i].${this.key};
var ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, ${key}, @assign({}, state, {
${blocks}[#i] = ${lookup}[${key}] = ${create_each_block}(#component, ${key}, @assign({}, state, {
${this.contextProps.join(',\n')}
}));
if (${last}) ${last}.next = ${iteration};
${last} = ${iteration};
if (#i === 0) ${head} = ${iteration};
}
`);
@ -286,29 +278,17 @@ export default class EachBlock extends Node {
const anchorNode = parentNode ? 'null' : 'anchor';
block.builders.create.addBlock(deindent`
var ${iteration} = ${head};
while (${iteration}) {
${iteration}.c();
${iteration} = ${iteration}.next;
}
for (#i = 0; #i < ${blocks}.length; #i += 1) ${blocks}[#i].c();
`);
if (parentNodes) {
block.builders.claim.addBlock(deindent`
var ${iteration} = ${head};
while (${iteration}) {
${iteration}.l(${parentNodes});
${iteration} = ${iteration}.next;
}
for (#i = 0; #i < ${blocks}.length; #i += 1) ${blocks}[#i].l(${parentNodes});
`);
}
block.builders.mount.addBlock(deindent`
var ${iteration} = ${head};
while (${iteration}) {
${iteration}.${mountOrIntro}(${initialMountNode}, ${anchorNode});
${iteration} = ${iteration}.next;
}
for (#i = 0; #i < ${blocks}.length; #i += 1) ${blocks}[#i].${mountOrIntro}(${initialMountNode}, ${anchorNode});
`);
const dynamic = this.block.hasUpdateMethod;
@ -316,31 +296,21 @@ export default class EachBlock extends Node {
block.builders.update.addBlock(deindent`
var ${each_block_value} = ${snippet};
@updateKeyedEach(#component, ${key}, changed, "${this.key}", ${dynamic}, ${each_block_value}, ${head}, ${lookup}, ${updateMountNode}, ${String(this.block.hasOutroMethod)}, ${create_each_block}, "${mountOrIntro}", function(#i) {
${blocks} = @updateKeyedEach(${blocks}, #component, ${key}, changed, "${this.key}", ${dynamic}, ${each_block_value}, ${lookup}, ${updateMountNode}, ${String(this.block.hasOutroMethod)}, ${create_each_block}, "${mountOrIntro}", function(#i) {
return @assign({}, state, {
${this.contextProps.join(',\n')}
});
});
${head} = ${lookup}[${each_block_value}[0] && ${each_block_value}[0].${this.key}];
`);
if (!parentNode) {
block.builders.unmount.addBlock(deindent`
var ${iteration} = ${head};
while (${iteration}) {
${iteration}.u();
${iteration} = ${iteration}.next;
}
for (#i = 0; #i < ${blocks}.length; #i += 1) ${blocks}[#i].u();
`);
}
block.builders.destroy.addBlock(deindent`
var ${iteration} = ${head};
while (${iteration}) {
${iteration}.d();
${iteration} = ${iteration}.next;
}
for (#i = 0; #i < ${blocks}.length; #i += 1) ${blocks}[#i].d();
`);
}

View File

@ -17,16 +17,13 @@ export function outroAndDestroyIteration(iteration, lookup) {
});
}
export function updateKeyedEach(component, key, changed, key_prop, dynamic, list, head, lookup, node, has_outro, create_each_block, intro_method, get_context) {
export function updateKeyedEach(blocks, component, key, changed, key_prop, dynamic, list, lookup, node, has_outro, create_each_block, intro_method, get_context) {
var old_indexes = {};
var i = 0;
var old_keys = [];
while (head) {
old_keys.push(head.key);
old_indexes[head.key] = i++;
head = head.next;
}
var old_keys = blocks.map(function(block) {
return block.key;
});
var o = old_keys.length;
var n = list.length;
@ -114,17 +111,10 @@ export function updateKeyedEach(component, key, changed, key_prop, dynamic, list
while (n--) {
var key = list[n][key_prop];
new_blocks[key][intro_method](node, next && next.first);
next = new_blocks[key];
next = lookup[key] = new_blocks[key];
}
// TODO keep track of keys, so this is unnecessary
var next = null;
var i = list.length;
while (i--) {
var key = list[i][key_prop];
var block = lookup[key] = new_blocks[key];
block.next = next;
next = block;
}
return list.map(function(item) {
return new_blocks[item[key_prop]];
});
}