fix: keep creating state sources for property checks during component init (#18949)

main
Serghei 4 days ago committed by GitHub
parent c85120fb9f
commit 707c28146b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: keep creating state sources for property checks during component init

@ -2,6 +2,7 @@
import { DEV } from 'esm-env'; import { DEV } from 'esm-env';
import { import {
get, get,
active_effect,
update_version, update_version,
active_reaction, active_reaction,
set_update_version, set_update_version,
@ -240,7 +241,8 @@ export function proxy(value) {
if ( if (
s !== undefined || s !== undefined ||
(active_reaction !== null && (!has || get_descriptor(target, prop)?.writable)) ((active_effect !== null || active_reaction !== null) &&
(!has || get_descriptor(target, prop)?.writable))
) { ) {
if (s === undefined) { if (s === undefined) {
s = with_parent(() => { s = with_parent(() => {

@ -0,0 +1,18 @@
import { tick } from 'svelte';
import { test } from '../../test';
export default test({
async test({ assert, target, logs }) {
await tick();
const [rerun, discard] = target.querySelectorAll('button');
rerun.click();
await tick();
discard.click();
await tick();
assert.deepEqual(logs, [1, false, 1, 1, 1]);
}
});

@ -0,0 +1,49 @@
<script>
import { fork } from 'svelte';
const init = $state({ x: 1 });
const init_missing = $state({});
const root = $state({ x: 1 });
const teardown = $state({ x: 1 });
const after_await = $state({ x: 1 });
let count = $state(0);
// each object is first checked with `in` while an effect is active but no reaction is
'x' in init;
'y' in init_missing;
$effect.root(() => {
'x' in root;
});
$effect(() => {
count;
return () => {
'x' in teardown;
};
});
await Promise.resolve();
'x' in after_await;
</script>
<button onclick={() => count++}>rerun</button>
<button
onclick={() => {
fork(() => {
init.x = 2;
init_missing.y = 1;
root.x = 2;
teardown.x = 2;
after_await.x = 2;
}).discard();
console.log(init.x, 'y' in init_missing, root.x, teardown.x, after_await.x);
}}
>
discard
</button>
Loading…
Cancel
Save