From a0aeb98685ff820b60191730f149d7550a6da0cf Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 11 Feb 2018 10:00:23 -0500 Subject: [PATCH] various fixes --- src/generators/dom/Block.ts | 2 +- src/generators/nodes/AwaitBlock.ts | 16 ++++++++-------- src/generators/nodes/Component.ts | 4 ++-- src/generators/nodes/EachBlock.ts | 25 ++++++++++++++++--------- src/generators/nodes/Element.ts | 4 ++-- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/generators/dom/Block.ts b/src/generators/dom/Block.ts index 2a50b3cfdb..15ee602ee2 100644 --- a/src/generators/dom/Block.ts +++ b/src/generators/dom/Block.ts @@ -326,7 +326,7 @@ export default class Block { return deindent` ${this.comment && `// ${escape(this.comment)}`} - function ${this.name}(#component, state${this.key ? `, ${localKey}` : ''}) { + function ${this.name}(#component${this.key ? `, ${localKey}` : ''}, state) { ${this.variables.size > 0 && `var ${Array.from(this.variables.keys()) .map(key => { diff --git a/src/generators/nodes/AwaitBlock.ts b/src/generators/nodes/AwaitBlock.ts index c5cce65758..5fa04d2c74 100644 --- a/src/generators/nodes/AwaitBlock.ts +++ b/src/generators/nodes/AwaitBlock.ts @@ -35,7 +35,7 @@ export default class AwaitBlock extends Node { ].forEach(([status, arg]) => { const child = this[status]; - const context = block.getUniqueName(arg || '_'); // TODO can we remove the extra param from pending blocks? + const context = arg || '_'; const contexts = new Map(block.contexts); contexts.set(arg, context); @@ -103,11 +103,11 @@ export default class AwaitBlock extends Node { // but it's probably not worth it block.builders.init.addBlock(deindent` - function ${replace_await_block}(${token}, type, ${value}, state) { + function ${replace_await_block}(${token}, type, state) { if (${token} !== ${await_token}) return; var ${old_block} = ${await_block}; - ${await_block} = (${await_block_type} = type)(state, ${resolved} = ${value}, #component); + ${await_block} = (${await_block_type} = type)(#component, state); if (${old_block}) { ${old_block}.u(); @@ -125,21 +125,21 @@ export default class AwaitBlock extends Node { if (@isPromise(${promise})) { ${promise}.then(function(${value}) { var state = #component.get(); - ${replace_await_block}(${token}, ${create_then_block}, ${value}, state); + ${replace_await_block}(${token}, ${create_then_block}, @assign({}, state, { ${this.then.block.context}: ${value} })); }, function (${error}) { var state = #component.get(); - ${replace_await_block}(${token}, ${create_catch_block}, ${error}, state); + ${replace_await_block}(${token}, ${create_catch_block}, @assign({}, state, { ${this.catch.block.context}: ${error} })); }); // if we previously had a then/catch block, destroy it if (${await_block_type} !== ${create_pending_block}) { - ${replace_await_block}(${token}, ${create_pending_block}, null, state); + ${replace_await_block}(${token}, ${create_pending_block}, state); return true; } } else { ${resolved} = ${promise}; if (${await_block_type} !== ${create_then_block}) { - ${replace_await_block}(${token}, ${create_then_block}, ${resolved}, state); + ${replace_await_block}(${token}, ${create_then_block}, @assign({}, state, { ${this.then.block.context}: ${resolved} })); return true; } } @@ -182,7 +182,7 @@ export default class AwaitBlock extends Node { if (${conditions.join(' && ')}) { // nothing } else { - ${await_block}.p(changed, state, ${resolved}); + ${await_block}.p(changed, state); } `); } else { diff --git a/src/generators/nodes/Component.ts b/src/generators/nodes/Component.ts index 83e88f2bda..8039cc2a26 100644 --- a/src/generators/nodes/Component.ts +++ b/src/generators/nodes/Component.ts @@ -417,7 +417,7 @@ export default class Component extends Node { const listName = block.listNames.get(contextName); const indexName = block.indexNames.get(contextName); - return `${listName}: ${listName},\n${indexName}: ${indexName}`; + return `${listName}: state.${listName},\n${indexName}: state.${indexName}`; }) .join(',\n'); @@ -428,7 +428,7 @@ export default class Component extends Node { const listName = block.listNames.get(contextName); const indexName = block.indexNames.get(contextName); - return `${name_context}.${listName} = ${listName};\n${name_context}.${indexName} = ${indexName};`; + return `${name_context}.${listName} = state.${listName};\n${name_context}.${indexName} = state.${indexName};`; }) .join('\n'); diff --git a/src/generators/nodes/EachBlock.ts b/src/generators/nodes/EachBlock.ts index 9a094affe4..5e6669254a 100644 --- a/src/generators/nodes/EachBlock.ts +++ b/src/generators/nodes/EachBlock.ts @@ -35,12 +35,11 @@ export default class EachBlock extends Node { block.addDependencies(dependencies); const indexNames = new Map(block.indexNames); - const indexName = - this.index || block.getUniqueName(`${this.context}_index`); + const indexName = this.index || `${this.context}_index`; indexNames.set(this.context, indexName); const listNames = new Map(block.listNames); - const listName = block.getUniqueName( + const listName = ( (this.expression.type === 'MemberExpression' && !this.expression.computed) ? this.expression.property.name : this.expression.type === 'Identifier' ? this.expression.name : `each_value` @@ -50,9 +49,9 @@ export default class EachBlock extends Node { const contextTypes = new Map(block.contextTypes); contextTypes.set(this.context, 'each'); - const context = block.getUniqueName(this.context); + const context = this.context; const contexts = new Map(block.contexts); - contexts.set(this.context, context); + contexts.set(this.context, context); // TODO this is now redundant const indexes = new Map(block.indexes); if (this.index) indexes.set(this.index, this.context); @@ -272,7 +271,10 @@ 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, state, ${key}); + var ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, ${key}, @assign({}, state, { + ${this.context}: ${each_block_value}[#i], + ${this.block.indexName}: #i + })); if (${last}) ${last}.next = ${iteration}; ${iteration}.last = ${last}; @@ -376,8 +378,13 @@ export default class EachBlock extends Node { var ${key} = ${each_block_value}[#i].${this.key}; var ${iteration} = ${lookup}[${key}]; + var ${this.each_context} = @assign({}, state, { + ${this.context}: ${each_block_value}[#i], + ${this.block.indexName}: #i + }); + ${dynamic && - `if (${iteration}) ${iteration}.p(changed, state);`} + `if (${iteration}) ${iteration}.p(changed, ${this.each_context});`} if (${expected}) { if (${key} === ${expected}.key) { @@ -398,7 +405,7 @@ export default class EachBlock extends Node { if (!${expected}) ${iteration}.m(${updateMountNode}, ${anchor}); } else { // key is being inserted - ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, state, ${key}); + ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, ${key}, ${this.each_context}); ${iteration}.c(); ${iteration}.${mountOrIntro}(${updateMountNode}, ${expected}.first); @@ -413,7 +420,7 @@ export default class EachBlock extends Node { ${iteration}.next = null; ${iteration}.m(${updateMountNode}, ${anchor}); } else { - ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, state, ${key}); + ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, ${key}, ${this.each_context}); ${iteration}.c(); ${iteration}.${mountOrIntro}(${updateMountNode}, ${anchor}); } diff --git a/src/generators/nodes/Element.ts b/src/generators/nodes/Element.ts index a512b21d94..13f8ccff5c 100644 --- a/src/generators/nodes/Element.ts +++ b/src/generators/nodes/Element.ts @@ -386,10 +386,10 @@ export default class Element extends Node { const indexName = block.indexNames.get(contextName); initialProps.push( - `${listName}: ${listName},\n${indexName}: ${indexName}` + `${listName}: state.${listName},\n${indexName}: state.${indexName}` ); updates.push( - `${name}._svelte.${listName} = ${listName};\n${name}._svelte.${indexName} = ${indexName};` + `${name}._svelte.${listName} = state.${listName};\n${name}._svelte.${indexName} = state.${indexName};` ); });