Skip to content

When a store is replaced with undefined/null, state does not update #7946

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

Closed
ramonsnir opened this issue Oct 14, 2022 · 1 comment · Fixed by #7947
Closed

When a store is replaced with undefined/null, state does not update #7946

ramonsnir opened this issue Oct 14, 2022 · 1 comment · Fixed by #7947

Comments

@ramonsnir
Copy link
Contributor

Describe the bug

I'll preface this by saying that reaching the bug may seem an anti-pattern, but this usage is permissible by Svelte's TypeScript output. Given that the type definitions allow this and it works in most cases, I believe this bug should be fixed. The type definitions should not change as that will break backward compatibility in a very aggressive manner.

When using the $store syntax, Svelte accepts either a store-like object or null/undefined. When using the $store syntax, the store may change between two or more stores, and then the runtime value of $store will match the current value of the current store. This does not work when switching between a store-like object and undefined.

<script>
	import { writable } from 'svelte/store';
	let store = writable('world');
	setTimeout(() => { store = undefined }, 1000);
</script>
<h1>Hello {$store}!</h1>

After the assignment, the value of $store does not update to be undefined. This happens because the subscribe utility doesn't call the store callbacks if the store isn't defined (despite the store contract requiring stores to call their callback immediately on subscribe).

I intend to open a PR for this.

Again: It may seem like this is customer misuse and the example should change to be setTimeout(() => { $store = undefined }, 1000);, but the example I shared is just an MCVE that shows the problem and this is permitted by the TypeScript type definitions (unlike, say store = 4 which isn't permitted).

If the user is aware of the issue, the user may try to do store = writable(undefined) which will work. If the user isn't aware, the user will encounter the bug.

Reproduction

Non-working REPL:

https://svelte.dev/repl/a335c8e6d976472d8c6e544c4c89c3cb?version=3.51.0

Working REPL:

https://svelte.dev/repl/aaa88e5d377a457cb7b14dad08491a84?version=3.51.0

Logs

No response

System Info

System:
    OS: Linux 5.15 Ubuntu 20.04.5 LTS (Focal Fossa)
    CPU: (16) x64 AMD EPYC 7B13
    Memory: 28.05 GB / 62.80 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 16.17.1 - ~/.nvm/versions/node/v16.17.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.17.1/bin/yarn
    npm: 8.19.2 - ~/.nvm/versions/node/v16.17.1/bin/npm
  npmPackages:
    rollup: ^2.77.0 => 2.77.0 
    svelte: ^3.50.1 => 3.50.1

Severity

annoyance

@Conduitry
Copy link
Member

Duplicate of #7555.

@Conduitry Conduitry closed this as not planned Won't fix, can't repro, duplicate, stale Oct 14, 2022
dummdidumm added a commit to ramonsnir/svelte that referenced this issue Apr 28, 2023
dummdidumm added a commit to ramonsnir/svelte that referenced this issue May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants