Skip to content

Stores not honoring contract described in the documentation #15984

@mbellgardt

Description

@mbellgardt

Describe the bug

In https://svelte.dev/docs/svelte/stores#Store-contract the documentations states:

A store may optionally contain a .set method, which [...] synchronously calls all of the store’s active subscription functions.

This is not the behavior I observe from Svelte's built-in stores, as they seem to schedule the calls to the subscriptions till after the calling function is done. I understand that this behavior is probably intended, my main concern is with the documentation.

It would be very helpful to have a proper documentation of how Svelte's stores actually behave.

Reproduction

Consider the following example:

<script lang="ts">
	import { readonly, type Writable, writable } from 'svelte/store'
	let name = 'world';

	const select: Writable<number> = writable(0)
	const select2: Writable<number> = writable($select)
	let i = 0

	$: console.log("select ", $select)
	$: console.log("select2 ", $select2)

	select.subscribe((v) => {
		console.log('select subscribe ', v)
		select2.set(v)
		console.log('after set')
	})

	select2.subscribe((v) => {
		console.log('select 2 subscribe ', v)
		select.set(v)
	})
</script>

<button on:click={() => select.set(++i)}>assign</button>

Which (after clicking the button) results in the output:

select subscribe  1
after set
select 2 subscribe  1
select  1
select2  1

As you can see, the .set method finishes and the surrounding code is executed before the subscriptions are called.

Logs

System Info

System:
    OS: Windows 11 10.0.22621
    CPU: (16) x64 AMD Ryzen 7 PRO 4750U with Radeon Graphics
    Memory: 12.33 GB / 31.37 GB
  Binaries:
    Node: 22.14.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.9.2 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (127.0.2651.74)
    Internet Explorer: 11.0.22621.3527

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions