Skip to content

A store does not notify its modification to subscribers #13514

@osamutake

Description

@osamutake

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:

  1. Follow the link below.
  2. Look at the "Console" panel to see the subscription to the store variable is done.
  3. Click the button in the top row labeled "Runed" several times to see all the numbers EXCEPT FOR the one labeled "Subscribed" increases.
  4. 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.
  5. Click the button in the second row labeled "Store" once to see all the numbers INCLUDING the one labeled "Subscribed" increases.
  6. Click the button in the top row labeled "Runed" several times to see all the numbers now INCLUDING the one labeled "Subscribed" increases.
  7. 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

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