kiss-for-react 2.0.8 → 2.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -256
- package/lib/cjs/ClassPersistor.js +1 -1
- package/lib/cjs/Persistor.js +4 -4
- package/lib/cjs/Store.js +3 -3
- package/lib/cjs/Wait.js +4 -4
- package/lib/cjs/types/ClassPersistor.d.ts +1 -1
- package/lib/cjs/types/Persistor.d.ts +5 -5
- package/lib/cjs/types/Store.d.ts +3 -3
- package/lib/esm/ClassPersistor.mjs +1 -1
- package/lib/esm/Persistor.mjs +4 -4
- package/lib/esm/Store.mjs +3 -3
- package/lib/esm/Wait.mjs +4 -4
- package/lib/esm/types/ClassPersistor.d.ts +1 -1
- package/lib/esm/types/Persistor.d.ts +5 -5
- package/lib/esm/types/Store.d.ts +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ npm install kiss-for-react
|
|
|
16
16
|
## Linter → **[eslint-plugin-kiss-for-react](https://www.npmjs.com/package/eslint-plugin-kiss-for-react)**
|
|
17
17
|
|
|
18
18
|
---
|
|
19
|
-
|
|
19
|
+
|
|
20
20
|
# Overview
|
|
21
21
|
|
|
22
22
|
## Store and state
|
|
@@ -24,9 +24,6 @@ npm install kiss-for-react
|
|
|
24
24
|
The **store** holds all application **state**. Here are a few examples:
|
|
25
25
|
|
|
26
26
|
```tsx
|
|
27
|
-
// Your state is a number
|
|
28
|
-
const store = createStore<number>({initialState: 1});
|
|
29
|
-
|
|
30
27
|
// Your state is a plain JavaScript object
|
|
31
28
|
const store = createStore({initialState: {name: 'Mary', age: 25}});
|
|
32
29
|
|
|
@@ -51,25 +48,13 @@ function App() {
|
|
|
51
48
|
|
|
52
49
|
## Components use the state
|
|
53
50
|
|
|
54
|
-
The `useAllState` hook lets you access the whole state from any component.
|
|
55
|
-
The component re-renders when any part of the state changes (not recommended, but good for
|
|
56
|
-
fast prototyping).
|
|
57
|
-
|
|
58
|
-
```tsx
|
|
59
|
-
function MyComponent() {
|
|
60
|
-
const state = useAllState<State>();
|
|
61
|
-
|
|
62
|
-
return <div>{state.name} is {state.age} years old</div>;
|
|
63
|
-
};
|
|
64
|
-
```
|
|
65
|
-
|
|
66
51
|
The `useSelect` hook selects only the part of the state that your component needs.
|
|
67
52
|
The component re-renders only when that part changes.
|
|
68
53
|
|
|
69
54
|
```tsx
|
|
70
55
|
function MyComponent() {
|
|
71
|
-
const name = useSelect((
|
|
72
|
-
const age = useSelect((
|
|
56
|
+
const name = useSelect((st: State) => st.name);
|
|
57
|
+
const age = useSelect((st: State) => st.age);
|
|
73
58
|
|
|
74
59
|
return <div>{name} is {age} years old</div>;
|
|
75
60
|
};
|
|
@@ -80,23 +65,15 @@ The `useObject` hook also causes the component to re-render only when needed:
|
|
|
80
65
|
```tsx
|
|
81
66
|
function MyComponent() {
|
|
82
67
|
|
|
83
|
-
const state = useObject((
|
|
84
|
-
name:
|
|
85
|
-
age:
|
|
68
|
+
const state = useObject((st: State) => ({
|
|
69
|
+
name: st.name,
|
|
70
|
+
age: st.age,
|
|
86
71
|
}));
|
|
87
72
|
|
|
88
73
|
return <div>{state.name} is {state.age} years old</div>;
|
|
89
74
|
};
|
|
90
75
|
```
|
|
91
76
|
|
|
92
|
-
These hooks connect to the store with React's own
|
|
93
|
-
[`useSyncExternalStore`](https://react.dev/reference/react/useSyncExternalStore),
|
|
94
|
-
so React reads the state itself, compares what each component selected, and renders only
|
|
95
|
-
the components whose selection changed. React also checks the state again before
|
|
96
|
-
committing a
|
|
97
|
-
render it paused (a transition), and renders again if the state changed meanwhile, so a
|
|
98
|
-
screen never shows parts of two different states at the same time.
|
|
99
|
-
|
|
100
77
|
|
|
101
78
|
|
|
102
79
|
## Actions and reducers
|
|
@@ -121,8 +98,8 @@ class Increment extends Action {
|
|
|
121
98
|
The store's state is **immutable**.
|
|
122
99
|
|
|
123
100
|
The only way to change the store's state is by dispatching an **action**,
|
|
124
|
-
|
|
125
|
-
The new state replaces the current one, and
|
|
101
|
+
which runs the action's reducer to get a new state.
|
|
102
|
+
The new state replaces the current one, and components re-render.
|
|
126
103
|
|
|
127
104
|
```tsx
|
|
128
105
|
// Dispatch an action
|
|
@@ -175,8 +152,7 @@ function MyComponent() {
|
|
|
175
152
|
```
|
|
176
153
|
|
|
177
154
|
To dispatch actions when the component mounts, when some values change, and when it
|
|
178
|
-
unmounts,
|
|
179
|
-
use `useStoreEffect` instead of a `useEffect`:
|
|
155
|
+
unmounts, use `useStoreEffect` instead of a `useEffect`:
|
|
180
156
|
|
|
181
157
|
```tsx
|
|
182
158
|
useStoreEffect({
|
|
@@ -196,23 +172,22 @@ useStoreEffect({
|
|
|
196
172
|
});
|
|
197
173
|
```
|
|
198
174
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
run
|
|
203
|
-
|
|
204
|
-
`
|
|
175
|
+
There are also:
|
|
176
|
+
|
|
177
|
+
* `onFocus` to run an action when the user comes back to the browser tab.
|
|
178
|
+
* `onBlur` to run an action when the user leaves the browser tab.
|
|
179
|
+
* `onOnline` to run an action when the internet connection comes back.
|
|
180
|
+
* `onOffline` to run an action when the internet connection is lost.
|
|
205
181
|
|
|
206
182
|
```tsx
|
|
207
|
-
useStoreEffect({
|
|
208
|
-
onMount: (store) => store.dispatch(new LoadPrices()),
|
|
183
|
+
useStoreEffect({
|
|
209
184
|
onFocus: (store) => store.dispatch(new LoadPrices()),
|
|
210
185
|
onOnline: (store) => store.dispatch(new LoadPrices()),
|
|
211
186
|
});
|
|
212
187
|
```
|
|
213
188
|
|
|
214
|
-
|
|
215
|
-
|
|
189
|
+
These are also available in components with `useIsFocused()` and `useIsOnline()`,
|
|
190
|
+
so that you can show an "offline" banner.
|
|
216
191
|
|
|
217
192
|
|
|
218
193
|
|
|
@@ -304,9 +279,9 @@ function UserRow({ userId }: { userId: string }) {
|
|
|
304
279
|
}
|
|
305
280
|
```
|
|
306
281
|
|
|
307
|
-
|
|
308
|
-
`
|
|
309
|
-
|
|
282
|
+
You can also pass an array to check several action types at once. For example, to show a
|
|
283
|
+
spinner while either `LoadUser` or `LoadPosts` is running, or an error message if either
|
|
284
|
+
has failed:
|
|
310
285
|
|
|
311
286
|
```tsx
|
|
312
287
|
const isLoading = useIsWaiting([LoadUser, LoadPosts]);
|
|
@@ -314,16 +289,15 @@ const isFailed = useIsFailed([LoadUser, LoadPosts]);
|
|
|
314
289
|
const error = useExceptionFor([LoadUser, LoadPosts]);
|
|
315
290
|
```
|
|
316
291
|
|
|
317
|
-
|
|
318
|
-
|
|
292
|
+
Note the `useExceptionFor` above returns the `UserException` thrown by `LoadUser` or by
|
|
293
|
+
`LoadPosts`. If no error happened, it returns `null`. You can then show this error message
|
|
294
|
+
in the screen.
|
|
319
295
|
|
|
320
296
|
### Suspense and error boundaries
|
|
321
297
|
|
|
322
298
|
If you prefer React's `<Suspense>` and error boundaries to `isWaiting`/`isFailed` checks,
|
|
323
|
-
use
|
|
324
|
-
|
|
325
|
-
error
|
|
326
|
-
to the nearest error boundary if the action failed:
|
|
299
|
+
use `useSuspense` instead. It suspends the component while the action runs, and throws the
|
|
300
|
+
error to the nearest error boundary if the action failed:
|
|
327
301
|
|
|
328
302
|
```tsx
|
|
329
303
|
function UserName({ userId }: { userId: string }) {
|
|
@@ -484,22 +458,6 @@ class LoadText extends Action {
|
|
|
484
458
|
}
|
|
485
459
|
```
|
|
486
460
|
|
|
487
|
-
By default, it checks the action class. To let actions of the same class run in parallel
|
|
488
|
-
when they have different parameters, override `nonReentrantKeyParams()`:
|
|
489
|
-
|
|
490
|
-
```tsx
|
|
491
|
-
class SaveItem extends Action {
|
|
492
|
-
constructor(readonly itemId: string) { super(); }
|
|
493
|
-
|
|
494
|
-
nonReentrant = true;
|
|
495
|
-
nonReentrantKeyParams() { return this.itemId; }
|
|
496
|
-
...
|
|
497
|
-
}
|
|
498
|
-
```
|
|
499
|
-
|
|
500
|
-
Now `SaveItem('A')` and `SaveItem('B')` can run in parallel, but two `SaveItem('A')`
|
|
501
|
-
cannot.
|
|
502
|
-
|
|
503
461
|
### Retry
|
|
504
462
|
|
|
505
463
|
To retry an action a few times with exponential backoff if it fails,
|
|
@@ -529,22 +487,6 @@ class LoadText extends Action {
|
|
|
529
487
|
}
|
|
530
488
|
```
|
|
531
489
|
|
|
532
|
-
By default, every error is retried.
|
|
533
|
-
To give up on errors that will fail again anyway (like a 404 or a 401),
|
|
534
|
-
override `shouldRetry`.
|
|
535
|
-
|
|
536
|
-
```tsx
|
|
537
|
-
class LoadText extends Action {
|
|
538
|
-
retry = {on: true}
|
|
539
|
-
|
|
540
|
-
shouldRetry(error: unknown, attempt: number) {
|
|
541
|
-
return !(error instanceof NotFoundError);
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
async reduce() { ... }
|
|
545
|
-
}
|
|
546
|
-
```
|
|
547
|
-
|
|
548
490
|
### CheckInternet
|
|
549
491
|
|
|
550
492
|
To check for an internet connection before running the action, add the `checkInternet`
|
|
@@ -559,27 +501,6 @@ class LoadPrices extends Action {
|
|
|
559
501
|
}
|
|
560
502
|
```
|
|
561
503
|
|
|
562
|
-
Use `checkInternet = { dialog: false }` to fail without a dialog, so you can show the
|
|
563
|
-
error in your components with `isFailed`. Or use `checkInternet = { abort: true }` to
|
|
564
|
-
abort the action silently, as if it had never been dispatched, without any errors.
|
|
565
|
-
|
|
566
|
-
To show a different error, override `noInternetException()`:
|
|
567
|
-
|
|
568
|
-
```tsx
|
|
569
|
-
class LoadPrices extends Action {
|
|
570
|
-
checkInternet = { dialog: true }
|
|
571
|
-
|
|
572
|
-
noInternetException() {
|
|
573
|
-
return new UserException('You are offline. The prices could not be loaded.');
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
async reduce() { ... }
|
|
577
|
-
}
|
|
578
|
-
```
|
|
579
|
-
|
|
580
|
-
The store checks the internet before it calls the action's `before()` method. So you can
|
|
581
|
-
override `before()` without calling `super.before()`.
|
|
582
|
-
|
|
583
504
|
### UnlimitedRetryCheckInternet
|
|
584
505
|
|
|
585
506
|
To keep trying an action until it succeeds, add the `unlimitedRetryCheckInternet`
|
|
@@ -640,9 +561,9 @@ You can also check `this.isAborted` inside the reducer to know if the action was
|
|
|
640
561
|
|
|
641
562
|
### Aborting actions
|
|
642
563
|
|
|
643
|
-
Call `store.abort(ActionType)` to abort all the actions of that type that are in
|
|
644
|
-
for example, from a "Cancel" button, or when the component that started a
|
|
645
|
-
unmounts.
|
|
564
|
+
Call `store.abort(ActionType)` to abort all the actions of that type that are in
|
|
565
|
+
progress, for example, from a "Cancel" button, or when the component that started a
|
|
566
|
+
request unmounts.
|
|
646
567
|
|
|
647
568
|
```tsx
|
|
648
569
|
useStoreEffect({
|
|
@@ -651,7 +572,8 @@ useStoreEffect({
|
|
|
651
572
|
});
|
|
652
573
|
```
|
|
653
574
|
|
|
654
|
-
To abort the actions of several types, pass an array:
|
|
575
|
+
To abort the actions of several types, pass an array:
|
|
576
|
+
`store.abort([LoadUser, LoadPosts])`.
|
|
655
577
|
|
|
656
578
|
### Throttle
|
|
657
579
|
|
|
@@ -671,18 +593,6 @@ class LoadPrices extends Action {
|
|
|
671
593
|
}
|
|
672
594
|
```
|
|
673
595
|
|
|
674
|
-
Override `ignoreThrottle` to run the action anyway under some conditions (for example,
|
|
675
|
-
a `force` flag).
|
|
676
|
-
|
|
677
|
-
```tsx
|
|
678
|
-
class LoadPrices extends Action {
|
|
679
|
-
constructor(public force = false) { super(); }
|
|
680
|
-
throttle = 5000;
|
|
681
|
-
get ignoreThrottle() { return this.force; }
|
|
682
|
-
...
|
|
683
|
-
}
|
|
684
|
-
```
|
|
685
|
-
|
|
686
596
|
### Fresh
|
|
687
597
|
|
|
688
598
|
To avoid reloading the same information too often, add a `fresh` property to your action
|
|
@@ -719,27 +629,15 @@ class SaveItem extends Action {
|
|
|
719
629
|
}
|
|
720
630
|
```
|
|
721
631
|
|
|
722
|
-
By default, all sequential actions share a single queue, even if they are of different
|
|
723
|
-
classes. To have independent queues, override `sequentialKeyParams()`. Actions with the
|
|
724
|
-
same key run one at a time, while actions with different keys run in parallel:
|
|
725
|
-
|
|
726
|
-
```tsx
|
|
727
|
-
class SaveUser extends Action {
|
|
728
|
-
sequential = true;
|
|
729
|
-
constructor(readonly userId: string) { super(); }
|
|
730
|
-
sequentialKeyParams() { return this.userId; }
|
|
731
|
-
...
|
|
732
|
-
}
|
|
733
|
-
```
|
|
734
|
-
|
|
735
632
|
### Polling
|
|
736
633
|
|
|
737
634
|
To periodically dispatch an action at a fixed interval, add a `poll` property to your
|
|
738
635
|
action class (usually as a constructor parameter), and override `createPollingAction()` to
|
|
739
|
-
return the action each tick dispatches.
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
636
|
+
return the action each tick dispatches.
|
|
637
|
+
|
|
638
|
+
Dispatch it with `Poll.start` to run it right away and start polling, and with `Poll.stop`
|
|
639
|
+
to stop polling. Use `Poll.once` (the default) to run it once, without affecting the
|
|
640
|
+
polling.
|
|
743
641
|
|
|
744
642
|
```tsx
|
|
745
643
|
class LoadPrices extends Action {
|
|
@@ -757,17 +655,6 @@ dispatch(new LoadPrices(Poll.start)); // Start polling.
|
|
|
757
655
|
dispatch(new LoadPrices(Poll.stop)); // Stop polling.
|
|
758
656
|
```
|
|
759
657
|
|
|
760
|
-
Each tick waits for the previous run to finish, so runs never overlap.
|
|
761
|
-
Set `pollWaitsForRun = false` to tick at a fixed rate instead.
|
|
762
|
-
|
|
763
|
-
By default, the ticks only run while the app is focused (in the browser, the tab is
|
|
764
|
-
visible) and online. A tick that comes due while the user is away waits, and runs as soon
|
|
765
|
-
as the user comes back to the tab, or the internet comes back. Set
|
|
766
|
-
`pollOnlyWhenFocused = false` or `pollOnlyWhenOnline = false` to keep polling anyway.
|
|
767
|
-
|
|
768
|
-
To stop all polling at once (for example, on logout), call `stopAllPolling()` from any
|
|
769
|
-
action.
|
|
770
|
-
|
|
771
658
|
## Advanced Features
|
|
772
659
|
|
|
773
660
|
### OptimisticCommand
|
|
@@ -806,8 +693,7 @@ again.
|
|
|
806
693
|
## Disposing the store
|
|
807
694
|
|
|
808
695
|
Apps have long-lived things that are not state, and must not go in the state: intervals,
|
|
809
|
-
WebSockets, `EventSource`s, `AbortController`s, listeners
|
|
810
|
-
which returns an unsubscribe function), RxJS subscriptions.
|
|
696
|
+
WebSockets, `EventSource`s, `AbortController`s, listeners etc.
|
|
811
697
|
|
|
812
698
|
Keep them in the store **props**, with `setProp`, and close them with `disposeProp`:
|
|
813
699
|
|
|
@@ -828,22 +714,9 @@ class StopClock extends Action {
|
|
|
828
714
|
}
|
|
829
715
|
```
|
|
830
716
|
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
`MessagePort` or `Worker` (closed or terminated), an object with `unsubscribe()` (RxJS)
|
|
835
|
-
and an object with `[Symbol.dispose]()` or `[Symbol.asyncDispose]()`.
|
|
836
|
-
|
|
837
|
-
Setting a prop again with the same key disposes the previous value, so dispatching
|
|
838
|
-
`StartClock` twice doesn't leak the first interval. Read a prop with
|
|
839
|
-
`prop(key)`, list them with `props`, and dispose many with `disposeProps(predicate?)`.
|
|
840
|
-
|
|
841
|
-
The same methods exist on the store. On logout, `store.logOut({ disposeProps: true })`
|
|
842
|
-
disposes them too.
|
|
843
|
-
|
|
844
|
-
To end the store, when the app finishes or at the end of each test, call
|
|
845
|
-
`store.dispose()`. It shuts down the store, stops all the action features, cancels the
|
|
846
|
-
waiting `dispatchWhen`s, pauses the persistor, and disposes all the props.
|
|
717
|
+
To end the store, when the app finishes, the user logs out, or at the end of each test,
|
|
718
|
+
call `store.dispose()`. It shuts down the store, stops all the action features, cancels
|
|
719
|
+
the waiting `dispatchWhen`s, pauses the persistor, and disposes all the props.
|
|
847
720
|
It returns a promise that waits for the async disposals.
|
|
848
721
|
|
|
849
722
|
|
|
@@ -854,10 +727,7 @@ Inject the services the actions use (API clients, repositories, analytics...) in
|
|
|
854
727
|
store, instead of importing them from modules. Each test then creates its own store with
|
|
855
728
|
its own dependencies (fakes, for example), with nothing global to set up or reset, and two
|
|
856
729
|
stores can live side by side (tests running concurrently, or one store per request in
|
|
857
|
-
server-side rendering).
|
|
858
|
-
testing...), a `configuration` factory (feature flags) and a `dependencies` factory. The
|
|
859
|
-
factories get the store, so they can depend on the environment, and the configuration is
|
|
860
|
-
created first, so the dependencies can read it:
|
|
730
|
+
server-side rendering).
|
|
861
731
|
|
|
862
732
|
```tsx
|
|
863
733
|
const store = createStore<State>({
|
|
@@ -868,12 +738,6 @@ const store = createStore<State>({
|
|
|
868
738
|
});
|
|
869
739
|
```
|
|
870
740
|
|
|
871
|
-
In components, `useEnvironment()` and `useConfiguration()` return the environment and the
|
|
872
|
-
configuration, and never re-render, since the values don't change. Components should
|
|
873
|
-
usually not need the dependencies. The `errorObserver` gets the store, so it can read
|
|
874
|
-
`store.environment`, for example to send the errors to a monitoring service only in
|
|
875
|
-
production.
|
|
876
|
-
|
|
877
741
|
In tests:
|
|
878
742
|
|
|
879
743
|
```tsx
|
|
@@ -906,64 +770,20 @@ store.dispatch(new InitAppAction());
|
|
|
906
770
|
|
|
907
771
|
## Server-side rendering
|
|
908
772
|
|
|
909
|
-
Kiss
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
Kiss app.
|
|
773
|
+
Kiss supports server-side rendering with Node and Next.js, including both the App Router
|
|
774
|
+
and the Pages Router. Load the data on the server, render the page, and let the browser
|
|
775
|
+
continue with the same state.
|
|
913
776
|
|
|
914
|
-
|
|
915
|
-
|
|
777
|
+
* **Reuse your actions.** The same actions can load data on the server and in the browser.
|
|
778
|
+
* **Keep the first render consistent.** Kiss keeps the initial browser state in sync with
|
|
779
|
+
the server-rendered HTML to avoid hydration mismatches.
|
|
780
|
+
* **Keep requests separate.** Each server request gets its own store and state.
|
|
781
|
+
* **Use streaming and server components.** Kiss works with React streaming and Next.js
|
|
782
|
+
Server Components.
|
|
916
783
|
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
const html = renderToString(
|
|
921
|
-
<StoreProvider store={store} serverState={store.state}>
|
|
922
|
-
<App />
|
|
923
|
-
</StoreProvider>
|
|
924
|
-
);
|
|
925
|
-
const transfer = JSON.stringify(store.state); // Goes in the HTML, for the browser.
|
|
926
|
-
await store.dispose();
|
|
927
|
-
```
|
|
928
|
-
|
|
929
|
-
In the browser, create the store with the state the server sent, and hydrate with the same
|
|
930
|
-
`StoreProvider`. That's all the setup. What you get:
|
|
931
|
-
|
|
932
|
-
- **No hydration mismatches.** While hydrating, the hooks render the state the server
|
|
933
|
-
rendered, even if a persistor already restored a newer state in the browser. After
|
|
934
|
-
hydration, the components catch up with the live state.
|
|
935
|
-
- **The same actions on both sides.** A `LoadData` that polls, retries or debounces in the
|
|
936
|
-
browser simply runs once in the server store, so a request leaves nothing running in Node.
|
|
937
|
-
- **Next.js, both routers.** A server component (or `getServerSideProps`) loads the data, and
|
|
938
|
-
a client component creates the store once and provides it. Each route gives its own data to
|
|
939
|
-
the store with `StoreSlice`: the HTML shows it, and client-side navigation shows the new
|
|
940
|
-
route's data in its very first render, with no empty or stale frame.
|
|
941
|
-
|
|
942
|
-
```tsx
|
|
943
|
-
// app/users/[id]/page.tsx, a server component.
|
|
944
|
-
export default async function UserPage({ params }) {
|
|
945
|
-
const user = await fetchUser((await params).id);
|
|
946
|
-
return <StoreSlice state={{ user }} key={user.id}><UserView /></StoreSlice>;
|
|
947
|
-
}
|
|
948
|
-
```
|
|
949
|
-
|
|
950
|
-
Mutations are ordinary Kiss actions, which call a server action or an API route, so you keep
|
|
951
|
-
`useIsWaiting`, `useIsFailed`, retries and optimistic updates on top of them.
|
|
952
|
-
- **Streaming.** `renderToPipeableStream` and `renderToReadableStream` work, Suspense
|
|
953
|
-
boundaries included.
|
|
954
|
-
- **Nothing half-loaded in the HTML.** On the server, `useIsWaiting` and `useIsFailed` are
|
|
955
|
-
`false`, and `useSuspense` never suspends, so the HTML never shows a spinner or an error.
|
|
956
|
-
|
|
957
|
-
Complete examples, each with a persistor in the browser and Playwright tests that check the
|
|
958
|
-
server HTML, the hydration and the store:
|
|
959
|
-
[Next.js app router](examples/nextjs-app-router),
|
|
960
|
-
[Next.js pages router](examples/nextjs-pages-router),
|
|
961
|
-
[Express with streaming](examples/ssr-express), and a
|
|
962
|
-
[todo app with Next.js](examples/todo-app-nextjs) with a class-based state and the
|
|
963
|
-
`ClassPersistor`. The details (serialization, the persistor, streaming, React Server
|
|
964
|
-
Components) are in the
|
|
965
|
-
[server-side rendering](https://kissforreact.org/react/miscellaneous/server-side-rendering)
|
|
966
|
-
page of the docs.
|
|
784
|
+
See
|
|
785
|
+
the [server-side rendering docs](https://kissforreact.org/react/miscellaneous/server-side-rendering)
|
|
786
|
+
for setup and examples.
|
|
967
787
|
|
|
968
788
|
|
|
969
789
|
|
|
@@ -1016,12 +836,9 @@ const store = createStore<string>({
|
|
|
1016
836
|
});
|
|
1017
837
|
```
|
|
1018
838
|
|
|
1019
|
-
The `errorObserver` gets all errors thrown by your actions
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
or `null` to swallow it. Then, a `UserException` is shown to the user in an error dialog,
|
|
1023
|
-
and
|
|
1024
|
-
any other error is thrown.
|
|
839
|
+
The `errorObserver` gets all errors thrown by your actions and persistor. It can log them,
|
|
840
|
+
swallow them, or return a `UserException` to show a message to the user in an error
|
|
841
|
+
dialog.
|
|
1025
842
|
|
|
1026
843
|
For example, here we convert the `FirestoreError` errors thrown by Firebase into
|
|
1027
844
|
`UserException` errors, which automatically show a message to the user. Other errors are
|
|
@@ -1039,28 +856,13 @@ errorObserver: ({ error, action }) => {
|
|
|
1039
856
|
}
|
|
1040
857
|
```
|
|
1041
858
|
|
|
1042
|
-
By default, Kiss logs what it does (for example, each dispatched action) to the console.
|
|
1043
|
-
Use `logger` to send these messages somewhere else, or set it to `null` to turn logging
|
|
1044
|
-
off.
|
|
1045
|
-
With `null`, Kiss doesn't even build the log messages:
|
|
1046
|
-
|
|
1047
|
-
```tsx
|
|
1048
|
-
const store = createStore<State>({
|
|
1049
|
-
initialState: new State(),
|
|
1050
|
-
logger: (obj) => myLogger.info(obj), // Or `null` to turn logging off.
|
|
1051
|
-
logStateChanges: true, // Also log every state change.
|
|
1052
|
-
});
|
|
1053
|
-
```
|
|
1054
|
-
|
|
1055
859
|
|
|
1056
860
|
|
|
1057
861
|
## Advanced action configuration
|
|
1058
862
|
|
|
1059
863
|
The team lead can create a base action class that all actions will extend, and add some
|
|
1060
|
-
common
|
|
1061
|
-
|
|
1062
|
-
parts of the state
|
|
1063
|
-
and helper methods to find information.
|
|
864
|
+
common functionality to it. For example, the base class can provide getter shortcuts to
|
|
865
|
+
important parts of the state and helper methods to find information.
|
|
1064
866
|
|
|
1065
867
|
```tsx
|
|
1066
868
|
class State {
|
package/lib/cjs/Persistor.js
CHANGED
|
@@ -8,8 +8,8 @@ const utils_1 = require("./utils");
|
|
|
8
8
|
* Use it like this:
|
|
9
9
|
*
|
|
10
10
|
* ```ts
|
|
11
|
-
* const store = createStore<
|
|
12
|
-
* initialState:
|
|
11
|
+
* const store = createStore<State>({
|
|
12
|
+
* initialState: State.initialState(),
|
|
13
13
|
* persistor: new MyPersistor(),
|
|
14
14
|
* });
|
|
15
15
|
*
|
|
@@ -97,9 +97,9 @@ exports.Persistor = Persistor;
|
|
|
97
97
|
* Use it like this:
|
|
98
98
|
*
|
|
99
99
|
* ```ts
|
|
100
|
-
* const store = createStore<
|
|
100
|
+
* const store = createStore<State>({
|
|
101
101
|
* ...otherOptions,
|
|
102
|
-
* persistor: new PersistorPrinterDecorator<
|
|
102
|
+
* persistor: new PersistorPrinterDecorator<State>(persistor),
|
|
103
103
|
* });
|
|
104
104
|
* ```
|
|
105
105
|
*/
|
package/lib/cjs/Store.js
CHANGED
|
@@ -823,8 +823,8 @@ class Store {
|
|
|
823
823
|
* the store to be ready before dispatching any actions:
|
|
824
824
|
*
|
|
825
825
|
* ```ts
|
|
826
|
-
* const store = createStore<
|
|
827
|
-
* initialState:
|
|
826
|
+
* const store = createStore<State>({
|
|
827
|
+
* initialState: State.initialState(),
|
|
828
828
|
* persistor: new MyPersistor(),
|
|
829
829
|
* });
|
|
830
830
|
*
|
|
@@ -3566,7 +3566,7 @@ class Store {
|
|
|
3566
3566
|
*
|
|
3567
3567
|
* ```ts
|
|
3568
3568
|
* try {
|
|
3569
|
-
* await store.logOut({ initialState:
|
|
3569
|
+
* await store.logOut({ initialState: State.initialState() });
|
|
3570
3570
|
* goToLoginScreen();
|
|
3571
3571
|
* } catch (error) {
|
|
3572
3572
|
* goToLogOutFailedScreen(error);
|
package/lib/cjs/Wait.js
CHANGED
|
@@ -122,8 +122,8 @@
|
|
|
122
122
|
// * This is useful when you want to wait for an Action to finish. For example:
|
|
123
123
|
// *
|
|
124
124
|
// * ```
|
|
125
|
-
// * class MyAction extends KissAction<
|
|
126
|
-
// * Future<
|
|
125
|
+
// * class MyAction extends KissAction<State> {
|
|
126
|
+
// * Future<State?> reduce() async {
|
|
127
127
|
// * await doSomething();
|
|
128
128
|
// * return null;
|
|
129
129
|
// * }
|
|
@@ -185,9 +185,9 @@
|
|
|
185
185
|
// * `copyWith` method that copies this field as a named parameter. For example:
|
|
186
186
|
// *
|
|
187
187
|
// * ```
|
|
188
|
-
// * class
|
|
188
|
+
// * class State {
|
|
189
189
|
// * wait: Wait;
|
|
190
|
-
// * copy({wait :Wait}) :
|
|
190
|
+
// * copy({wait :Wait}) :State => State({wait: wait});
|
|
191
191
|
// * }
|
|
192
192
|
// * ```
|
|
193
193
|
// */
|
|
@@ -3,8 +3,8 @@ import { KissAction } from './KissAction';
|
|
|
3
3
|
* Use it like this:
|
|
4
4
|
*
|
|
5
5
|
* ```ts
|
|
6
|
-
* const store = createStore<
|
|
7
|
-
* initialState:
|
|
6
|
+
* const store = createStore<State>({
|
|
7
|
+
* initialState: State.initialState(),
|
|
8
8
|
* persistor: new MyPersistor(),
|
|
9
9
|
* });
|
|
10
10
|
*
|
|
@@ -45,7 +45,7 @@ export declare abstract class Persistor<St> {
|
|
|
45
45
|
* can report an error with `addError` instead of throwing. For example:
|
|
46
46
|
*
|
|
47
47
|
* ```ts
|
|
48
|
-
* async readState(): Promise<
|
|
48
|
+
* async readState(): Promise<State | null> {
|
|
49
49
|
* try {
|
|
50
50
|
* return await this.read();
|
|
51
51
|
* } catch (error) {
|
|
@@ -143,9 +143,9 @@ export declare abstract class Persistor<St> {
|
|
|
143
143
|
* Use it like this:
|
|
144
144
|
*
|
|
145
145
|
* ```ts
|
|
146
|
-
* const store = createStore<
|
|
146
|
+
* const store = createStore<State>({
|
|
147
147
|
* ...otherOptions,
|
|
148
|
-
* persistor: new PersistorPrinterDecorator<
|
|
148
|
+
* persistor: new PersistorPrinterDecorator<State>(persistor),
|
|
149
149
|
* });
|
|
150
150
|
* ```
|
|
151
151
|
*/
|
package/lib/cjs/types/Store.d.ts
CHANGED
|
@@ -918,8 +918,8 @@ export declare class Store<St> {
|
|
|
918
918
|
* the store to be ready before dispatching any actions:
|
|
919
919
|
*
|
|
920
920
|
* ```ts
|
|
921
|
-
* const store = createStore<
|
|
922
|
-
* initialState:
|
|
921
|
+
* const store = createStore<State>({
|
|
922
|
+
* initialState: State.initialState(),
|
|
923
923
|
* persistor: new MyPersistor(),
|
|
924
924
|
* });
|
|
925
925
|
*
|
|
@@ -1760,7 +1760,7 @@ export declare class Store<St> {
|
|
|
1760
1760
|
*
|
|
1761
1761
|
* ```ts
|
|
1762
1762
|
* try {
|
|
1763
|
-
* await store.logOut({ initialState:
|
|
1763
|
+
* await store.logOut({ initialState: State.initialState() });
|
|
1764
1764
|
* goToLoginScreen();
|
|
1765
1765
|
* } catch (error) {
|
|
1766
1766
|
* goToLogOutFailedScreen(error);
|
package/lib/esm/Persistor.mjs
CHANGED
|
@@ -5,8 +5,8 @@ import { describeValue } from './utils.mjs';
|
|
|
5
5
|
* Use it like this:
|
|
6
6
|
*
|
|
7
7
|
* ```ts
|
|
8
|
-
* const store = createStore<
|
|
9
|
-
* initialState:
|
|
8
|
+
* const store = createStore<State>({
|
|
9
|
+
* initialState: State.initialState(),
|
|
10
10
|
* persistor: new MyPersistor(),
|
|
11
11
|
* });
|
|
12
12
|
*
|
|
@@ -91,9 +91,9 @@ export class Persistor {
|
|
|
91
91
|
* Use it like this:
|
|
92
92
|
*
|
|
93
93
|
* ```ts
|
|
94
|
-
* const store = createStore<
|
|
94
|
+
* const store = createStore<State>({
|
|
95
95
|
* ...otherOptions,
|
|
96
|
-
* persistor: new PersistorPrinterDecorator<
|
|
96
|
+
* persistor: new PersistorPrinterDecorator<State>(persistor),
|
|
97
97
|
* });
|
|
98
98
|
* ```
|
|
99
99
|
*/
|
package/lib/esm/Store.mjs
CHANGED
|
@@ -940,8 +940,8 @@ export class Store {
|
|
|
940
940
|
* the store to be ready before dispatching any actions:
|
|
941
941
|
*
|
|
942
942
|
* ```ts
|
|
943
|
-
* const store = createStore<
|
|
944
|
-
* initialState:
|
|
943
|
+
* const store = createStore<State>({
|
|
944
|
+
* initialState: State.initialState(),
|
|
945
945
|
* persistor: new MyPersistor(),
|
|
946
946
|
* });
|
|
947
947
|
*
|
|
@@ -3693,7 +3693,7 @@ export class Store {
|
|
|
3693
3693
|
*
|
|
3694
3694
|
* ```ts
|
|
3695
3695
|
* try {
|
|
3696
|
-
* await store.logOut({ initialState:
|
|
3696
|
+
* await store.logOut({ initialState: State.initialState() });
|
|
3697
3697
|
* goToLoginScreen();
|
|
3698
3698
|
* } catch (error) {
|
|
3699
3699
|
* goToLogOutFailedScreen(error);
|
package/lib/esm/Wait.mjs
CHANGED
|
@@ -122,8 +122,8 @@
|
|
|
122
122
|
// * This is useful when you want to wait for an Action to finish. For example:
|
|
123
123
|
// *
|
|
124
124
|
// * ```
|
|
125
|
-
// * class MyAction extends KissAction<
|
|
126
|
-
// * Future<
|
|
125
|
+
// * class MyAction extends KissAction<State> {
|
|
126
|
+
// * Future<State?> reduce() async {
|
|
127
127
|
// * await doSomething();
|
|
128
128
|
// * return null;
|
|
129
129
|
// * }
|
|
@@ -185,9 +185,9 @@
|
|
|
185
185
|
// * `copyWith` method that copies this field as a named parameter. For example:
|
|
186
186
|
// *
|
|
187
187
|
// * ```
|
|
188
|
-
// * class
|
|
188
|
+
// * class State {
|
|
189
189
|
// * wait: Wait;
|
|
190
|
-
// * copy({wait :Wait}) :
|
|
190
|
+
// * copy({wait :Wait}) :State => State({wait: wait});
|
|
191
191
|
// * }
|
|
192
192
|
// * ```
|
|
193
193
|
// */
|
|
@@ -3,8 +3,8 @@ import { KissAction } from './KissAction';
|
|
|
3
3
|
* Use it like this:
|
|
4
4
|
*
|
|
5
5
|
* ```ts
|
|
6
|
-
* const store = createStore<
|
|
7
|
-
* initialState:
|
|
6
|
+
* const store = createStore<State>({
|
|
7
|
+
* initialState: State.initialState(),
|
|
8
8
|
* persistor: new MyPersistor(),
|
|
9
9
|
* });
|
|
10
10
|
*
|
|
@@ -45,7 +45,7 @@ export declare abstract class Persistor<St> {
|
|
|
45
45
|
* can report an error with `addError` instead of throwing. For example:
|
|
46
46
|
*
|
|
47
47
|
* ```ts
|
|
48
|
-
* async readState(): Promise<
|
|
48
|
+
* async readState(): Promise<State | null> {
|
|
49
49
|
* try {
|
|
50
50
|
* return await this.read();
|
|
51
51
|
* } catch (error) {
|
|
@@ -143,9 +143,9 @@ export declare abstract class Persistor<St> {
|
|
|
143
143
|
* Use it like this:
|
|
144
144
|
*
|
|
145
145
|
* ```ts
|
|
146
|
-
* const store = createStore<
|
|
146
|
+
* const store = createStore<State>({
|
|
147
147
|
* ...otherOptions,
|
|
148
|
-
* persistor: new PersistorPrinterDecorator<
|
|
148
|
+
* persistor: new PersistorPrinterDecorator<State>(persistor),
|
|
149
149
|
* });
|
|
150
150
|
* ```
|
|
151
151
|
*/
|
package/lib/esm/types/Store.d.ts
CHANGED
|
@@ -918,8 +918,8 @@ export declare class Store<St> {
|
|
|
918
918
|
* the store to be ready before dispatching any actions:
|
|
919
919
|
*
|
|
920
920
|
* ```ts
|
|
921
|
-
* const store = createStore<
|
|
922
|
-
* initialState:
|
|
921
|
+
* const store = createStore<State>({
|
|
922
|
+
* initialState: State.initialState(),
|
|
923
923
|
* persistor: new MyPersistor(),
|
|
924
924
|
* });
|
|
925
925
|
*
|
|
@@ -1760,7 +1760,7 @@ export declare class Store<St> {
|
|
|
1760
1760
|
*
|
|
1761
1761
|
* ```ts
|
|
1762
1762
|
* try {
|
|
1763
|
-
* await store.logOut({ initialState:
|
|
1763
|
+
* await store.logOut({ initialState: State.initialState() });
|
|
1764
1764
|
* goToLoginScreen();
|
|
1765
1765
|
* } catch (error) {
|
|
1766
1766
|
* goToLogOutFailedScreen(error);
|