Merge branch 'sveltejs:master' into tutorial-updates

pull/6376/head
Debargha Roy 5 years ago
commit 4ad50b8768

@ -1,5 +1,9 @@
# Svelte changelog # 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 ## 3.38.2
* Revert hydration optimisation for the time being ([#6279](https://github.com/sveltejs/svelte/issues/6279)) * Revert hydration optimisation for the time being ([#6279](https://github.com/sveltejs/svelte/issues/6279))

@ -245,10 +245,10 @@ This makes it possible to wrap almost any other reactive state handling library
#### `writable` #### `writable`
```js ```js
store = writable(value: any) store = writable(value?: any)
``` ```
```js ```js
store = writable(value: any, (set: (value: any) => void) => () => void) store = writable(value?: any, start?: (set: (value: any) => void) => () => void)
``` ```
--- ---
@ -297,14 +297,12 @@ unsubscribe(); // logs 'no more subscribers'
#### `readable` #### `readable`
```js ```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. 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`.
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).
```js ```js
import { readable } from 'svelte/store'; import { readable } from 'svelte/store';

@ -50,7 +50,7 @@ const subscriber_queue = [];
* @param value initial value * @param value initial value
* @param {StartStopNotifier}start start and stop notifications for subscriptions * @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 { return {
subscribe: writable(value, start).subscribe subscribe: writable(value, start).subscribe
}; };
@ -61,7 +61,7 @@ export function readable<T>(value: T, start: StartStopNotifier<T>): Readable<T>
* @param {*=}value initial value * @param {*=}value initial value
* @param {StartStopNotifier=}start start and stop notifications for subscriptions * @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; let stop: Unsubscriber;
const subscribers: Array<SubscribeInvalidateTuple<T>> = []; const subscribers: Array<SubscribeInvalidateTuple<T>> = [];

@ -22,6 +22,19 @@ describe('store', () => {
assert.deepEqual(values, [0, 1, 2]); 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', () => { it('calls provided subscribe handler', () => {
let called = 0; let called = 0;
@ -114,6 +127,32 @@ describe('store', () => {
assert.deepEqual(values, [0, 1, 2]); 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 = { const fake_observable = {
Loading…
Cancel
Save