diff --git a/src/runtime/internal/utils.ts b/src/runtime/internal/utils.ts index 8868e38ee29f..c9e37eef3783 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 000000000000..12297a3b8a77 --- /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 000000000000..9c1ed4a56094 --- /dev/null +++ b/test/runtime/samples/store-auto-subscribe-removed-store/main.svelte @@ -0,0 +1,5 @@ + + +

{$store}