Skip to content

Commit 435a9c1

Browse files
trueadmdummdidumm
andauthored
fix: ensure toStore subscription correctly syncs latest value (#14015)
* fix: ensure toStore subscription correctly syncs latest value * Update packages/svelte/src/store/index-client.js --------- Co-authored-by: Simon H <[email protected]>
1 parent 0106204 commit 435a9c1

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

.changeset/shaggy-frogs-fetch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: ensure toStore subscription correctly syncs latest value

packages/svelte/src/store/index-client.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ export { derived, get, readable, readonly, writable } from './shared/index.js';
4242
* @returns {Writable<V> | Readable<V>}
4343
*/
4444
export function toStore(get, set) {
45-
const store = writable(get(), (set) => {
46-
let ran = false;
45+
let init_value = get();
46+
const store = writable(init_value, (set) => {
47+
// If the value has changed before we call subscribe, then
48+
// we need to treat the value as already having run
49+
let ran = init_value !== get();
4750

4851
// TODO do we need a different implementation on the server?
4952
const teardown = effect_root(() => {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
html: `1`,
5+
mode: ['client', 'hydrate']
6+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script>
2+
import { toStore } from 'svelte/store';
3+
4+
let count = $state(0);
5+
6+
const store = toStore(
7+
() => count,
8+
(v) => (count = v)
9+
);
10+
11+
store.set(1);
12+
</script>
13+
14+
{$store}

0 commit comments

Comments
 (0)