Skip to content

fix: ensure computed props are cached with derived #9757

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 1 commit into from
Dec 4, 2023
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
5 changes: 5 additions & 0 deletions .changeset/selfish-tools-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure computed props are cached with derived
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,18 @@ function serialize_inline_component(node, component_name, context) {
const [, value] = serialize_attribute_value(attribute.value, context);

if (attribute.metadata.dynamic) {
push_prop(b.get(attribute.name, [b.return(value)]));
const contains_call_expression =
Array.isArray(attribute.value) &&
attribute.value.some((n) => {
return n.type === 'ExpressionTag' && n.metadata.contains_call_expression;
});
if (contains_call_expression) {
const id = b.id(context.state.scope.generate(attribute.name));
context.state.init.push(b.var(id, b.call('$.derived', b.thunk(value))));
push_prop(b.get(attribute.name, [b.return(b.call('$.get', id))]));
} else {
push_prop(b.get(attribute.name, [b.return(value)]));
}
} else {
push_prop(b.init(attribute.name, value));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
let { random } = $props();
</script>

<p>{random}</p>
<p>{random}</p>
<p>{random}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { test } from '../../test';

let math_random = Math.random;
let calls = 0;

export default test({
skip_if_hydrate: 'permanent',
before_test() {
Math.random = function () {
calls++;
return math_random.call(this);
};
},
after_test() {
Math.random = math_random;
calls = 0;
},
test({ assert }) {
assert.equal(calls, 1);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import Child from './Child.svelte';
</script>

<Child random={Math.random().toFixed(2)} />