Skip to content

invalidate $$props or $$restProps only when there's changes #5123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

* Fix reactivity when passing `$$props` to a `<slot>` ([#3364](https://github.com/sveltejs/svelte/issues/3364))
* Fix unneeded invalidation of `$$props` and `$$restProps` ([#4993](https://github.com/sveltejs/svelte/issues/4993), [#5118](https://github.com/sveltejs/svelte/issues/5118))

## 3.24.0

Expand Down
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} />