fix: don't set state withing `with_parent` in proxy (#16176)

Closes #16164

We can't set everywhere within with_parent otherwise if it's the first time we are reading a derived it could look like we are setting state in a derived (which you are not).
elliott/13891
Paolo Ricciuti 3 months ago committed by GitHub
parent 931f211b25
commit 7e588857c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: don't set state withing `with_parent` in proxy

@ -93,21 +93,19 @@ export function proxy(value) {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/getOwnPropertyDescriptor#invariants
e.state_descriptors_fixed();
}
with_parent(() => {
var s = sources.get(prop);
if (s === undefined) {
s = source(descriptor.value, stack);
var s = sources.get(prop);
if (s === undefined) {
s = with_parent(() => {
var s = source(descriptor.value, stack);
sources.set(prop, s);
if (DEV && typeof prop === 'string') {
tag(s, get_label(path, prop));
}
} else {
set(s, descriptor.value, true);
}
});
return s;
});
} else {
set(s, descriptor.value, true);
}
return true;
},
@ -268,11 +266,8 @@ export function proxy(value) {
// object property before writing to that property.
if (s === undefined) {
if (!has || get_descriptor(target, prop)?.writable) {
s = with_parent(() => {
var s = source(undefined, stack);
set(s, proxy(value));
return s;
});
s = with_parent(() => source(undefined, stack));
set(s, proxy(value));
sources.set(prop, s);

@ -0,0 +1,5 @@
import { test } from '../../test';
export default test({
async test() {}
});

@ -0,0 +1,15 @@
<script>
function with_writes(initialState) {
const derive = $state(initialState)
return derive
}
let data = $state({ example: 'Example' })
let my_derived = $derived(with_writes({ example: data.example }))
$effect(() => {
my_derived.example = 'Bar'
});
</script>
<input bind:value={data.example} />
Loading…
Cancel
Save