-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Closed
Description
Describe the bug
On svelte 5 environment, I wanted to have cooperation of a writable store and a runed variable.
So, I tried this code on https://svelte-5-preview.vercel.app/:
<script lang="ts">
import { writable } from 'svelte/store';
import { onMount } from 'svelte';
let store = writable({ count: 0 });
let runed = $state($store);
let updating = false;
onMount(() => {
return store.subscribe(($store) => {
if (!updating) {
updating = true;
runed = $store;
updating = false;
}
});
});
$effect.pre(() => {
if (!updating) {
updating = true;
$store = runed;
updating = false;
}
});
let subscribed = $state(0);
onMount(() => {
console.log('subscribe');
const unsub = store.subscribe(($store) => {
console.log('updating');
subscribed = $store.count;
});
return ()=> {
console.log('unsubscribe');
unsub();
}
});
</script>
<p>
Runed:
<input bind:value={runed.count} id="runed" />
<button onclick={() => (runed.count = Number(runed.count) + 1)}>
clicks {runed.count}
</button>
</p>
<p>
Store:
<input bind:value={$store.count} id="store" />
<button onclick={() => ($store = { count: Number($store.count) + 1 })}>
clicks {$store.count}
</button>
</p>
<p id="subscribed">
Subscribed: {subscribed}
</p>It works almost perfectly.
Two buttons and two input fields nicely follows the change of the value.
However, the "subscribed" variable is not notified the change until the controls bound to the store variable is changed.
Try to see it with following this instruction:
- Follow the link below.
- Look at the "Console" panel to see the subscription to the store variable is done.
- Click the button in the top row labeled "Runed" several times to see all the numbers EXCEPT FOR the one labeled "Subscribed" increases.
- Look at the "Console" panel to see "updating" message is not shown when the button is clicked, i.e. change of the store value is not notified to the subscribers.
- Click the button in the second row labeled "Store" once to see all the numbers INCLUDING the one labeled "Subscribed" increases.
- Click the button in the top row labeled "Runed" several times to see all the numbers now INCLUDING the one labeled "Subscribed" increases.
- Look at the "Console" panel to see the subscription to the store variable is not changed at all but now "updating" messages appear whenever the value is changed.
I doubt this to be an expected behavior.
Reproduction
Follow this link to try the above code.
Logs
No response
System Info
It was tested on `https://svelte-5-preview.vercel.app/` on 2024-10-06.Severity
blocking an upgrade
Metadata
Metadata
Assignees
Labels
No labels