Skip to content
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

fix: allow writable/readable empty initialization #6293

Merged
merged 10 commits into from
May 20, 2021
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Svelte changelog

## Unreleased

* Fix type signatures of `writable` and `readable`. It's possible to invoke them without arguments ([#6291](https://github.com/sveltejs/svelte/issues/6291), [#6345](https://github.com/sveltejs/svelte/issues/6345))

## 3.38.2

* Revert hydration optimisation for the time being ([#6279](https://github.com/sveltejs/svelte/issues/6279))
Expand Down
10 changes: 4 additions & 6 deletions site/content/docs/03-run-time.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ This makes it possible to wrap almost any other reactive state handling library
#### `writable`

```js
store = writable(value: any)
store = writable(value?: any)
```
```js
store = writable(value: any, (set: (value: any) => void) => () => void)
store = writable(value?: any, start?: (set: (value: any) => void) => () => void)
```

---
Expand Down Expand Up @@ -297,14 +297,12 @@ unsubscribe(); // logs 'no more subscribers'
#### `readable`

```js
store = readable(value: any, (set: (value: any) => void) => () => void)
store = readable(value?: any, start?: (set: (value: any) => void) => () => void)
```

---

Creates a store whose value cannot be set from 'outside', the first argument is the store's initial value.

The second argument to `readable` is the same as the second argument to `writable`, except that it is required with `readable` (since otherwise there would be no way to update the store value).
Creates a store whose value cannot be set from 'outside', the first argument is the store's initial value, and the second argument to `readable` is the same as the second argument to `writable`.

```js
import { readable } from 'svelte/store';
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const subscriber_queue = [];
* @param value initial value
* @param {StartStopNotifier}start start and stop notifications for subscriptions
*/
export function readable<T>(value: T, start: StartStopNotifier<T>): Readable<T> {
export function readable<T>(value?: T, start?: StartStopNotifier<T>): Readable<T> {
return {
subscribe: writable(value, start).subscribe
};
Expand All @@ -61,7 +61,7 @@ export function readable<T>(value: T, start: StartStopNotifier<T>): Readable<T>
* @param {*=}value initial value
* @param {StartStopNotifier=}start start and stop notifications for subscriptions
*/
export function writable<T>(value: T, start: StartStopNotifier<T> = noop): Writable<T> {
export function writable<T>(value?: T, start: StartStopNotifier<T> = noop): Writable<T> {
let stop: Unsubscriber;
const subscribers: Array<SubscribeInvalidateTuple<T>> = [];

Expand Down
39 changes: 39 additions & 0 deletions test/store/index.js → test/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ describe('store', () => {
assert.deepEqual(values, [0, 1, 2]);
});

it('creates an undefined writable store', () => {
const store = writable();
const values = [];

const unsubscribe = store.subscribe(value => {
values.push(value);
});

unsubscribe();

assert.deepEqual(values, [undefined]);
});

it('calls provided subscribe handler', () => {
let called = 0;

Expand Down Expand Up @@ -114,6 +127,32 @@ describe('store', () => {

assert.deepEqual(values, [0, 1, 2]);
});

it('creates an undefined readable store', () => {
const store = readable();
const values = [];

const unsubscribe = store.subscribe(value => {
values.push(value);
});

unsubscribe();

assert.deepEqual(values, [undefined]);
});

it('creates a readable store without updater', () => {
const store = readable(100);
const values = [];

const unsubscribe = store.subscribe(value => {
values.push(value);
});

unsubscribe();

assert.deepEqual(values, [100]);
});
});

const fake_observable = {
Expand Down