From 88ef883f6ac41abc0339456298bca3234382e50d Mon Sep 17 00:00:00 2001 From: Ramon Snir Date: Fri, 14 Oct 2022 18:22:11 +0000 Subject: [PATCH] [fix] allow $store to be used with changing values including nullish values --- src/runtime/internal/utils.ts | 3 +++ .../_config.js | 21 +++++++++++++++++++ .../main.svelte | 5 +++++ 3 files changed, 29 insertions(+) create mode 100644 test/runtime/samples/store-auto-subscribe-removed-store/_config.js create mode 100644 test/runtime/samples/store-auto-subscribe-removed-store/main.svelte diff --git a/src/runtime/internal/utils.ts b/src/runtime/internal/utils.ts index 8868e38ee2..c9e37eef37 100644 --- a/src/runtime/internal/utils.ts +++ b/src/runtime/internal/utils.ts @@ -66,6 +66,9 @@ export function validate_store(store, name) { export function subscribe(store, ...callbacks) { if (store == null) { + for (const callback of callbacks) { + callback(store); + } return noop; } const unsub = store.subscribe(...callbacks); diff --git a/test/runtime/samples/store-auto-subscribe-removed-store/_config.js b/test/runtime/samples/store-auto-subscribe-removed-store/_config.js new file mode 100644 index 0000000000..12297a3b8a --- /dev/null +++ b/test/runtime/samples/store-auto-subscribe-removed-store/_config.js @@ -0,0 +1,21 @@ +import { writable } from '../../../../store'; + +export default { + html: ` +

undefined

+ `, + async test({ assert, component, target }) { + component.store = writable('foo'); + assert.htmlEqual(target.innerHTML, ` +

foo

+ `); + component.store = undefined; + assert.htmlEqual(target.innerHTML, ` +

undefined

+ `); + component.store = writable('bar'); + assert.htmlEqual(target.innerHTML, ` +

bar

+ `); + } +}; diff --git a/test/runtime/samples/store-auto-subscribe-removed-store/main.svelte b/test/runtime/samples/store-auto-subscribe-removed-store/main.svelte new file mode 100644 index 0000000000..9c1ed4a560 --- /dev/null +++ b/test/runtime/samples/store-auto-subscribe-removed-store/main.svelte @@ -0,0 +1,5 @@ + + +

{$store}