Skip to content

Commit

Permalink
invalidate $$props or $$restProps only when there's changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Jul 10, 2020
1 parent d19bcef commit 52863a6
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/compiler/compile/render_dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export default function dom(
const set = (uses_props || uses_rest || writable_props.length > 0 || component.slots.size > 0)
? x`
${$$props} => {
${(uses_props || uses_rest) && b`if (@is_empty(${$$props})) return;`}
${uses_props && renderer.invalidate('$$props', x`$$props = @assign(@assign({}, $$props), @exclude_internal_props($$new_props))`)}
${uses_rest && !uses_props && x`$$props = @assign(@assign({}, $$props), @exclude_internal_props($$new_props))`}
${uses_rest && renderer.invalidate('$$restProps', x`$$restProps = ${compute_rest}`)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ export default class InlineComponentWrapper extends Wrapper {
${name} = null;
}
} else if (${switch_value}) {
${updates.length && b`${name}.$set(${name_changes});`}
${updates.length > 0 && b`${name}.$set(${name_changes});`}
}
`);

Expand Down
4 changes: 4 additions & 0 deletions src/runtime/internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export function not_equal(a, b) {
return a != a ? b == b : a !== b;
}

export function is_empty(obj) {
return Object.keys(obj).length === 0;
}

export function validate_store(store, name) {
if (store != null && typeof store.subscribe !== 'function') {
throw new Error(`'${name}' is not a store with a 'subscribe' method`);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
export let id;
export let callback;
$: $$props, callback(id);
</script>
30 changes: 30 additions & 0 deletions test/runtime/samples/props-reactive-only-with-change/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
let callbacks = [];

export default {
props: {
callback: (value) => callbacks.push(value),
val1: "1",
val2: "2",
},

before_test() {
callbacks = [];
},

async test({ assert, component, target }) {
assert.equal(callbacks.length, 2);
assert.equal(JSON.stringify(callbacks), '["1","2"]');

component.val1 = "3";
assert.equal(callbacks.length, 3);
assert.equal(JSON.stringify(callbacks), '["1","2","1"]');

component.val1 = "4";
assert.equal(callbacks.length, 4);
assert.equal(JSON.stringify(callbacks), '["1","2","1","1"]');

component.val2 = "5";
assert.equal(callbacks.length, 5);
assert.equal(JSON.stringify(callbacks), '["1","2","1","1","2"]');
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
import Comp from './Comp.svelte';
export let callback;
export let val1, val2;
</script>

<Comp id="1" {callback} value={val1} />
<Comp id="2" {callback} value={val2} />

0 comments on commit 52863a6

Please sign in to comment.