From f4145099f4fe050cc20656c6c74bca868d35ef52 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sat, 24 Mar 2018 10:19:03 -0400 Subject: [PATCH] remove linked list stuff --- src/generators/nodes/EachBlock.ts | 48 ++++++------------------------- src/shared/keyed-each.js | 26 ++++++----------- 2 files changed, 17 insertions(+), 57 deletions(-) diff --git a/src/generators/nodes/EachBlock.ts b/src/generators/nodes/EachBlock.ts index 6e248a3a63..1b96738a9b 100644 --- a/src/generators/nodes/EachBlock.ts +++ b/src/generators/nodes/EachBlock.ts @@ -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(); `); } diff --git a/src/shared/keyed-each.js b/src/shared/keyed-each.js index a8fe9d1399..a53fe9e7dc 100644 --- a/src/shared/keyed-each.js +++ b/src/shared/keyed-each.js @@ -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]]; + }); } \ No newline at end of file