make autosubscribing to a nullish store a no-op (#4304)

* make autosubscribing to a nullish store a no-op (#2181)

* update changelog

* add test
pull/4316/head
Conduitry 5 years ago committed by Rich Harris
parent fd9b23e5e4
commit f01bb639be

@ -2,6 +2,7 @@
## 3.17.3
* Make autosubscribing to a nullish store a no-op ([#2181](https://github.com/sveltejs/svelte/issues/2181))
* Fix updating a `<slot>` inside an `{#if}` or other block ([#4292](https://github.com/sveltejs/svelte/issues/4292))
* Fix using RxJS observables in `derived` stores ([#4298](https://github.com/sveltejs/svelte/issues/4298))
* Add dev mode check to disallow duplicate keys in a keyed `{#each}` ([#4301](https://github.com/sveltejs/svelte/issues/4301))

@ -43,12 +43,15 @@ export function not_equal(a, b) {
}
export function validate_store(store, name) {
if (!store || typeof store.subscribe !== 'function') {
if (store != null && typeof store.subscribe !== 'function') {
throw new Error(`'${name}' is not a store with a 'subscribe' method`);
}
}
export function subscribe(store, ...callbacks) {
if (store == null) {
return noop;
}
const unsub = store.subscribe(...callbacks);
return unsub.unsubscribe ? () => unsub.unsubscribe() : unsub;
}

@ -0,0 +1,13 @@
import { writable } from '../../../../store';
export default {
html: `
<p>undefined</p>
`,
async test({ assert, component, target }) {
component.store = writable('foo');
assert.htmlEqual(target.innerHTML, `
<p>foo</p>
`);
}
};

@ -0,0 +1,5 @@
<script>
export let store;
</script>
<p>{$store}</p>
Loading…
Cancel
Save