Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shaggy-frogs-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure toStore subscription correctly syncs latest value
5 changes: 4 additions & 1 deletion packages/svelte/src/store/index-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ export { derived, get, readable, readonly, writable } from './shared/index.js';
* @returns {Writable<V> | Readable<V>}
*/
export function toStore(get, set) {
let init_value = get();
const store = writable(get(), (set) => {
let ran = false;
// 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(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { test } from '../../test';

export default test({
html: `1`,
mode: ['client', 'hydrate']
});
Original file line number Diff line number Diff line change
@@ -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}