Skip to content

Commit

Permalink
make autosubscribing to a nullish store a no-op (#4304)
Browse files Browse the repository at this point in the history
* make autosubscribing to a nullish store a no-op (#2181)

* update changelog

* add test
  • Loading branch information
Conduitry authored and Rich-Harris committed Jan 23, 2020
1 parent fd9b23e commit f01bb63
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 4 additions & 1 deletion src/runtime/internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 13 additions & 0 deletions test/runtime/samples/store-auto-subscribe-nullish/_config.js
Original file line number Diff line number Diff line change
@@ -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>
`);
}
};
5 changes: 5 additions & 0 deletions test/runtime/samples/store-auto-subscribe-nullish/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let store;
</script>

<p>{$store}</p>

0 comments on commit f01bb63

Please sign in to comment.