various fixes

pull/1173/head
Rich Harris 7 years ago
parent c0292cbca1
commit a0aeb98685

@ -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 => {

@ -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 {

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

@ -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});
}

@ -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};`
);
});

Loading…
Cancel
Save