Skip to content

Commit

Permalink
fix types: allow writable/readable empty initialization (#6293)
Browse files Browse the repository at this point in the history
Fixes #6291
Fixes #6345

Both writable and readable initialized without any arguments are already valid, but TS complains about it. This makes both allowed to be emptily initialized. It's also possible to invoke readable with one argument only.
  • Loading branch information
ignatiusmb authored May 20, 2021
1 parent 3301b09 commit b295d68
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
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

0 comments on commit b295d68

Please sign in to comment.