Skip to content

Commit c084479

Browse files
committed
Add, document, and test new store implementation
1 parent 25e7880 commit c084479

File tree

8 files changed

+899
-328
lines changed

8 files changed

+899
-328
lines changed

documentation/docs/03-runtime/02-svelte-store.md

Lines changed: 116 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: 'svelte/store'
44

55
The `svelte/store` module exports functions for creating [readable](/docs/svelte-store#readable), [writable](/docs/svelte-store#writable) and [derived](/docs/svelte-store#derived) stores.
66

7-
Keep in mind that you don't _have_ to use these functions to enjoy the [reactive `$store` syntax](/docs/svelte-components#script-4-prefix-stores-with-$-to-access-their-values) in your components. Any object that correctly implements `.subscribe`, unsubscribe, and (optionally) `.set` is a valid store, and will work both with the special syntax, and with Svelte's built-in [`derived` stores](/docs/svelte-store#derived).
7+
Keep in mind that you don't _have_ to use these functions to enjoy the [reactive `$store` syntax](/docs/svelte-components#script-4-prefix-stores-with-$-to-access-their-values) in your components. Any object that correctly implements the `subscribe` (including returning unsubscribe functions) and (optionally) `set` methods is a valid store, and will work both with the special syntax and with Svelte's built-in [derived stores](/docs/svelte-store#derived).
88

99
This makes it possible to wrap almost any other reactive state handling library for use in Svelte. Read more about the [store contract](/docs/svelte-components#script-4-prefix-stores-with-$-to-access-their-values) to see what a correct implementation looks like.
1010

@@ -18,8 +18,7 @@ Function that creates a store which has values that can be set from 'outside' co
1818

1919
`update` is a method that takes one argument which is a callback. The callback takes the existing store value as its argument and returns the new value to be set to the store.
2020

21-
```js
22-
/// file: store.js
21+
```ts
2322
import { writable } from 'svelte/store';
2423

2524
const count = writable(0);
@@ -35,8 +34,7 @@ count.update((n) => n + 1); // logs '2'
3534

3635
If a function is passed as the second argument, it will be called when the number of subscribers goes from zero to one (but not from one to two, etc). That function will be passed a `set` function which changes the value of the store, and an `update` function which works like the `update` method on the store, taking a callback to calculate the store's new value from its old value. It must return a `stop` function that is called when the subscriber count goes from one to zero.
3736

38-
```js
39-
/// file: store.js
37+
```ts
4038
import { writable } from 'svelte/store';
4139

4240
const count = writable(0, () => {
@@ -59,27 +57,60 @@ Note that the value of a `writable` is lost when it is destroyed, for example wh
5957

6058
> EXPORT_SNIPPET: svelte/store#readable
6159
62-
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`.
60+
Creates a store which can be subscribed to, but does not have the `set` and `update` methods that a writable store has. The value of a readable store is instead set by the `initial_value` argument at creation and then updated internally by an `on_start` function. This function is called when the store receives its first subscriber.
61+
62+
The `on_start` function allows the creation of stores whose value changes automatically based on application-specific logic. It's passed `set` and `update` functions that behave like the methods available on writable stores.
63+
64+
The `on_start` function can optionally return an `on_stop` function which will be called when the store loses its last subscriber. This allows stores to go dormant when not being used by any other code.
6365

6466
```ts
6567
import { readable } from 'svelte/store';
6668

67-
const time = readable(new Date(), (set) => {
68-
set(new Date());
69+
const lastPressedSimpleKey = readable('', (set) => {
70+
const handleEvent = (event: KeyboardEvent) => {
71+
if (event.key.length === 1) {
72+
set(event.key);
73+
}
74+
};
6975

70-
const interval = setInterval(() => {
71-
set(new Date());
72-
}, 1000);
76+
window.addEventListener('keypress', handleEvent, { passive: true });
77+
78+
return function onStop() {
79+
window.removeEventListener('keypress', handleEvent);
80+
};
81+
});
7382

74-
return () => clearInterval(interval);
83+
lastPressedSimpleKey.subscribe((value) => {
84+
console.log(`Most recently pressed simple key is "${value}".`);
7585
});
86+
```
7687

77-
const ticktock = readable('tick', (set, update) => {
88+
`on_start` could also set up a timer to poll an API for data which changes frequently, or even establish a WebSocket connection. The following example creates a store which polls [Open Notify's public ISS position API](http://open-notify.org/Open-Notify-API/ISS-Location-Now/) every 3 seconds.
89+
90+
> If `set` or `update` are called after the store has lost its last subscriber, they will have no effect. You should still take care to clean up any asynchronous callbacks registered in `on_start` by providing a suitable `on_stop` function, but a few accidental late calls will not negatively affect the store.
91+
>
92+
> For instance, in the example below `set` may be called late if the `issPosition` store loses its last subscriber after a `fetch` call is made but before the corresponding HTTP response is received.
93+
94+
```ts
95+
import { readable } from 'svelte/store';
96+
97+
const issPosition = readable({ latitude: 0, longitude: 0 }, (set) => {
7898
const interval = setInterval(() => {
79-
update((sound) => (sound === 'tick' ? 'tock' : 'tick'));
80-
}, 1000);
99+
fetch('http://api.open-notify.org/iss-now.json')
100+
.then((response) => response.json())
101+
.then((payload) => set(payload.iss_position));
102+
}, 3000);
103+
104+
return function onStop() {
105+
clearInterval(interval);
106+
};
107+
});
81108

82-
return () => clearInterval(interval);
109+
issPosition.subscribe(({ latitude, longitude }) => {
110+
console.log(
111+
`The ISS is currently above ${latitude}°, ${longitude` +
112+
` in the ${latitude > 0 ? 'northern' : 'southern'} hemisphere.`
113+
);
83114
});
84115
```
85116

@@ -108,7 +139,9 @@ import { derived } from 'svelte/store';
108139
const doubled = derived(a, ($a) => $a * 2);
109140
```
110141

111-
The callback can set a value asynchronously by accepting a second argument, `set`, and an optional third argument, `update`, calling either or both of them when appropriate.
142+
The `derive_value` function can set values asynchronously by accepting a second argument, `set`, and an optional third argument, `update`, and calling either or both of these functions when appropriate.
143+
144+
> If `set` and `update` are, in combination, called multiple times synchronously, only the last change will cause the store's subscribers to be notified. For instance, calling `update` and then `set` synchronously in a `derive_value` function will only cause the value passed to `set` to be sent to subscribers.
112145
113146
In this case, you can also pass a third argument to `derived` — the initial value of the derived store before `set` or `update` is first called. If no initial value is specified, the store's initial value will be `undefined`.
114147

@@ -123,7 +156,7 @@ declare global {
123156
export {};
124157

125158
// @filename: index.ts
126-
// @errors: 18046 2769 7006
159+
// @errors: 18046 2769 7006 2722
127160
// ---cut---
128161
import { derived } from 'svelte/store';
129162

@@ -143,7 +176,7 @@ const delayedIncrement = derived(a, ($a, set, update) => {
143176
});
144177
```
145178

146-
If you return a function from the callback, it will be called when a) the callback runs again, or b) the last subscriber unsubscribes.
179+
If you return a function from the `derive_value` function, it will be called a) before the function runs again, or b) after the last subscriber unsubscribes.
147180

148181
```ts
149182
// @filename: ambient.d.ts
@@ -188,7 +221,6 @@ declare global {
188221
export {};
189222

190223
// @filename: index.ts
191-
192224
// ---cut---
193225
import { derived } from 'svelte/store';
194226

@@ -199,13 +231,75 @@ const delayed = derived([a, b], ([$a, $b], set) => {
199231
});
200232
```
201233

234+
### TypeScript type inference
235+
236+
If a multi-argument `derive_value` function is passed to `derive`, TypeScript may not be able to infer the type of the derived store, yielding a store of type `Readable<unknown>`. Set an initial value for the store to resolve this; `undefined` with a type assertion is sufficient. Alternatively, you may use type arguments, although this requires specifying the types of the dependency stores, too.
237+
238+
```ts
239+
// @filename: ambient.d.ts
240+
import { type Writable } from 'svelte/store';
241+
242+
declare global {
243+
const a: Writable<number>;
244+
}
245+
246+
export {};
247+
248+
// @filename: index.ts
249+
// ---cut---
250+
import { derived } from 'svelte/store';
251+
252+
// @errors: 2769
253+
const aInc = derived(
254+
a,
255+
($a, set) => setTimeout(() => set($a + 1), 1000),
256+
undefined as unknown as number
257+
);
258+
259+
const concatenated = derived<[number, number], string>([a, aInc], ([$a, $aInc], set) =>
260+
setTimeout(() => set(`${$a}${$aInc}`), 1000)
261+
);
262+
```
263+
264+
`derived` can derive new stores from stores not created by Svelte, including from RxJS `Observable`s. In this case, TypeScript may not be able to infer the types of data held by the dependency stores. Use a type assertion to `ExternalStore` or a type argument to provide the missing context.
265+
266+
Until TypeScript gains support for [partial type argument inference](https://github.com/microsoft/TypeScript/issues/26242), the latter option requires also specifying the return type of `derive_store`.
267+
268+
```ts
269+
// @filename: ambient.d.ts
270+
import { type Writable } from 'svelte/store';
271+
272+
declare global {
273+
const a: Writable<number>;
274+
const observable: {
275+
subscribe: (fn: (value: unknown) => void) => { unsubscribe: () => void };
276+
};
277+
}
278+
279+
export {};
280+
281+
// @filename: index.ts
282+
// ---cut---
283+
import { derived, type ExternalStore } from 'svelte/store';
284+
285+
const sum = derived(
286+
[a, observable as ExternalStore<number>],
287+
([$a, $observable]) => $a + $observable
288+
);
289+
290+
const sumMore = derived<[number, number], number>(
291+
[sum, observable],
292+
([$sum, $observable]) => $sum + $observable
293+
);
294+
```
295+
202296
## `readonly`
203297

204298
> EXPORT_SNIPPET: svelte/store#readonly
205299
206300
This simple helper function makes a store readonly. You can still subscribe to the changes from the original one using this new readable store.
207301

208-
```js
302+
```ts
209303
import { readonly, writable } from 'svelte/store';
210304

211305
const writableStore = writable(1);
@@ -224,7 +318,7 @@ readableStore.set(2); // ERROR
224318
225319
Generally, you should read the value of a store by subscribing to it and using the value as it changes over time. Occasionally, you may need to retrieve the value of a store to which you're not subscribed. `get` allows you to do so.
226320

227-
> This works by creating a subscription, reading the value, then unsubscribing. It's therefore not recommended in hot code paths.
321+
> By default, `get` subscribes to the given store, makes note of its value, then unsubscribes again. Passing `true` as a second argument causes `get` to directly read the internal state of the store instead, which, in the case of a derived store, may be outdated or `undefined`. Where performance is important, it's recommended to set `allow_stale` to `true` or not use `get`.
228322
229323
```ts
230324
// @filename: ambient.d.ts

packages/svelte/src/internal/client/runtime.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { subscribe_to_store } from '../../store/utils.js';
1+
import { subscribe_to_store } from '../../store/index.js';
22
import { EMPTY_FUNC } from '../common.js';
33
import { unwrap } from './render.js';
44
import { map_delete, map_get, map_set } from './operations.js';

packages/svelte/src/internal/server/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as $ from '../client/runtime.js';
22
import { set_is_ssr } from '../client/runtime.js';
33
import { is_promise } from '../common.js';
4-
import { subscribe_to_store } from '../../store/utils.js';
4+
import { subscribe_to_store } from '../../store/index.js';
55

66
export * from '../client/validate.js';
77

0 commit comments

Comments
 (0)