Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derived store is unsubscribed twice and started without any subscribers #8364

Closed
jrouleau opened this issue Mar 9, 2023 · 1 comment · Fixed by #8368
Closed

Derived store is unsubscribed twice and started without any subscribers #8364

jrouleau opened this issue Mar 9, 2023 · 1 comment · Fixed by #8368

Comments

@jrouleau
Copy link
Contributor

jrouleau commented Mar 9, 2023

Describe the bug

When a derived store is unsubscribed from another derived store sharing the same dependency, it is unsubscribed from twice and then started without any subscribers.

As far as I understand, here is a timeline of events for the following example:

  1. store3 is subscribed
  2. store3 subscribes to store1 as a dependency
  3. store1 is true, so store3 subscribes to store2
  4. store2 subscribes to store1 as a dependency
  5. store1 is set to false
  6. store3 receives update from store1 and unsubscribes from store2
  7. store2 receives update from store1, its stop function is run again, and then it is started without any subscribers

Reproduction

  import { derived, writable } from 'svelte/store';

  const store1 = writable(true);

  const store2 = derived([store1], ([$store1], set) => {
    console.log('store2 started');
    let unsubscribed = false;
    return () => {
      console.log('store2 stopped');
      if (unsubscribed) console.error('already unsubscribed!');
      unsubscribed = true;
    };
  });

  const store3 = derived([store1], ([$store1], set) => {
    if ($store1) return store2.subscribe(() => {});
  });

  store3.subscribe(() => {});
  store1.set(false);

Logs

store2 started
store2 stopped
store2 stopped
already unsubscribed!
store2 started

System Info

npmPackages:
    svelte: ^3.55.1 => 3.55.1

Severity

annoyance

dummdidumm added a commit that referenced this issue Mar 14, 2023
…th a shared ancestor (#8368)

Fixes #8364

---------

Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
@Conduitry
Copy link
Member

This should be fixed in 3.57.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants