Skip to content

Commit

Permalink
fix: make prop fallback values deeply reactive if needed (sveltejs#11804
Browse files Browse the repository at this point in the history
)

If a property is mutated, the assumption is that it is deeply reactive. In those cases, the fallback value should be proxified so that it also is deeply reactive.
fixes sveltejs#11425
  • Loading branch information
dummdidumm committed May 28, 2024
1 parent ba6697d commit 68263c8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-roses-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: make prop fallback values deeply reactive if needed
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,14 @@ export const javascript_visitors_runes = {
property.value.type === 'AssignmentPattern' ? property.value.left : property.value;
assert.equal(id.type, 'Identifier');
const binding = /** @type {import('#compiler').Binding} */ (state.scope.get(id.name));
const initial =
let initial =
binding.initial &&
/** @type {import('estree').Expression} */ (visit(binding.initial));
// We're adding proxy here on demand and not within the prop runtime function so that
// people not using proxied state anywhere in their code don't have to pay the additional bundle size cost
if (initial && binding.mutated && should_proxy_or_freeze(initial, state.scope)) {
initial = b.call('$.proxy', initial);
}

if (binding.reassigned || state.analysis.accessors || initial) {
declarations.push(b.declarator(id, get_prop_source(binding, state, name, initial)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export default test({
assert.htmlEqual(
target.innerHTML,
`
<button>mutate: 0</button>
<button>reassign: 0</button>
<button>mutate: 1</button>
<button>reassign: 1</button>
`
);

Expand Down

0 comments on commit 68263c8

Please sign in to comment.