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 <5968653+dummdidumm@users.noreply.github.com>
pull/14035/head
Dominic Gannaway 10 months ago committed by GitHub
parent 0106204982
commit 435a9c1cd0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: ensure toStore subscription correctly syncs latest value

@ -42,8 +42,11 @@ export { derived, get, readable, readonly, writable } from './shared/index.js';
* @returns {Writable<V> | Readable<V>}
*/
export function toStore(get, set) {
const store = writable(get(), (set) => {
let ran = false;
let init_value = get();
const store = writable(init_value, (set) => {
// If the value has changed before we call subscribe, then
// we need to treat the value as already having run
let ran = init_value !== get();
// TODO do we need a different implementation on the server?
const teardown = effect_root(() => {

@ -0,0 +1,6 @@
import { test } from '../../test';
export default test({
html: `1`,
mode: ['client', 'hydrate']
});

@ -0,0 +1,14 @@
<script>
import { toStore } from 'svelte/store';
let count = $state(0);
const store = toStore(
() => count,
(v) => (count = v)
);
store.set(1);
</script>
{$store}
Loading…
Cancel
Save