From d24454038490e17126b18cbf43dbe707f05a7142 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Tue, 4 May 2021 09:06:46 +0700 Subject: [PATCH 01/10] set writable value to optional --- src/runtime/store/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/store/index.ts b/src/runtime/store/index.ts index cf15db5250b9..985f8c792d4b 100644 --- a/src/runtime/store/index.ts +++ b/src/runtime/store/index.ts @@ -61,7 +61,7 @@ export function readable(value: T, start: StartStopNotifier): Readable * @param {*=}value initial value * @param {StartStopNotifier=}start start and stop notifications for subscriptions */ -export function writable(value: T, start: StartStopNotifier = noop): Writable { +export function writable(value?: T, start: StartStopNotifier = noop): Writable { let stop: Unsubscriber; const subscribers: Array> = []; From cab4ae267ffca5dca88ea026d1a62765b53f497a Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Tue, 4 May 2021 10:46:51 +0700 Subject: [PATCH 02/10] add readable overloads for empty initializer --- src/runtime/store/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/runtime/store/index.ts b/src/runtime/store/index.ts index 985f8c792d4b..96daf11249c7 100644 --- a/src/runtime/store/index.ts +++ b/src/runtime/store/index.ts @@ -50,7 +50,9 @@ const subscriber_queue = []; * @param value initial value * @param {StartStopNotifier}start start and stop notifications for subscriptions */ -export function readable(value: T, start: StartStopNotifier): Readable { +export function readable(): Readable; +export function readable(value: T, start: StartStopNotifier): Readable; +export function readable(value?: T, start?: StartStopNotifier): Readable { return { subscribe: writable(value, start).subscribe }; From 714b97a0a2a0184d18668fa332a82854637e906f Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Tue, 4 May 2021 10:53:08 +0700 Subject: [PATCH 03/10] update and clarify docs --- site/content/docs/03-run-time.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/site/content/docs/03-run-time.md b/site/content/docs/03-run-time.md index 7fbc8850de86..9da9dcf486dd 100644 --- a/site/content/docs/03-run-time.md +++ b/site/content/docs/03-run-time.md @@ -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, callback?: (set: (value: any) => void) => () => void) ``` --- @@ -297,7 +297,7 @@ unsubscribe(); // logs 'no more subscribers' #### `readable` ```js -store = readable(value: any, (set: (value: any) => void) => () => void) +store = readable(value: any, callback: (set: (value: any) => void) => () => void) ``` --- From 77ea546017689883b79cc7d301ef20615aa6e481 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Thu, 20 May 2021 17:07:10 +0700 Subject: [PATCH 04/10] make readable start callback optional --- site/content/docs/03-run-time.md | 4 ++-- src/runtime/store/index.ts | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/site/content/docs/03-run-time.md b/site/content/docs/03-run-time.md index 9da9dcf486dd..c846ca03954b 100644 --- a/site/content/docs/03-run-time.md +++ b/site/content/docs/03-run-time.md @@ -248,7 +248,7 @@ This makes it possible to wrap almost any other reactive state handling library store = writable(value?: any) ``` ```js -store = writable(value?: any, callback?: (set: (value: any) => void) => () => void) +store = writable(value?: any, start?: (set: (value: any) => void) => () => void) ``` --- @@ -297,7 +297,7 @@ unsubscribe(); // logs 'no more subscribers' #### `readable` ```js -store = readable(value: any, callback: (set: (value: any) => void) => () => void) +store = readable(value?: any, start?: (set: (value: any) => void) => () => void) ``` --- diff --git a/src/runtime/store/index.ts b/src/runtime/store/index.ts index 96daf11249c7..213a4ff6fdc6 100644 --- a/src/runtime/store/index.ts +++ b/src/runtime/store/index.ts @@ -50,8 +50,6 @@ const subscriber_queue = []; * @param value initial value * @param {StartStopNotifier}start start and stop notifications for subscriptions */ -export function readable(): Readable; -export function readable(value: T, start: StartStopNotifier): Readable; export function readable(value?: T, start?: StartStopNotifier): Readable { return { subscribe: writable(value, start).subscribe From f6fa3a9922cce49e55d2c12a65e768dab65601b2 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Thu, 20 May 2021 17:29:00 +0700 Subject: [PATCH 05/10] remove mention of required for readable --- site/content/docs/03-run-time.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/site/content/docs/03-run-time.md b/site/content/docs/03-run-time.md index c846ca03954b..c0bd14531610 100644 --- a/site/content/docs/03-run-time.md +++ b/site/content/docs/03-run-time.md @@ -302,9 +302,7 @@ 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'; From bc81f082a570e2558a4a5f566ae866a177475c27 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Thu, 20 May 2021 19:26:47 +0700 Subject: [PATCH 06/10] rename store/index.js -> store/index.ts --- test/store/{index.js => index.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/store/{index.js => index.ts} (100%) diff --git a/test/store/index.js b/test/store/index.ts similarity index 100% rename from test/store/index.js rename to test/store/index.ts From 38680ddfe1db139cf35224b6f0356c8e562def22 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Thu, 20 May 2021 19:36:09 +0700 Subject: [PATCH 07/10] test: undefined writable store --- test/store/index.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/store/index.ts b/test/store/index.ts index 2af4a6f35d85..bd6bcd803e1e 100644 --- a/test/store/index.ts +++ b/test/store/index.ts @@ -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; From 2392701833e6150f25b36764e0e20f3273801222 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Thu, 20 May 2021 19:37:30 +0700 Subject: [PATCH 08/10] test: undefined readable store --- test/store/index.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/store/index.ts b/test/store/index.ts index bd6bcd803e1e..35d6e6344b17 100644 --- a/test/store/index.ts +++ b/test/store/index.ts @@ -127,6 +127,19 @@ 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]); + }); }); const fake_observable = { From 2b491284fd9c727b6fbb8018b02cfeeb1167a870 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Thu, 20 May 2021 19:39:13 +0700 Subject: [PATCH 09/10] test: readable without updater --- test/store/index.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/store/index.ts b/test/store/index.ts index 35d6e6344b17..b6fc5940e111 100644 --- a/test/store/index.ts +++ b/test/store/index.ts @@ -140,6 +140,19 @@ describe('store', () => { 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 = { From 6e5103b25bb86527677f909bb4ea3c0831ba6dd6 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Thu, 20 May 2021 15:29:43 +0200 Subject: [PATCH 10/10] update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 141d5661bc39..89249634bbd2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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))