Skip to content

Commit 4756954

Browse files
committed
invalidate $$props or $$restProps only when there's changes
1 parent d19bcef commit 4756954

File tree

6 files changed

+50
-1
lines changed

6 files changed

+50
-1
lines changed

src/compiler/compile/render_dom/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export default function dom(
8686
const set = (uses_props || uses_rest || writable_props.length > 0 || component.slots.size > 0)
8787
? x`
8888
${$$props} => {
89+
${(uses_props || uses_rest) && b`if (@is_empty(${$$props})) return;`}
8990
${uses_props && renderer.invalidate('$$props', x`$$props = @assign(@assign({}, $$props), @exclude_internal_props($$new_props))`)}
9091
${uses_rest && !uses_props && x`$$props = @assign(@assign({}, $$props), @exclude_internal_props($$new_props))`}
9192
${uses_rest && renderer.invalidate('$$restProps', x`$$restProps = ${compute_rest}`)}

src/compiler/compile/render_dom/wrappers/InlineComponent/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ export default class InlineComponentWrapper extends Wrapper {
468468
${name} = null;
469469
}
470470
} else if (${switch_value}) {
471-
${updates.length && b`${name}.$set(${name_changes});`}
471+
${updates.length > 0 && b`${name}.$set(${name_changes});`}
472472
}
473473
`);
474474

src/runtime/internal/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export function not_equal(a, b) {
4242
return a != a ? b == b : a !== b;
4343
}
4444

45+
export function is_empty(obj) {
46+
return Object.keys(obj).length === 0;
47+
}
48+
4549
export function validate_store(store, name) {
4650
if (store != null && typeof store.subscribe !== 'function') {
4751
throw new Error(`'${name}' is not a store with a 'subscribe' method`);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
export let id;
3+
export let callback;
4+
5+
$: $$props, callback(id);
6+
</script>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
let callbacks = [];
2+
3+
export default {
4+
props: {
5+
callback: (value) => callbacks.push(value),
6+
val1: "1",
7+
val2: "2",
8+
},
9+
10+
before_test() {
11+
callbacks = [];
12+
},
13+
14+
async test({ assert, component, target }) {
15+
assert.equal(callbacks.length, 2);
16+
assert.equal(JSON.stringify(callbacks), '["1","2"]');
17+
18+
component.val1 = "3";
19+
assert.equal(callbacks.length, 3);
20+
assert.equal(JSON.stringify(callbacks), '["1","2","1"]');
21+
22+
component.val1 = "4";
23+
assert.equal(callbacks.length, 4);
24+
assert.equal(JSON.stringify(callbacks), '["1","2","1","1"]');
25+
26+
component.val2 = "5";
27+
assert.equal(callbacks.length, 5);
28+
assert.equal(JSON.stringify(callbacks), '["1","2","1","1","2"]');
29+
},
30+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
import Comp from './Comp.svelte';
3+
export let callback;
4+
export let val1, val2;
5+
</script>
6+
7+
<Comp id="1" {callback} value={val1} />
8+
<Comp id="2" {callback} value={val2} />

0 commit comments

Comments
 (0)