fix: ensure $state proxy invokes set accessor if present (#12503)

pull/12504/head
Dominic Gannaway 5 months ago committed by GitHub
parent 2ce2b7d98b
commit 20e6508c47
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: ensure $state proxy invokes set accessor if present

@ -243,9 +243,14 @@ const state_proxy_handler = {
}
}
var descriptor = Reflect.getOwnPropertyDescriptor(target, prop);
// Set the new value before updating any signals so that any listeners get the new value
// @ts-ignore
target[prop] = value;
if (descriptor?.set) {
descriptor.set.call(receiver, value);
} else {
target[prop] = value;
}
if (not_has) {
// If we have mutated an array directly, we might need to

@ -0,0 +1,22 @@
import { flushSync } from 'svelte';
import { test, ok } from '../../test';
export default test({
html: `<input><p>svelte</p>`,
ssrHtml: `<input value="SVELTE"><p>svelte</p>`,
test({ assert, target }) {
const input = target.querySelector('input');
ok(input);
const event = new window.Event('input');
input.value = 'SVELTEy';
input.dispatchEvent(event);
flushSync();
assert.htmlEqual(target.innerHTML, `<input><p>sveltey</p>`);
assert.equal(input.value, 'SVELTEY');
}
});

@ -0,0 +1,14 @@
<script>
let text = $state({
value: 'svelte',
get uppercase() {
return this.value.toUpperCase()
},
set uppercase(v) {
this.value = v.toLowerCase()
}
})
</script>
<input bind:value={text.uppercase} />
<p>{text.value}</p>
Loading…
Cancel
Save