Skip to content

Commit

Permalink
various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Feb 11, 2018
1 parent c0292cb commit a0aeb98
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/generators/dom/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
16 changes: 8 additions & 8 deletions src/generators/nodes/AwaitBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/generators/nodes/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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');

Expand Down
25 changes: 16 additions & 9 deletions src/generators/nodes/EachBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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);
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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});
}
Expand Down
4 changes: 2 additions & 2 deletions src/generators/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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};`
);
});

Expand Down

0 comments on commit a0aeb98

Please sign in to comment.