differentiate unchange attrs and always change attrs

pull/4487/head
Tan Li Hau 6 years ago
parent 20e079007c
commit 33500ada61

@ -224,7 +224,9 @@ export default class InlineComponentWrapper extends Wrapper {
const condition = dependencies.size > 0 && (dependencies.size !== all_dependencies.size) const condition = dependencies.size > 0 && (dependencies.size !== all_dependencies.size)
? renderer.dirty(Array.from(dependencies)) ? renderer.dirty(Array.from(dependencies))
: null; : null;
const unchanged = dependencies.size === 0;
let change_object;
if (attr.is_spread) { if (attr.is_spread) {
const value = attr.expression.manipulate(block); const value = attr.expression.manipulate(block);
initial_props.push(value); initial_props.push(value);
@ -233,13 +235,20 @@ export default class InlineComponentWrapper extends Wrapper {
if (attr.expression.node.type !== 'ObjectExpression') { if (attr.expression.node.type !== 'ObjectExpression') {
value_object = x`@get_spread_object(${value})`; value_object = x`@get_spread_object(${value})`;
} }
changes.push(condition ? x`${condition} && ${value_object}` : value_object); change_object = value_object;
} else { } else {
const obj = x`{ ${name}: ${attr.get_value(block)} }`; const obj = x`{ ${name}: ${attr.get_value(block)} }`;
initial_props.push(obj); initial_props.push(obj);
change_object = obj;
changes.push(condition ? x`${condition} && ${obj}` : x`${levels}[${i}]`);
} }
changes.push(
unchanged
? x`${levels}[${i}]`
: condition
? x`${condition} && ${change_object}`
: change_object
);
}); });
block.chunks.init.push(b` block.chunks.init.push(b`

@ -0,0 +1,13 @@
<script>
export let foo;
export let baz;
export let qux;
export let quux;
export let selected;
</script>
<p>foo: {foo}</p>
<p>baz: {baz} ({typeof baz})</p>
<p>qux: {qux}</p>
<p>quux: {quux}</p>
<p>selected: {selected}</p>

@ -0,0 +1,76 @@
export default {
props: {
list: [{
foo: 'lol',
baz: 40 + 2,
qux: 5,
quux: 'core'
}, {
foo: 'lolzz',
baz: 50 + 2,
qux: 1,
quux: 'quuxx'
}],
},
html: `
<div>
<p>foo: lol</p>
<p>baz: 42 (number)</p>
<p>qux: 0</p>
<p>quux: core</p>
<p>selected: true</p>
<p>foo: lolzz</p>
<p>baz: 52 (number)</p>
<p>qux: 0</p>
<p>quux: quuxx</p>
<p>selected: false</p>
</div>
`,
test({ assert, component, target }) {
component.list = [{
foo: 'lol',
baz: 40 + 3,
qux: 8,
quux: 'heart'
}, {
foo: 'lolzz',
baz: 50 + 3,
qux: 8,
quux: 'heartxx'
}];
assert.htmlEqual(target.innerHTML, `
<div>
<p>foo: lol</p>
<p>baz: 43 (number)</p>
<p>qux: 0</p>
<p>quux: heart</p>
<p>selected: true</p>
<p>foo: lolzz</p>
<p>baz: 53 (number)</p>
<p>qux: 0</p>
<p>quux: heartxx</p>
<p>selected: false</p>
</div>
`);
component.qux = 1;
assert.htmlEqual(target.innerHTML, `
<div>
<p>foo: lol</p>
<p>baz: 43 (number)</p>
<p>qux: 1</p>
<p>quux: heart</p>
<p>selected: false</p>
<p>foo: lolzz</p>
<p>baz: 53 (number)</p>
<p>qux: 1</p>
<p>quux: heartxx</p>
<p>selected: true</p>
</div>
`);
}
};

@ -0,0 +1,12 @@
<script>
import Widget from './Widget.svelte';
export let list;
export let qux = 0;
</script>
<div>
{#each list as item, index (item.foo)}
<Widget {...item} qux={qux} selected={qux === index} />
{/each}
</div>
Loading…
Cancel
Save