kiss-for-react 1.0.3 → 2.0.0

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.
Files changed (155) hide show
  1. package/README.md +699 -156
  2. package/lib/cjs/ClassPersistor.js +177 -112
  3. package/lib/cjs/Esserializer/DeserializationOptions.js +4 -4
  4. package/lib/cjs/Esserializer/SerializeOptions.js +4 -4
  5. package/lib/cjs/Esserializer/builtins.js +258 -0
  6. package/lib/cjs/Esserializer/constant.js +39 -131
  7. package/lib/cjs/Esserializer/deserializer.js +226 -313
  8. package/lib/cjs/Esserializer/general.js +91 -39
  9. package/lib/cjs/Esserializer/index.js +47 -43
  10. package/lib/cjs/Esserializer/serializer.js +168 -133
  11. package/lib/cjs/Evt.js +132 -0
  12. package/lib/cjs/Hooks.js +1472 -969
  13. package/lib/cjs/KissAction.js +4635 -1333
  14. package/lib/cjs/LoadPages.js +336 -0
  15. package/lib/cjs/Persistor.js +189 -120
  16. package/lib/cjs/ProcessPersistence.js +579 -262
  17. package/lib/cjs/Store.js +4278 -1534
  18. package/lib/cjs/StoreException.js +162 -75
  19. package/lib/cjs/StoreProvider.js +88 -0
  20. package/lib/cjs/UserException.js +212 -218
  21. package/lib/cjs/Wait.js +309 -309
  22. package/lib/cjs/index.js +69 -42
  23. package/lib/cjs/server.js +54 -0
  24. package/lib/cjs/types/ClassPersistor.d.ts +143 -112
  25. package/lib/cjs/types/ClassPersistor.d.ts.map +1 -1
  26. package/lib/cjs/types/Esserializer/DeserializationOptions.d.ts +13 -6
  27. package/lib/cjs/types/Esserializer/DeserializationOptions.d.ts.map +1 -1
  28. package/lib/cjs/types/Esserializer/SerializeOptions.d.ts +11 -5
  29. package/lib/cjs/types/Esserializer/SerializeOptions.d.ts.map +1 -1
  30. package/lib/cjs/types/Esserializer/builtins.d.ts +36 -0
  31. package/lib/cjs/types/Esserializer/builtins.d.ts.map +1 -0
  32. package/lib/cjs/types/Esserializer/constant.d.ts +25 -49
  33. package/lib/cjs/types/Esserializer/constant.d.ts.map +1 -1
  34. package/lib/cjs/types/Esserializer/deserializer.d.ts +27 -17
  35. package/lib/cjs/types/Esserializer/deserializer.d.ts.map +1 -1
  36. package/lib/cjs/types/Esserializer/general.d.ts +28 -4
  37. package/lib/cjs/types/Esserializer/general.d.ts.map +1 -1
  38. package/lib/cjs/types/Esserializer/index.d.ts +33 -29
  39. package/lib/cjs/types/Esserializer/index.d.ts.map +1 -1
  40. package/lib/cjs/types/Esserializer/serializer.d.ts +14 -3
  41. package/lib/cjs/types/Esserializer/serializer.d.ts.map +1 -1
  42. package/lib/cjs/types/Evt.d.ts +124 -0
  43. package/lib/cjs/types/Evt.d.ts.map +1 -0
  44. package/lib/cjs/types/Hooks.d.ts +1174 -837
  45. package/lib/cjs/types/Hooks.d.ts.map +1 -1
  46. package/lib/cjs/types/KissAction.d.ts +5199 -1398
  47. package/lib/cjs/types/KissAction.d.ts.map +1 -1
  48. package/lib/cjs/types/LoadPages.d.ts +287 -0
  49. package/lib/cjs/types/LoadPages.d.ts.map +1 -0
  50. package/lib/cjs/types/Persistor.d.ts +187 -118
  51. package/lib/cjs/types/Persistor.d.ts.map +1 -1
  52. package/lib/cjs/types/ProcessPersistence.d.ts +197 -99
  53. package/lib/cjs/types/ProcessPersistence.d.ts.map +1 -1
  54. package/lib/cjs/types/Store.d.ts +2253 -1042
  55. package/lib/cjs/types/Store.d.ts.map +1 -1
  56. package/lib/cjs/types/StoreException.d.ts +97 -28
  57. package/lib/cjs/types/StoreException.d.ts.map +1 -1
  58. package/lib/cjs/types/StoreProvider.d.ts +80 -0
  59. package/lib/cjs/types/StoreProvider.d.ts.map +1 -0
  60. package/lib/cjs/types/UserException.d.ts +178 -169
  61. package/lib/cjs/types/UserException.d.ts.map +1 -1
  62. package/lib/cjs/types/index.d.ts +12 -9
  63. package/lib/cjs/types/index.d.ts.map +1 -1
  64. package/lib/cjs/types/server.d.ts +12 -0
  65. package/lib/cjs/types/server.d.ts.map +1 -0
  66. package/lib/cjs/types/utils.d.ts +15 -1
  67. package/lib/cjs/types/utils.d.ts.map +1 -1
  68. package/lib/cjs/utils.js +45 -7
  69. package/lib/esm/ClassPersistor.mjs +177 -0
  70. package/lib/esm/Esserializer/{DeserializationOptions.js → DeserializationOptions.mjs} +3 -3
  71. package/lib/esm/Esserializer/{SerializeOptions.js → SerializeOptions.mjs} +3 -3
  72. package/lib/esm/Esserializer/builtins.mjs +254 -0
  73. package/lib/esm/Esserializer/constant.mjs +28 -0
  74. package/lib/esm/Esserializer/deserializer.mjs +221 -0
  75. package/lib/esm/Esserializer/general.mjs +81 -0
  76. package/lib/esm/Esserializer/index.mjs +43 -0
  77. package/lib/esm/Esserializer/serializer.mjs +164 -0
  78. package/lib/esm/Evt.mjs +151 -0
  79. package/lib/esm/Hooks.mjs +1446 -0
  80. package/lib/esm/KissAction.mjs +4689 -0
  81. package/lib/esm/LoadPages.mjs +340 -0
  82. package/lib/esm/Persistor.mjs +180 -0
  83. package/lib/esm/ProcessPersistence.mjs +581 -0
  84. package/lib/esm/Store.mjs +4400 -0
  85. package/lib/esm/StoreException.mjs +153 -0
  86. package/lib/esm/StoreProvider.mjs +80 -0
  87. package/lib/esm/{UserException.js → UserException.mjs} +240 -246
  88. package/lib/esm/{Wait.js → Wait.mjs} +309 -309
  89. package/lib/esm/index.mjs +12 -9
  90. package/lib/esm/server.mjs +23 -0
  91. package/lib/esm/types/ClassPersistor.d.ts +143 -112
  92. package/lib/esm/types/ClassPersistor.d.ts.map +1 -1
  93. package/lib/esm/types/Esserializer/DeserializationOptions.d.ts +13 -6
  94. package/lib/esm/types/Esserializer/DeserializationOptions.d.ts.map +1 -1
  95. package/lib/esm/types/Esserializer/SerializeOptions.d.ts +11 -5
  96. package/lib/esm/types/Esserializer/SerializeOptions.d.ts.map +1 -1
  97. package/lib/esm/types/Esserializer/builtins.d.ts +36 -0
  98. package/lib/esm/types/Esserializer/builtins.d.ts.map +1 -0
  99. package/lib/esm/types/Esserializer/constant.d.ts +25 -49
  100. package/lib/esm/types/Esserializer/constant.d.ts.map +1 -1
  101. package/lib/esm/types/Esserializer/deserializer.d.ts +27 -17
  102. package/lib/esm/types/Esserializer/deserializer.d.ts.map +1 -1
  103. package/lib/esm/types/Esserializer/general.d.ts +28 -4
  104. package/lib/esm/types/Esserializer/general.d.ts.map +1 -1
  105. package/lib/esm/types/Esserializer/index.d.ts +33 -29
  106. package/lib/esm/types/Esserializer/index.d.ts.map +1 -1
  107. package/lib/esm/types/Esserializer/serializer.d.ts +14 -3
  108. package/lib/esm/types/Esserializer/serializer.d.ts.map +1 -1
  109. package/lib/esm/types/Evt.d.ts +124 -0
  110. package/lib/esm/types/Evt.d.ts.map +1 -0
  111. package/lib/esm/types/Hooks.d.ts +1174 -837
  112. package/lib/esm/types/Hooks.d.ts.map +1 -1
  113. package/lib/esm/types/KissAction.d.ts +5199 -1398
  114. package/lib/esm/types/KissAction.d.ts.map +1 -1
  115. package/lib/esm/types/LoadPages.d.ts +287 -0
  116. package/lib/esm/types/LoadPages.d.ts.map +1 -0
  117. package/lib/esm/types/Persistor.d.ts +187 -118
  118. package/lib/esm/types/Persistor.d.ts.map +1 -1
  119. package/lib/esm/types/ProcessPersistence.d.ts +197 -99
  120. package/lib/esm/types/ProcessPersistence.d.ts.map +1 -1
  121. package/lib/esm/types/Store.d.ts +2253 -1042
  122. package/lib/esm/types/Store.d.ts.map +1 -1
  123. package/lib/esm/types/StoreException.d.ts +97 -28
  124. package/lib/esm/types/StoreException.d.ts.map +1 -1
  125. package/lib/esm/types/StoreProvider.d.ts +80 -0
  126. package/lib/esm/types/StoreProvider.d.ts.map +1 -0
  127. package/lib/esm/types/UserException.d.ts +178 -169
  128. package/lib/esm/types/UserException.d.ts.map +1 -1
  129. package/lib/esm/types/index.d.ts +12 -9
  130. package/lib/esm/types/index.d.ts.map +1 -1
  131. package/lib/esm/types/server.d.ts +12 -0
  132. package/lib/esm/types/server.d.ts.map +1 -0
  133. package/lib/esm/types/utils.d.ts +15 -1
  134. package/lib/esm/types/utils.d.ts.map +1 -1
  135. package/lib/esm/utils.mjs +38 -0
  136. package/package.json +55 -31
  137. package/lib/cjs/UnmodifiableSetView.js +0 -53
  138. package/lib/cjs/types/UnmodifiableSetView.d.ts +0 -24
  139. package/lib/cjs/types/UnmodifiableSetView.d.ts.map +0 -1
  140. package/lib/esm/ClassPersistor.js +0 -112
  141. package/lib/esm/Esserializer/constant.js +0 -81
  142. package/lib/esm/Esserializer/deserializer.js +0 -307
  143. package/lib/esm/Esserializer/general.js +0 -34
  144. package/lib/esm/Esserializer/index.js +0 -39
  145. package/lib/esm/Esserializer/serializer.js +0 -130
  146. package/lib/esm/Hooks.js +0 -955
  147. package/lib/esm/KissAction.js +0 -1459
  148. package/lib/esm/Persistor.js +0 -113
  149. package/lib/esm/ProcessPersistence.js +0 -262
  150. package/lib/esm/Store.js +0 -1606
  151. package/lib/esm/StoreException.js +0 -69
  152. package/lib/esm/UnmodifiableSetView.js +0 -50
  153. package/lib/esm/types/UnmodifiableSetView.d.ts +0 -24
  154. package/lib/esm/types/UnmodifiableSetView.d.ts.map +0 -1
  155. package/lib/esm/utils.js +0 -3
@@ -1,75 +1,162 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ExceptionUtils = exports.TimeoutException = exports.StoreException = void 0;
4
- /**
5
- All exceptions thrown by Kiss are of type StoreException.
6
- */
7
- class StoreException extends Error {
8
- constructor(message) {
9
- super(message);
10
- this.name = 'StoreException';
11
- // Maintains proper stack trace for where our error was thrown (only available on V8).
12
- if (Error.captureStackTrace) {
13
- Error.captureStackTrace(this, StoreException);
14
- }
15
- }
16
- }
17
- exports.StoreException = StoreException;
18
- class TimeoutException extends Error {
19
- constructor(message) {
20
- super(message);
21
- this.name = 'TimeoutException';
22
- // Maintains proper stack trace for where our error was thrown (only available on V8).
23
- if (Error.captureStackTrace) {
24
- Error.captureStackTrace(this, StoreException);
25
- }
26
- }
27
- }
28
- exports.TimeoutException = TimeoutException;
29
- /**
30
- * The global default timeout for the wait functions (like `waitCondition` etc.) is 10 minutes.
31
- * This value is not final and can be modified.
32
- * To disable the timeout, modify this to a large value, like 300000000000 (almost 10 years).
33
- */
34
- TimeoutException.defaultTimeoutMillis = 60 * 1000 * 10;
35
- class ExceptionUtils {
36
- /**
37
- Cuts the stack trace at the given string, to avoid showing internal Kiss lines,
38
- which are not interesting to the user.
39
-
40
- IMPORTANT: This should only be called during development. V8 only computes it if someone
41
- actually reads the property, which improves performance dramatically for handable errors.
42
- When you use this, you'll pay the cost even if your caller doesn't need the stack.
43
-
44
- The given text will be found at the start of the line, like this: ` at ${text} `
45
- */
46
- static sanitizedStackTrace(error, text) {
47
- const stack = error.stack;
48
- if (stack === undefined)
49
- return error;
50
- const stackLines = stack.split('\n');
51
- let newStackStartIndex = -1;
52
- // Be aware that `constructor.name` cannot be used with bundlers that mangle
53
- // class or function names, because the error class name in the minified code
54
- // might be shortened.
55
- const errorLine = ` at ${text} `;
56
- // Find the index where the exception was dispatched.
57
- for (let i = 0; i < stackLines.length - 1; i++) {
58
- if (stackLines[i].startsWith(errorLine)) {
59
- newStackStartIndex = i + 1;
60
- break;
61
- }
62
- }
63
- if (newStackStartIndex !== -1) {
64
- // Reconstruct the stack trace from the desired start point
65
- // Also, include the error name and message as the first line
66
- const newStack = [
67
- `${error.name}: ${error.message}`,
68
- ...stackLines.slice(newStackStartIndex)
69
- ].join('\n');
70
- error.stack = newStack;
71
- }
72
- return error;
73
- }
74
- }
75
- exports.ExceptionUtils = ExceptionUtils;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ExceptionUtils = exports.TimeoutException = exports.AbortDispatchException = exports.StoreException = void 0;
4
+ exports.kissAbort = kissAbort;
5
+ exports.isKissAbort = isKissAbort;
6
+ /**
7
+ All exceptions thrown by Kiss are of type StoreException.
8
+ */
9
+ class StoreException extends Error {
10
+ constructor(message) {
11
+ super(message);
12
+ this.name = 'StoreException';
13
+ // Maintains proper stack trace for where our error was thrown (only available on V8).
14
+ if (Error.captureStackTrace) {
15
+ Error.captureStackTrace(this, StoreException);
16
+ }
17
+ }
18
+ }
19
+ exports.StoreException = StoreException;
20
+ /**
21
+ * If an action throws an `AbortDispatchException` from its `before` or `reduce` methods, the
22
+ * action aborts immediately, silently. Note the `after` method still runs, as usual.
23
+ *
24
+ * This is similar to throwing a `UserException`, but without showing any errors to the user,
25
+ * and without the action counting as failed:
26
+ *
27
+ * - The action status has `isDispatchAborted: true`, and the exception as its `originalError`.
28
+ * So `status.isCompletedOk` is `false`.
29
+ * - Like any other error, the exception is first given to the action's `wrapError`, and then to
30
+ * the `errorObserver`. They may return it unaltered (the default), turn it into another error,
31
+ * or swallow it by returning `null`. And if they return an `AbortDispatchException` for some
32
+ * other error, the action is aborted in the same way.
33
+ * - The action is not added to the failed actions, so `isFailed` is `false` for it.
34
+ * - It's never thrown by `dispatch`, and `dispatchAndWait` resolves with the action status,
35
+ * instead of rejecting.
36
+ * - It's not retried, if the action uses `retry`.
37
+ *
38
+ * You can use it in the `before` method to abort the action before the `reduce` method runs,
39
+ * after some ASYNC check. This is different from `abortDispatch()`, which must decide
40
+ * synchronously. For example:
41
+ *
42
+ * ```ts
43
+ * class LoadProfile extends KissAction<State> {
44
+ *
45
+ * async before() {
46
+ * if (await isProfileCached()) throw new AbortDispatchException();
47
+ * }
48
+ *
49
+ * async reduce() { ... }
50
+ * }
51
+ * ```
52
+ *
53
+ * Note Kiss itself also aborts actions with an `AbortDispatchException`: an `ignoreOld` action
54
+ * that became old, a `sequential` action discarded from its queue, an action with
55
+ * `checkInternet = { abort: true }` when there is no internet, and an action that stopped
56
+ * retrying. These are NOT given to `wrapError` or the `errorObserver`, since they didn't come
57
+ * from your code. So a `wrapError` that turns all errors into a `UserException` doesn't show
58
+ * them to the user.
59
+ *
60
+ * See also:
61
+ * - `abortDispatch()`, which is a way to abort the dispatch synchronously, before the action
62
+ * runs.
63
+ */
64
+ class AbortDispatchException extends Error {
65
+ constructor(message = 'Dispatch aborted.') {
66
+ super(message);
67
+ this.name = 'AbortDispatchException';
68
+ // Maintains proper stack trace for where our error was thrown (only available on V8).
69
+ if (Error.captureStackTrace) {
70
+ Error.captureStackTrace(this, AbortDispatchException);
71
+ }
72
+ }
73
+ }
74
+ exports.AbortDispatchException = AbortDispatchException;
75
+ // The `AbortDispatchException`s created by Kiss itself, and not by the user's code.
76
+ const _kissAborts = new WeakSet();
77
+ /**
78
+ * For Kiss internal use only.
79
+ * Creates an `AbortDispatchException` for an action that Kiss itself aborts. It's not given to
80
+ * the action's `wrapError` or to the `errorObserver`.
81
+ */
82
+ function kissAbort(message) {
83
+ const error = new AbortDispatchException(message);
84
+ _kissAborts.add(error);
85
+ return error;
86
+ }
87
+ /**
88
+ * For Kiss internal use only.
89
+ * Returns true if the error is an `AbortDispatchException` created by `kissAbort`.
90
+ */
91
+ function isKissAbort(error) {
92
+ return (error instanceof AbortDispatchException) && _kissAborts.has(error);
93
+ }
94
+ class TimeoutException extends Error {
95
+ constructor(message) {
96
+ super(message);
97
+ this.name = 'TimeoutException';
98
+ // Maintains proper stack trace for where our error was thrown (only available on V8).
99
+ if (Error.captureStackTrace) {
100
+ Error.captureStackTrace(this, TimeoutException);
101
+ }
102
+ }
103
+ }
104
+ exports.TimeoutException = TimeoutException;
105
+ /**
106
+ * The default timeout of the wait functions that are meant for tests only, like
107
+ * `waitActionCondition`, `waitAllActions`, `waitActionType`, `waitAllActionTypes` and
108
+ * `waitAnyActionTypeFinishes`. It's 3 seconds, which is shorter than the default test timeout
109
+ * of Jest and Vitest (5 seconds). This way, a wait that never completes fails the test with
110
+ * a clear `TimeoutException`, instead of the test runner's generic timeout error.
111
+ *
112
+ * You can change it globally, for example to 10 seconds, if your tests are slower:
113
+ *
114
+ * ```ts
115
+ * TimeoutException.defaultTimeoutMillis = 10_000;
116
+ * ```
117
+ *
118
+ * Note `waitCondition` and `dispatchWhen` have no default, since you may also use them in
119
+ * production. You must always give them a `timeoutMillis`.
120
+ */
121
+ TimeoutException.defaultTimeoutMillis = 3000;
122
+ class ExceptionUtils {
123
+ /**
124
+ Cuts the stack trace at the given string, to avoid showing internal Kiss lines,
125
+ which are not interesting to the user.
126
+
127
+ IMPORTANT: This should only be called during development. V8 only computes it if someone
128
+ actually reads the property, which improves performance dramatically for handable errors.
129
+ When you use this, you'll pay the cost even if your caller doesn't need the stack.
130
+
131
+ The given text will be found at the start of the line, like this: ` at ${text} `
132
+ */
133
+ static sanitizedStackTrace(error, text) {
134
+ const stack = error.stack;
135
+ if (stack === undefined)
136
+ return error;
137
+ const stackLines = stack.split('\n');
138
+ let newStackStartIndex = -1;
139
+ // Be aware that `constructor.name` cannot be used with bundlers that mangle
140
+ // class or function names, because the error class name in the minified code
141
+ // might be shortened.
142
+ const errorLine = ` at ${text} `;
143
+ // Find the index where the exception was dispatched.
144
+ for (let i = 0; i < stackLines.length - 1; i++) {
145
+ if (stackLines[i].startsWith(errorLine)) {
146
+ newStackStartIndex = i + 1;
147
+ break;
148
+ }
149
+ }
150
+ if (newStackStartIndex !== -1) {
151
+ // Reconstruct the stack trace from the desired start point
152
+ // Also, include the error name and message as the first line
153
+ const newStack = [
154
+ `${error.name}: ${error.message}`,
155
+ ...stackLines.slice(newStackStartIndex)
156
+ ].join('\n');
157
+ error.stack = newStack;
158
+ }
159
+ return error;
160
+ }
161
+ }
162
+ exports.ExceptionUtils = ExceptionUtils;
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ 'use client';
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.StoreContext = void 0;
8
+ exports.StoreProvider = StoreProvider;
9
+ exports.StoreSlice = StoreSlice;
10
+ const react_1 = __importDefault(require("react"));
11
+ const StoreException_1 = require("./StoreException");
12
+ // Read through the default import, not as named imports: under the `react-server` export
13
+ // condition, React's server build has no `createContext` or `useContext`, and Node's ESM loader
14
+ // rejects a named import that the module doesn't export, when it links the module. A React
15
+ // Server Components bundler never evaluates this module on the server (it's marked
16
+ // `'use client'`), but the package's server entry re-exports the components from here, and a
17
+ // plain Node import of it with that condition (the package's own tests) must load.
18
+ const { createContext, useContext, useEffect, useLayoutEffect, useMemo, useState } = react_1.default;
19
+ // Under the `react-server` export condition, React's server build has no `createContext`. A
20
+ // React Server Components bundler never evaluates this module on the server (it's marked
21
+ // `'use client'`, so its exports become client references), but the package's server entry
22
+ // re-exports the components from here, and a plain Node import of it with that condition (as in
23
+ // the package's own tests) must not throw.
24
+ exports.StoreContext = (typeof createContext === 'function')
25
+ ? createContext({ store: null, getServerState: () => undefined })
26
+ : null;
27
+ function StoreProvider(props) {
28
+ const { store, children } = props;
29
+ const serverState = ('serverState' in props) ? props.serverState : store._initialState;
30
+ const value = useMemo(() => ({ store, getServerState: () => serverState }), [store, serverState]);
31
+ return (react_1.default.createElement(exports.StoreContext.Provider, { value: value }, children));
32
+ }
33
+ // `useLayoutEffect` doesn't run on the server, and React 18 warns when it's rendered there.
34
+ const useIsomorphicLayoutEffect = (typeof window === 'undefined') ? useEffect : useLayoutEffect;
35
+ /**
36
+ * Merges the state the server loaded for a route into the store, in a server-rendered app with
37
+ * several routes (see "Server-side rendering" in the README). The store lives above the routes
38
+ * (the root layout in the Next.js app router, `_app.tsx` in the pages router), and each route's
39
+ * server code loads its own data. Render the route's content inside a `StoreSlice` with that
40
+ * data, in the Next.js app router:
41
+ *
42
+ * ```tsx
43
+ * // app/users/[id]/page.tsx, a server component.
44
+ * export default async function UserPage({ params }) {
45
+ * const user = await loadUser((await params).id);
46
+ * return <StoreSlice state={{ user }} key={user.id}><UserView /></StoreSlice>;
47
+ * }
48
+ * ```
49
+ *
50
+ * What it does:
51
+ * - On the server, it merges the slice into the request's store, and the components inside it
52
+ * render the merged state, so the HTML shows the route's data.
53
+ * - On the client, while hydrating, the components inside it hydrate against the same merged
54
+ * server state (no mismatch, even if the live store already differs), and the slice is merged
55
+ * into the live store before they render, so nothing changes under them after hydration.
56
+ * - On client-side navigation, the slice is merged into the live store before the children
57
+ * render, so the first render of the route already shows its data: no empty or stale frame.
58
+ * The other components (the layout) see the change right after the commit.
59
+ *
60
+ * The merge is `store.mergeServerState(slice)`: the `mergeServerState` option of `createStore`,
61
+ * or a shallow merge for plain-object states. The slice is merged once per mount: use `key`
62
+ * (the entity id, for example) to merge again when the route's data changes. If the store is
63
+ * not ready yet (a persistor still reading), the slice is merged again into the persisted
64
+ * state when it arrives.
65
+ */
66
+ function StoreSlice(props) {
67
+ const { state: slice, children } = props;
68
+ const parent = useContext(exports.StoreContext);
69
+ const store = parent.store;
70
+ if (store === null)
71
+ throw new StoreException_1.StoreException('StoreSlice must be used within a StoreProvider.');
72
+ // Once per mount, before the children render. The server state of the subtree is the
73
+ // parent's server state with the slice: what the server HTML shows for this route. The live
74
+ // store gets the slice now, silently: publishing it here, while React renders, would make
75
+ // React warn about updating the other components that use the store during a render. It's
76
+ // published after the commit, in the layout effect, so the other components catch up before
77
+ // the browser paints. (React calls this initializer twice in StrictMode: the second merge
78
+ // finds nothing to change, and publishes nothing.)
79
+ const [applied] = useState(() => ({
80
+ serverState: store._mergeSlice(parent.getServerState(), slice),
81
+ publish: store._mergeServerStateSilently(slice),
82
+ }));
83
+ useIsomorphicLayoutEffect(() => {
84
+ applied.publish();
85
+ }, [applied]);
86
+ const value = useMemo(() => ({ store, getServerState: () => applied.serverState }), [store, applied]);
87
+ return (react_1.default.createElement(exports.StoreContext.Provider, { value: value }, children));
88
+ }