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.
- package/README.md +699 -156
- package/lib/cjs/ClassPersistor.js +177 -112
- package/lib/cjs/Esserializer/DeserializationOptions.js +4 -4
- package/lib/cjs/Esserializer/SerializeOptions.js +4 -4
- package/lib/cjs/Esserializer/builtins.js +258 -0
- package/lib/cjs/Esserializer/constant.js +39 -131
- package/lib/cjs/Esserializer/deserializer.js +226 -313
- package/lib/cjs/Esserializer/general.js +91 -39
- package/lib/cjs/Esserializer/index.js +47 -43
- package/lib/cjs/Esserializer/serializer.js +168 -133
- package/lib/cjs/Evt.js +132 -0
- package/lib/cjs/Hooks.js +1472 -969
- package/lib/cjs/KissAction.js +4635 -1333
- package/lib/cjs/LoadPages.js +336 -0
- package/lib/cjs/Persistor.js +189 -120
- package/lib/cjs/ProcessPersistence.js +579 -262
- package/lib/cjs/Store.js +4278 -1534
- package/lib/cjs/StoreException.js +162 -75
- package/lib/cjs/StoreProvider.js +88 -0
- package/lib/cjs/UserException.js +212 -218
- package/lib/cjs/Wait.js +309 -309
- package/lib/cjs/index.js +69 -42
- package/lib/cjs/server.js +54 -0
- package/lib/cjs/types/ClassPersistor.d.ts +143 -112
- package/lib/cjs/types/ClassPersistor.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/DeserializationOptions.d.ts +13 -6
- package/lib/cjs/types/Esserializer/DeserializationOptions.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/SerializeOptions.d.ts +11 -5
- package/lib/cjs/types/Esserializer/SerializeOptions.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/builtins.d.ts +36 -0
- package/lib/cjs/types/Esserializer/builtins.d.ts.map +1 -0
- package/lib/cjs/types/Esserializer/constant.d.ts +25 -49
- package/lib/cjs/types/Esserializer/constant.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/deserializer.d.ts +27 -17
- package/lib/cjs/types/Esserializer/deserializer.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/general.d.ts +28 -4
- package/lib/cjs/types/Esserializer/general.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/index.d.ts +33 -29
- package/lib/cjs/types/Esserializer/index.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/serializer.d.ts +14 -3
- package/lib/cjs/types/Esserializer/serializer.d.ts.map +1 -1
- package/lib/cjs/types/Evt.d.ts +124 -0
- package/lib/cjs/types/Evt.d.ts.map +1 -0
- package/lib/cjs/types/Hooks.d.ts +1174 -837
- package/lib/cjs/types/Hooks.d.ts.map +1 -1
- package/lib/cjs/types/KissAction.d.ts +5199 -1398
- package/lib/cjs/types/KissAction.d.ts.map +1 -1
- package/lib/cjs/types/LoadPages.d.ts +287 -0
- package/lib/cjs/types/LoadPages.d.ts.map +1 -0
- package/lib/cjs/types/Persistor.d.ts +187 -118
- package/lib/cjs/types/Persistor.d.ts.map +1 -1
- package/lib/cjs/types/ProcessPersistence.d.ts +197 -99
- package/lib/cjs/types/ProcessPersistence.d.ts.map +1 -1
- package/lib/cjs/types/Store.d.ts +2253 -1042
- package/lib/cjs/types/Store.d.ts.map +1 -1
- package/lib/cjs/types/StoreException.d.ts +97 -28
- package/lib/cjs/types/StoreException.d.ts.map +1 -1
- package/lib/cjs/types/StoreProvider.d.ts +80 -0
- package/lib/cjs/types/StoreProvider.d.ts.map +1 -0
- package/lib/cjs/types/UserException.d.ts +178 -169
- package/lib/cjs/types/UserException.d.ts.map +1 -1
- package/lib/cjs/types/index.d.ts +12 -9
- package/lib/cjs/types/index.d.ts.map +1 -1
- package/lib/cjs/types/server.d.ts +12 -0
- package/lib/cjs/types/server.d.ts.map +1 -0
- package/lib/cjs/types/utils.d.ts +15 -1
- package/lib/cjs/types/utils.d.ts.map +1 -1
- package/lib/cjs/utils.js +45 -7
- package/lib/esm/ClassPersistor.mjs +177 -0
- package/lib/esm/Esserializer/{DeserializationOptions.js → DeserializationOptions.mjs} +3 -3
- package/lib/esm/Esserializer/{SerializeOptions.js → SerializeOptions.mjs} +3 -3
- package/lib/esm/Esserializer/builtins.mjs +254 -0
- package/lib/esm/Esserializer/constant.mjs +28 -0
- package/lib/esm/Esserializer/deserializer.mjs +221 -0
- package/lib/esm/Esserializer/general.mjs +81 -0
- package/lib/esm/Esserializer/index.mjs +43 -0
- package/lib/esm/Esserializer/serializer.mjs +164 -0
- package/lib/esm/Evt.mjs +151 -0
- package/lib/esm/Hooks.mjs +1446 -0
- package/lib/esm/KissAction.mjs +4689 -0
- package/lib/esm/LoadPages.mjs +340 -0
- package/lib/esm/Persistor.mjs +180 -0
- package/lib/esm/ProcessPersistence.mjs +581 -0
- package/lib/esm/Store.mjs +4400 -0
- package/lib/esm/StoreException.mjs +153 -0
- package/lib/esm/StoreProvider.mjs +80 -0
- package/lib/esm/{UserException.js → UserException.mjs} +240 -246
- package/lib/esm/{Wait.js → Wait.mjs} +309 -309
- package/lib/esm/index.mjs +12 -9
- package/lib/esm/server.mjs +23 -0
- package/lib/esm/types/ClassPersistor.d.ts +143 -112
- package/lib/esm/types/ClassPersistor.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/DeserializationOptions.d.ts +13 -6
- package/lib/esm/types/Esserializer/DeserializationOptions.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/SerializeOptions.d.ts +11 -5
- package/lib/esm/types/Esserializer/SerializeOptions.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/builtins.d.ts +36 -0
- package/lib/esm/types/Esserializer/builtins.d.ts.map +1 -0
- package/lib/esm/types/Esserializer/constant.d.ts +25 -49
- package/lib/esm/types/Esserializer/constant.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/deserializer.d.ts +27 -17
- package/lib/esm/types/Esserializer/deserializer.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/general.d.ts +28 -4
- package/lib/esm/types/Esserializer/general.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/index.d.ts +33 -29
- package/lib/esm/types/Esserializer/index.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/serializer.d.ts +14 -3
- package/lib/esm/types/Esserializer/serializer.d.ts.map +1 -1
- package/lib/esm/types/Evt.d.ts +124 -0
- package/lib/esm/types/Evt.d.ts.map +1 -0
- package/lib/esm/types/Hooks.d.ts +1174 -837
- package/lib/esm/types/Hooks.d.ts.map +1 -1
- package/lib/esm/types/KissAction.d.ts +5199 -1398
- package/lib/esm/types/KissAction.d.ts.map +1 -1
- package/lib/esm/types/LoadPages.d.ts +287 -0
- package/lib/esm/types/LoadPages.d.ts.map +1 -0
- package/lib/esm/types/Persistor.d.ts +187 -118
- package/lib/esm/types/Persistor.d.ts.map +1 -1
- package/lib/esm/types/ProcessPersistence.d.ts +197 -99
- package/lib/esm/types/ProcessPersistence.d.ts.map +1 -1
- package/lib/esm/types/Store.d.ts +2253 -1042
- package/lib/esm/types/Store.d.ts.map +1 -1
- package/lib/esm/types/StoreException.d.ts +97 -28
- package/lib/esm/types/StoreException.d.ts.map +1 -1
- package/lib/esm/types/StoreProvider.d.ts +80 -0
- package/lib/esm/types/StoreProvider.d.ts.map +1 -0
- package/lib/esm/types/UserException.d.ts +178 -169
- package/lib/esm/types/UserException.d.ts.map +1 -1
- package/lib/esm/types/index.d.ts +12 -9
- package/lib/esm/types/index.d.ts.map +1 -1
- package/lib/esm/types/server.d.ts +12 -0
- package/lib/esm/types/server.d.ts.map +1 -0
- package/lib/esm/types/utils.d.ts +15 -1
- package/lib/esm/types/utils.d.ts.map +1 -1
- package/lib/esm/utils.mjs +38 -0
- package/package.json +55 -31
- package/lib/cjs/UnmodifiableSetView.js +0 -53
- package/lib/cjs/types/UnmodifiableSetView.d.ts +0 -24
- package/lib/cjs/types/UnmodifiableSetView.d.ts.map +0 -1
- package/lib/esm/ClassPersistor.js +0 -112
- package/lib/esm/Esserializer/constant.js +0 -81
- package/lib/esm/Esserializer/deserializer.js +0 -307
- package/lib/esm/Esserializer/general.js +0 -34
- package/lib/esm/Esserializer/index.js +0 -39
- package/lib/esm/Esserializer/serializer.js +0 -130
- package/lib/esm/Hooks.js +0 -955
- package/lib/esm/KissAction.js +0 -1459
- package/lib/esm/Persistor.js +0 -113
- package/lib/esm/ProcessPersistence.js +0 -262
- package/lib/esm/Store.js +0 -1606
- package/lib/esm/StoreException.js +0 -69
- package/lib/esm/UnmodifiableSetView.js +0 -50
- package/lib/esm/types/UnmodifiableSetView.d.ts +0 -24
- package/lib/esm/types/UnmodifiableSetView.d.ts.map +0 -1
- package/lib/esm/utils.js +0 -3
package/lib/cjs/types/Store.d.ts
CHANGED
|
@@ -1,1043 +1,2254 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
*
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
*
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
* ```
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
*
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
* }
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
*
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
*
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
*
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
-
*
|
|
289
|
-
*
|
|
290
|
-
*
|
|
291
|
-
*
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
*
|
|
310
|
-
*
|
|
311
|
-
*
|
|
312
|
-
*
|
|
313
|
-
*
|
|
314
|
-
*
|
|
315
|
-
*
|
|
316
|
-
*
|
|
317
|
-
*
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
*
|
|
322
|
-
*
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
*
|
|
330
|
-
*
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
*
|
|
334
|
-
*
|
|
335
|
-
*
|
|
336
|
-
*
|
|
337
|
-
*
|
|
338
|
-
*
|
|
339
|
-
*
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
*
|
|
344
|
-
*
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
* The
|
|
354
|
-
*
|
|
355
|
-
*
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
359
|
-
*
|
|
360
|
-
*
|
|
361
|
-
*
|
|
362
|
-
*
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
*
|
|
367
|
-
*
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
*
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
*
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
*
|
|
387
|
-
*
|
|
388
|
-
*
|
|
389
|
-
*
|
|
390
|
-
*
|
|
391
|
-
*
|
|
392
|
-
*
|
|
393
|
-
*
|
|
394
|
-
*
|
|
395
|
-
*
|
|
396
|
-
*
|
|
397
|
-
*
|
|
398
|
-
*
|
|
399
|
-
*
|
|
400
|
-
*
|
|
401
|
-
*
|
|
402
|
-
*
|
|
403
|
-
*
|
|
404
|
-
*
|
|
405
|
-
*
|
|
406
|
-
*
|
|
407
|
-
*
|
|
408
|
-
*
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
*
|
|
413
|
-
*
|
|
414
|
-
*
|
|
415
|
-
*
|
|
416
|
-
*
|
|
417
|
-
*
|
|
418
|
-
*
|
|
419
|
-
*
|
|
420
|
-
* store.
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
*
|
|
424
|
-
*
|
|
425
|
-
*
|
|
426
|
-
*
|
|
427
|
-
*
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
*
|
|
459
|
-
*
|
|
460
|
-
*
|
|
461
|
-
*
|
|
462
|
-
*
|
|
463
|
-
* - `
|
|
464
|
-
* - `
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
*
|
|
469
|
-
*
|
|
470
|
-
*
|
|
471
|
-
*
|
|
472
|
-
*
|
|
473
|
-
*
|
|
474
|
-
*
|
|
475
|
-
*
|
|
476
|
-
*
|
|
477
|
-
*
|
|
478
|
-
*
|
|
479
|
-
*
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
*
|
|
530
|
-
*
|
|
531
|
-
*
|
|
532
|
-
*
|
|
533
|
-
*
|
|
534
|
-
*
|
|
535
|
-
*
|
|
536
|
-
*
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
*
|
|
541
|
-
*
|
|
542
|
-
*
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
*
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
*
|
|
566
|
-
*
|
|
567
|
-
*
|
|
568
|
-
*
|
|
569
|
-
*
|
|
570
|
-
*
|
|
571
|
-
*
|
|
572
|
-
*
|
|
573
|
-
*
|
|
574
|
-
*
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
*
|
|
579
|
-
*
|
|
580
|
-
*
|
|
581
|
-
*
|
|
582
|
-
*
|
|
583
|
-
*
|
|
584
|
-
*
|
|
585
|
-
*
|
|
586
|
-
*
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
*
|
|
593
|
-
*
|
|
594
|
-
*
|
|
595
|
-
*
|
|
596
|
-
*
|
|
597
|
-
*
|
|
598
|
-
*
|
|
599
|
-
*
|
|
600
|
-
*
|
|
601
|
-
*
|
|
602
|
-
*
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
*
|
|
609
|
-
*
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
*
|
|
616
|
-
*
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
*
|
|
628
|
-
*
|
|
629
|
-
*
|
|
630
|
-
* ```ts
|
|
631
|
-
* store.
|
|
632
|
-
*
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
*
|
|
639
|
-
*
|
|
640
|
-
*
|
|
641
|
-
*
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
*
|
|
658
|
-
*
|
|
659
|
-
*
|
|
660
|
-
*
|
|
661
|
-
*
|
|
662
|
-
*
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
*
|
|
672
|
-
*
|
|
673
|
-
*
|
|
674
|
-
*
|
|
675
|
-
*
|
|
676
|
-
*
|
|
677
|
-
*
|
|
678
|
-
* and
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
*
|
|
683
|
-
*
|
|
684
|
-
*
|
|
685
|
-
*
|
|
686
|
-
*
|
|
687
|
-
*
|
|
688
|
-
*
|
|
689
|
-
*
|
|
690
|
-
*
|
|
691
|
-
* Note:
|
|
692
|
-
*
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
*
|
|
697
|
-
*
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
*
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
*
|
|
706
|
-
*
|
|
707
|
-
*
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
*
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
*
|
|
720
|
-
*
|
|
721
|
-
*
|
|
722
|
-
*
|
|
723
|
-
*
|
|
724
|
-
*
|
|
725
|
-
*
|
|
726
|
-
* This
|
|
727
|
-
*
|
|
728
|
-
*
|
|
729
|
-
*
|
|
730
|
-
*
|
|
731
|
-
*
|
|
732
|
-
*
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
*
|
|
737
|
-
*
|
|
738
|
-
*
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
*
|
|
743
|
-
*
|
|
744
|
-
*
|
|
745
|
-
*
|
|
746
|
-
*
|
|
747
|
-
*
|
|
748
|
-
*
|
|
749
|
-
*
|
|
750
|
-
*
|
|
751
|
-
*
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
*
|
|
759
|
-
*
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
*
|
|
764
|
-
*
|
|
765
|
-
*
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
*
|
|
770
|
-
*
|
|
771
|
-
*
|
|
772
|
-
*
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
*
|
|
777
|
-
*
|
|
778
|
-
*
|
|
779
|
-
*
|
|
780
|
-
*
|
|
781
|
-
*
|
|
782
|
-
*
|
|
783
|
-
*
|
|
784
|
-
*
|
|
785
|
-
*
|
|
786
|
-
* `
|
|
787
|
-
*
|
|
788
|
-
*
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
*
|
|
803
|
-
*
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
*
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
*
|
|
837
|
-
*
|
|
838
|
-
*
|
|
839
|
-
*
|
|
840
|
-
*
|
|
841
|
-
*
|
|
842
|
-
*
|
|
843
|
-
*
|
|
844
|
-
*
|
|
845
|
-
*
|
|
846
|
-
*
|
|
847
|
-
*
|
|
848
|
-
*
|
|
849
|
-
*
|
|
850
|
-
*
|
|
851
|
-
*
|
|
852
|
-
*
|
|
853
|
-
*
|
|
854
|
-
*
|
|
855
|
-
*
|
|
856
|
-
*
|
|
857
|
-
*
|
|
858
|
-
*
|
|
859
|
-
*
|
|
860
|
-
*
|
|
861
|
-
*
|
|
862
|
-
*
|
|
863
|
-
*
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
*
|
|
874
|
-
*
|
|
875
|
-
*
|
|
876
|
-
*
|
|
877
|
-
*
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
*
|
|
908
|
-
*
|
|
909
|
-
*
|
|
910
|
-
*
|
|
911
|
-
*
|
|
912
|
-
*
|
|
913
|
-
*
|
|
914
|
-
*
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
*
|
|
924
|
-
* (
|
|
925
|
-
*
|
|
926
|
-
*
|
|
927
|
-
*
|
|
928
|
-
*
|
|
929
|
-
*
|
|
930
|
-
*
|
|
931
|
-
*
|
|
932
|
-
*
|
|
933
|
-
*
|
|
934
|
-
*
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
*
|
|
941
|
-
*
|
|
942
|
-
*
|
|
943
|
-
*
|
|
944
|
-
*
|
|
945
|
-
*
|
|
946
|
-
*
|
|
947
|
-
*
|
|
948
|
-
*
|
|
949
|
-
*
|
|
950
|
-
*
|
|
951
|
-
*
|
|
952
|
-
*
|
|
953
|
-
*
|
|
954
|
-
* `
|
|
955
|
-
* `
|
|
956
|
-
* `
|
|
957
|
-
*
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
*
|
|
968
|
-
*
|
|
969
|
-
*
|
|
970
|
-
*
|
|
971
|
-
*
|
|
972
|
-
*
|
|
973
|
-
*
|
|
974
|
-
*
|
|
975
|
-
*
|
|
976
|
-
*
|
|
977
|
-
*
|
|
978
|
-
*
|
|
979
|
-
*
|
|
980
|
-
*
|
|
981
|
-
*
|
|
982
|
-
*
|
|
983
|
-
*
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
*
|
|
988
|
-
*
|
|
989
|
-
*
|
|
990
|
-
*
|
|
991
|
-
*
|
|
992
|
-
*
|
|
993
|
-
*
|
|
994
|
-
*
|
|
995
|
-
*
|
|
996
|
-
*
|
|
997
|
-
* ```
|
|
998
|
-
*
|
|
999
|
-
*
|
|
1000
|
-
*
|
|
1001
|
-
*
|
|
1002
|
-
*
|
|
1003
|
-
*
|
|
1004
|
-
*
|
|
1005
|
-
*
|
|
1006
|
-
*
|
|
1007
|
-
*
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
*
|
|
1016
|
-
*
|
|
1017
|
-
*
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
*
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1
|
+
import { UserException } from './UserException';
|
|
2
|
+
import { Persistor } from './Persistor';
|
|
3
|
+
import { ActionStatus, KissAction, StatusOptions } from './KissAction';
|
|
4
|
+
import { Evt, EvtState } from './Evt';
|
|
5
|
+
/** One failure kept for `isFailed` and `exceptionFor`: the action's `statusKey` and its error. */
|
|
6
|
+
export type Failure = {
|
|
7
|
+
statusKey: unknown;
|
|
8
|
+
error: unknown;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* A function that disposes the value of a store prop, given to `setProp` as the `dispose`
|
|
12
|
+
* option. For example, `clearInterval` for an interval. It may return a promise, which
|
|
13
|
+
* `store.dispose()` waits for.
|
|
14
|
+
*/
|
|
15
|
+
export type PropDispose<V = any> = (value: V) => unknown;
|
|
16
|
+
/**
|
|
17
|
+
* The options of `store.setProp` and `KissAction.setProp`.
|
|
18
|
+
*/
|
|
19
|
+
export interface SetPropOptions<V = any> {
|
|
20
|
+
/**
|
|
21
|
+
* How to dispose the value, when the prop is disposed (with `disposeProp`, `disposeProps`
|
|
22
|
+
* or `store.dispose()`), or when it's replaced by `setProp` with the same key. It gets the
|
|
23
|
+
* value. For example, `{ dispose: clearInterval }` for an interval, or
|
|
24
|
+
* `{ dispose: (unsubscribe) => unsubscribe() }` for an unsubscribe function. It may return a
|
|
25
|
+
* promise, which `store.dispose()` waits for.
|
|
26
|
+
*
|
|
27
|
+
* Without it, Kiss disposes the value by its type (see `setProp`). Timers and functions need
|
|
28
|
+
* it, since Kiss can't tell them from other numbers and functions.
|
|
29
|
+
*/
|
|
30
|
+
dispose?: PropDispose<V>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Selects the props to dispose, for `disposeProps` and `logOut`. Gets the key and the value
|
|
34
|
+
* of each prop, and returns `true` for the ones to dispose.
|
|
35
|
+
*/
|
|
36
|
+
export type PropPredicate = (key: unknown, value: unknown) => boolean;
|
|
37
|
+
interface ConstructorParams<St> {
|
|
38
|
+
/**
|
|
39
|
+
* The initial state of the app.
|
|
40
|
+
*/
|
|
41
|
+
initialState: St;
|
|
42
|
+
/**
|
|
43
|
+
* The environment the app is running in: production, staging, testing, etc. Any value, and
|
|
44
|
+
* usually an enum. Read it with `store.environment`, in actions with
|
|
45
|
+
* `this.store.environment`, and in components with `useEnvironment()`. It never changes
|
|
46
|
+
* while the app runs.
|
|
47
|
+
*
|
|
48
|
+
* ```ts
|
|
49
|
+
* const store = createStore<State>({
|
|
50
|
+
* initialState: State.initialState,
|
|
51
|
+
* environment: Environment.production,
|
|
52
|
+
* });
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* See also: `configuration` and `dependencies`, which are created by factories that get
|
|
56
|
+
* the store, so they can depend on the environment.
|
|
57
|
+
*/
|
|
58
|
+
environment?: unknown;
|
|
59
|
+
/**
|
|
60
|
+
* A factory that creates the configuration of the app: feature flags and other
|
|
61
|
+
* configuration values. It gets the store, so it can read `store.environment`. It's called
|
|
62
|
+
* once, when the store is created, before the `dependencies` factory. Read the result with
|
|
63
|
+
* `store.configuration`, in actions with `this.store.configuration`, and in components with
|
|
64
|
+
* `useConfiguration()`. It's meant to be immutable: it doesn't change while the app runs.
|
|
65
|
+
*
|
|
66
|
+
* ```ts
|
|
67
|
+
* const store = createStore<State>({
|
|
68
|
+
* initialState: State.initialState,
|
|
69
|
+
* environment: Environment.production,
|
|
70
|
+
* configuration: (store) => new Config({ showAdminConsole: store.environment !== Environment.production }),
|
|
71
|
+
* });
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
74
|
+
* `store.dispose()` disposes the configuration, if it has a `[Symbol.asyncDispose]()`,
|
|
75
|
+
* `[Symbol.dispose]()` or `dispose()` method.
|
|
76
|
+
*/
|
|
77
|
+
configuration?: (store: Store<St>) => unknown;
|
|
78
|
+
/**
|
|
79
|
+
* A factory that creates the dependencies of the app: the services, repositories, API
|
|
80
|
+
* clients, etc, that the actions use. It gets the store, so it can read `store.environment`
|
|
81
|
+
* and `store.configuration` (which is created first). It's called once, when the store is
|
|
82
|
+
* created. Read the result with `store.dependencies`, and in actions with
|
|
83
|
+
* `this.store.dependencies`. Components should usually not need the dependencies.
|
|
84
|
+
*
|
|
85
|
+
* Since the dependencies belong to the store, each test creates its own store with its own
|
|
86
|
+
* dependencies (fakes, for example), with nothing global to set up or reset:
|
|
87
|
+
*
|
|
88
|
+
* ```ts
|
|
89
|
+
* const store = createStore<State>({
|
|
90
|
+
* initialState: State.initialState,
|
|
91
|
+
* environment: Environment.testing,
|
|
92
|
+
* dependencies: (store) => new FakeDependencies(),
|
|
93
|
+
* });
|
|
94
|
+
* ```
|
|
95
|
+
*
|
|
96
|
+
* `store.environment`, `store.configuration` and `store.dependencies` are typed `unknown`.
|
|
97
|
+
* Declare typed getters once, in your base action:
|
|
98
|
+
*
|
|
99
|
+
* ```ts
|
|
100
|
+
* abstract class Action extends KissAction<State> {
|
|
101
|
+
* get env() { return this.store.environment as Environment; }
|
|
102
|
+
* get config() { return this.store.configuration as Config; }
|
|
103
|
+
* get deps() { return this.store.dependencies as Dependencies; }
|
|
104
|
+
* }
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* `store.dispose()` disposes the dependencies, if they have a `[Symbol.asyncDispose]()`,
|
|
108
|
+
* `[Symbol.dispose]()` or `dispose()` method. They are disposed after the props, and before
|
|
109
|
+
* the configuration. `logOut` keeps them, since they belong to the app, and not to the user.
|
|
110
|
+
*/
|
|
111
|
+
dependencies?: (store: Store<St>) => unknown;
|
|
112
|
+
/**
|
|
113
|
+
* Kiss calls this function automatically when actions throw `UserException`s, so that
|
|
114
|
+
* they can be shown to the user. Usually, this function opens some UI like a dialog or a toast,
|
|
115
|
+
* with the error message.
|
|
116
|
+
*
|
|
117
|
+
* The `count` parameter is the number of exceptions still in the queue.
|
|
118
|
+
*
|
|
119
|
+
* You should explicitly call `next` function when the user is ready to see the next exception
|
|
120
|
+
* in the queue, when the user dismisses the dialog or toast. If there are no more exceptions,
|
|
121
|
+
* `next` will do nothing. Otherwise, it will call `showUserException` again. Example:
|
|
122
|
+
*
|
|
123
|
+
* ```ts
|
|
124
|
+
* const showUserException: ShowUserException =
|
|
125
|
+
* (exception, count, next) => {
|
|
126
|
+
* Alert.alert(
|
|
127
|
+
* exception.title || exception.message,
|
|
128
|
+
* exception.title ? exception.message : '',
|
|
129
|
+
* [{ text: 'OK', onPress: (_value?: string) => next() }]
|
|
130
|
+
* );
|
|
131
|
+
* };
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
showUserException?: (exception: UserException, count: number, next: () => void) => void;
|
|
135
|
+
/**
|
|
136
|
+
* The persistor saves and retrieves the application's state from the local device storage,
|
|
137
|
+
* ensuring data persistence across app restarts. Without a defined persistor, the app's state
|
|
138
|
+
* will not be saved.
|
|
139
|
+
*
|
|
140
|
+
* The `Persistor` is an abstract base class, allowing developers to easily craft their custom
|
|
141
|
+
* persistors. Kiss makes it easy, by automatically invoking the persistor upon any state
|
|
142
|
+
* modification, and passing it the information that needs to be saved as well as the last saved
|
|
143
|
+
* state.
|
|
144
|
+
*
|
|
145
|
+
* Kiss also offers a built-in `ClassPersistor` that can be used to persist the state by
|
|
146
|
+
* serializing it to a string and saving it to the local device storage.
|
|
147
|
+
*
|
|
148
|
+
* A persistor is not allowed in a server store (see the `server` option): the store creation
|
|
149
|
+
* throws a `StoreException`.
|
|
150
|
+
*/
|
|
151
|
+
persistor?: Persistor<St>;
|
|
152
|
+
/**
|
|
153
|
+
* How `store.mergeServerState(slice)` and the `StoreSlice` component merge a slice of
|
|
154
|
+
* state, loaded on the server for a route, into the store state (see "Server-side rendering"
|
|
155
|
+
* in the README). It gets the current state and the slice, and returns the new state. Return
|
|
156
|
+
* the same state when nothing changes, so that nothing is published.
|
|
157
|
+
*
|
|
158
|
+
* Without it, the state must be a plain object, and the merge is shallow:
|
|
159
|
+
* `{ ...state, ...slice }`. A class-based state needs this option, where its deserialization
|
|
160
|
+
* also goes, since a slice is a plain object (in Next.js, it's a prop that a server component
|
|
161
|
+
* gives to a client component, so it must be serializable):
|
|
162
|
+
*
|
|
163
|
+
* ```ts
|
|
164
|
+
* mergeServerState: (state, slice: { user?: UserJson }) =>
|
|
165
|
+
* slice.user ? state.withUser(User.fromJson(slice.user)) : state,
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
mergeServerState?: (state: St, slice: any) => St;
|
|
169
|
+
/**
|
|
170
|
+
* The `logger` is a function that Kiss uses when it calls `Store.log()`
|
|
171
|
+
* to log information. It's set up during the creation of the store. For example:
|
|
172
|
+
*
|
|
173
|
+
* ```ts
|
|
174
|
+
* const store = new Store<State>({
|
|
175
|
+
* initialState: new State(),
|
|
176
|
+
* logger: (obj) => process.stdout.write(obj + '\n'),
|
|
177
|
+
* });
|
|
178
|
+
* ```
|
|
179
|
+
*
|
|
180
|
+
* Or using a logger library:
|
|
181
|
+
*
|
|
182
|
+
* ```ts
|
|
183
|
+
* import { Logger } from 'my-logger-library';
|
|
184
|
+
*
|
|
185
|
+
* const store = new Store<State>({
|
|
186
|
+
* initialState: new State(),
|
|
187
|
+
* logger: (obj) => Logger.log(obj),
|
|
188
|
+
* });
|
|
189
|
+
* ```
|
|
190
|
+
*
|
|
191
|
+
* Note, if you don't define a logger yourself, the default is to print
|
|
192
|
+
* the log messages to the console with `console.log()`.
|
|
193
|
+
*
|
|
194
|
+
* This is how you may completely turn logging off:
|
|
195
|
+
*
|
|
196
|
+
* ```ts
|
|
197
|
+
* const store = new Store<State>({
|
|
198
|
+
* initialState: new State(),
|
|
199
|
+
* logger: null
|
|
200
|
+
* });
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
logger?: ((obj: any) => void) | null;
|
|
204
|
+
/**
|
|
205
|
+
* If `true`, will use Store.log() to log all state changes.
|
|
206
|
+
* It uses `Store.describeStateChange()` to create the description of those changes.
|
|
207
|
+
*
|
|
208
|
+
* Note this is true (turned on) by default, as it may be useful for development, testing and
|
|
209
|
+
* debugging. Be sure to turn it off in production, as it may slow down your application:
|
|
210
|
+
*
|
|
211
|
+
* ```ts
|
|
212
|
+
* // In production
|
|
213
|
+
* const store = new Store<State>({
|
|
214
|
+
* initialState: new State(),
|
|
215
|
+
* logStateChanges: false,
|
|
216
|
+
* });
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
219
|
+
logStateChanges?: boolean;
|
|
220
|
+
/**
|
|
221
|
+
* An `errorObserver` can be set during the `Store` creation. It's a global way to
|
|
222
|
+
* observe, change and swallow errors. It's given all errors thrown by your actions (including
|
|
223
|
+
* those of type `UserException`), and also the errors of the `Persistor`.
|
|
224
|
+
*
|
|
225
|
+
* It's called with an object with these fields:
|
|
226
|
+
*
|
|
227
|
+
* - `error`: The error, AFTER the action's `wrapError` (or the persistor's `wrapError`).
|
|
228
|
+
* - `originalError`: The error BEFORE `wrapError`.
|
|
229
|
+
* - `action`: The action that threw the error, or `null` if the error didn't come from an
|
|
230
|
+
* action (for example, if it came from the `Persistor`). Always check for `null`.
|
|
231
|
+
* - `store`: Use it to read the state, if it's relevant to the error. Do NOT use it to
|
|
232
|
+
* dispatch actions, as this may have unpredictable results.
|
|
233
|
+
*
|
|
234
|
+
* It must return the error to be used:
|
|
235
|
+
*
|
|
236
|
+
* - If it returns the same `error` unaltered, this original error will be used.
|
|
237
|
+
* - If it returns something else, that will be used instead of `error`.
|
|
238
|
+
* - If it returns `null` (or `undefined`, or nothing), the error is disabled (swallowed).
|
|
239
|
+
*
|
|
240
|
+
* IMPORTANT: If instead of RETURNING an error you THROW an error inside the
|
|
241
|
+
* `errorObserver`, Kiss will catch it and use it instead of `error`. In other words,
|
|
242
|
+
* returning an error or throwing an error works the same way. But it's recommended that you
|
|
243
|
+
* return the error.
|
|
244
|
+
*
|
|
245
|
+
* Then, for the errors thrown by actions:
|
|
246
|
+
* - A `UserException` is shown to the user (in the `showUserException` UI), and is not thrown.
|
|
247
|
+
* - An `AbortDispatchException` aborts the action silently (see `AbortDispatchException`).
|
|
248
|
+
* Note the ones Kiss itself uses to abort actions (for example, for `ignoreOld` or
|
|
249
|
+
* `sequential`) are not given to the `errorObserver`.
|
|
250
|
+
* - Any other error is thrown by `dispatch` (or rejects `dispatchAndWait`).
|
|
251
|
+
*
|
|
252
|
+
* And for the errors of the `Persistor` (thrown by `persistDifference` after the persistor's
|
|
253
|
+
* `wrapError`, thrown while reading the persisted state, or added with `Persistor.addError`):
|
|
254
|
+
* - A `UserException` is shown to the user.
|
|
255
|
+
* - Any other error is thrown as an unhandled promise rejection, since there is no `dispatch`
|
|
256
|
+
* call to throw it to. Return `null` if you don't want that, for example after logging it.
|
|
257
|
+
*
|
|
258
|
+
* Without an `errorObserver`, all errors are used unaltered.
|
|
259
|
+
*
|
|
260
|
+
* Note the `errorObserver` is called AFTER the action's `wrapError`. It's not called if
|
|
261
|
+
* `wrapError` returns `null`.
|
|
262
|
+
*
|
|
263
|
+
* Use cases:
|
|
264
|
+
*
|
|
265
|
+
* 1. Log errors to services like Sentry or Firebase Crashlytics in production, and print them
|
|
266
|
+
* to the console in development and tests. Since this is done in a centralized way, you
|
|
267
|
+
* don't have to "pollute" your code with logging calls. You can use the `action` to log the
|
|
268
|
+
* action name, and any action properties you may find interesting.
|
|
269
|
+
*
|
|
270
|
+
* 2. Have a global place to convert some errors into `UserException`s. For example, Firebase
|
|
271
|
+
* may throw some errors in response to a bad connection to the server. In this case, you
|
|
272
|
+
* may want to show the user a dialog explaining that the connection is bad, which you can
|
|
273
|
+
* do by converting it to a `UserException`. While this could also be done in the action's
|
|
274
|
+
* `wrapError`, you'd have to add it to all actions that use Firebase.
|
|
275
|
+
*
|
|
276
|
+
* For example:
|
|
277
|
+
*
|
|
278
|
+
* ```ts
|
|
279
|
+
* errorObserver: ({ error, action }) => {
|
|
280
|
+
*
|
|
281
|
+
* // Shows a dialog for connection errors.
|
|
282
|
+
* if (error instanceof ConnectionError)
|
|
283
|
+
* return new UserException('Please check your connection.').withHardCause(error);
|
|
284
|
+
*
|
|
285
|
+
* // Throws the other errors during development and tests, so that we can see them.
|
|
286
|
+
* if (inDevelopment() || inTests() || (error instanceof UserException)) return error;
|
|
287
|
+
*
|
|
288
|
+
* // In production, logs the other errors, and swallows them.
|
|
289
|
+
* Logger.error(`Got ${error} in ${action ?? 'the persistor'}.`);
|
|
290
|
+
* return null;
|
|
291
|
+
* }
|
|
292
|
+
* ```
|
|
293
|
+
*
|
|
294
|
+
* Note: Declared as a method so that observers typed with a non-null `action` still compile.
|
|
295
|
+
*/
|
|
296
|
+
errorObserver?(params: {
|
|
297
|
+
error: any;
|
|
298
|
+
originalError: any;
|
|
299
|
+
action: KissAction<St> | null;
|
|
300
|
+
store: Store<St>;
|
|
301
|
+
}): any;
|
|
302
|
+
/**
|
|
303
|
+
* An `actionObserver` can be set during the `Store` creation.
|
|
304
|
+
* It's called whenever actions are dispatched, and also when they finish.
|
|
305
|
+
* The action and the dispatch-count are available, as well as the `ini` parameter:
|
|
306
|
+
* If `ini` is true, this is right before the action is dispatched.
|
|
307
|
+
* If `ini` is false, this is right after the action finishes.
|
|
308
|
+
*
|
|
309
|
+
* The `actionObserver` is a good place to log which actions are dispatched by your application.
|
|
310
|
+
* It should not throw errors. If it does, the error is ignored, and the action runs normally.
|
|
311
|
+
* For example, the following code logs actions to the console in development or test mode.
|
|
312
|
+
* and logs actions to Crashlytics in production mode:
|
|
313
|
+
*
|
|
314
|
+
* ```ts
|
|
315
|
+
* actionObserver: (action, dispatchCount, ini) => {
|
|
316
|
+
* if (inDevelopment() || inTests()) {
|
|
317
|
+
* if (ini) console.log('Action dispatched: ' + action);
|
|
318
|
+
* else console.log('Action finished: ' + action);
|
|
319
|
+
* }
|
|
320
|
+
* else Crashlytics.log('Dispatched: ' + action);
|
|
321
|
+
* }
|
|
322
|
+
* ```
|
|
323
|
+
*/
|
|
324
|
+
actionObserver?: (action: KissAction<St>, dispatchCount: number, ini: boolean) => void;
|
|
325
|
+
/**
|
|
326
|
+
* A `stateObserver`s can be set during the `Store` creation.
|
|
327
|
+
* It's called for all dispatched actions, right after the reducer returns, before
|
|
328
|
+
* the action's `after()` method, before the action's `wrapError()`, and before
|
|
329
|
+
* the `errorObserver`.
|
|
330
|
+
*
|
|
331
|
+
* The parameters are:
|
|
332
|
+
*
|
|
333
|
+
* - action = The action itself.
|
|
334
|
+
*
|
|
335
|
+
* - prevState = The state right before the new state returned by the reducer is applied. Note
|
|
336
|
+
* this may be different from the state when the action was dispatched.
|
|
337
|
+
*
|
|
338
|
+
* - newState = The state returned by the reducer. Note: If you need to know if the state was
|
|
339
|
+
* changed or not by the reducer, you can compare both states:
|
|
340
|
+
* `let ifStateChanged = prevState !== newState;`
|
|
341
|
+
*
|
|
342
|
+
* - error = Is null if the action completed with no error. Otherwise, will be the error thrown
|
|
343
|
+
* by the reducer (before any wrapError is applied). Note that, in case of error, both
|
|
344
|
+
* `prevState` and `newState` will be the current store state when the error was thrown.
|
|
345
|
+
*
|
|
346
|
+
* - dispatchCount = The sequential number of the dispatch.
|
|
347
|
+
*
|
|
348
|
+
* The `stateObserver` should not throw errors. If it does, the error is ignored, and the
|
|
349
|
+
* action runs normally.
|
|
350
|
+
*
|
|
351
|
+
* <br>
|
|
352
|
+
*
|
|
353
|
+
* The state-observer is a good place to add an interface to the Redux DevTools.
|
|
354
|
+
* It's also a good place to add METRICS to your application. For example:
|
|
355
|
+
*
|
|
356
|
+
* ```ts
|
|
357
|
+
* function stateObserver(action: KissAction<State>, prevState: State, newState: State, error: any, dispatchCount: number) {
|
|
358
|
+
* saveMetrics(action, newState, error);
|
|
359
|
+
* }
|
|
360
|
+
* ```
|
|
361
|
+
*
|
|
362
|
+
* An interesting idea is to add a method to the Action base class called `setMetrics`, that
|
|
363
|
+
* allows actions to return tailored custom metrics about the action, and then use it in the
|
|
364
|
+
* state-observer to track those metrics. For example:
|
|
365
|
+
*
|
|
366
|
+
* ```ts
|
|
367
|
+
* class LoadUser extends Action {
|
|
368
|
+
* async reduce() {
|
|
369
|
+
* let user = await loadUser();
|
|
370
|
+
* this.log('User', user.id); // Here!
|
|
371
|
+
* return (state: State) => state.copy({ user });
|
|
372
|
+
* }
|
|
373
|
+
* }
|
|
374
|
+
*
|
|
375
|
+
* stateObserver: (action, prevState, newState, error, dispatchCount) => {
|
|
376
|
+
* let actionLog = action.getLog(); // Here!
|
|
377
|
+
* saveMetrics(action, actionLog, newState, error);
|
|
378
|
+
* }
|
|
379
|
+
* ```
|
|
380
|
+
*/
|
|
381
|
+
stateObserver?: (action: KissAction<St>, prevState: St, newState: St, error: any, dispatchCount: number) => void;
|
|
382
|
+
/**
|
|
383
|
+
* Where the store learns whether the app is **focused** (see `store.isFocused`). By default,
|
|
384
|
+
* in the browser, the app is focused when the document is visible (`document.visibilityState`,
|
|
385
|
+
* following the `visibilitychange` event), so switching to another browser tab, or minimizing
|
|
386
|
+
* the window, makes it not focused. On the server, in Node and in React Native, where there
|
|
387
|
+
* is no `document`, the default says the app is always focused.
|
|
388
|
+
*
|
|
389
|
+
* Give a source to use something else. It gets a `setFocused` function to call whenever the
|
|
390
|
+
* value changes (and right away, with the current value, if it knows it), and returns a
|
|
391
|
+
* function that stops listening, called by `store.dispose()`. For example, in React Native:
|
|
392
|
+
*
|
|
393
|
+
* ```ts
|
|
394
|
+
* import { AppState } from 'react-native';
|
|
395
|
+
*
|
|
396
|
+
* const store = createStore<State>({
|
|
397
|
+
* initialState: State.initialState,
|
|
398
|
+
* focusSource: (setFocused) => {
|
|
399
|
+
* setFocused(AppState.currentState === 'active');
|
|
400
|
+
* const sub = AppState.addEventListener('change', (s) => setFocused(s === 'active'));
|
|
401
|
+
* return () => sub.remove();
|
|
402
|
+
* },
|
|
403
|
+
* });
|
|
404
|
+
* ```
|
|
405
|
+
*
|
|
406
|
+
* The source is started lazily, when something first reads the signal: `store.isFocused`,
|
|
407
|
+
* `useIsFocused()`, or a `useStoreEffect` with `onFocus`/`onBlur`. Until the source sets a
|
|
408
|
+
* value, the app counts as focused. In tests, use `store.setFocused(bool)` instead.
|
|
409
|
+
*/
|
|
410
|
+
focusSource?: AppSignalSource;
|
|
411
|
+
/**
|
|
412
|
+
* Where the store learns whether the app is **online** (see `store.isOnline`). By default, in
|
|
413
|
+
* the browser, it's `navigator.onLine`, following the `online` and `offline` window events.
|
|
414
|
+
* Where there is no `navigator.onLine` (the server, Node, React Native), the default says the
|
|
415
|
+
* app is always online.
|
|
416
|
+
*
|
|
417
|
+
* Give a source to use something else, like `NetInfo` in React Native, or a ping to your own
|
|
418
|
+
* server. It gets a `setOnline` function to call whenever the value changes (and right away,
|
|
419
|
+
* with the current value, if it knows it), and returns a function that stops listening,
|
|
420
|
+
* called by `store.dispose()`:
|
|
421
|
+
*
|
|
422
|
+
* ```ts
|
|
423
|
+
* import NetInfo from '@react-native-community/netinfo';
|
|
424
|
+
*
|
|
425
|
+
* const store = createStore<State>({
|
|
426
|
+
* initialState: State.initialState,
|
|
427
|
+
* onlineSource: (setOnline) => NetInfo.addEventListener((s) => setOnline(s.isConnected === true)),
|
|
428
|
+
* });
|
|
429
|
+
* ```
|
|
430
|
+
*
|
|
431
|
+
* The online signal is also what the `checkInternet` and `unlimitedRetryCheckInternet`
|
|
432
|
+
* action features check by default (the default `hasInternet()` of the actions reads
|
|
433
|
+
* `store.isOnline`), so a custom source makes them work in React Native with no change to the
|
|
434
|
+
* actions. The source is started lazily, when something first reads the signal. Until the
|
|
435
|
+
* source sets a value, the app counts as online. In tests, use `store.setOnline(bool)`, or
|
|
436
|
+
* `store.forceInternetOnOffSimulation`, which takes precedence for the action features.
|
|
437
|
+
*/
|
|
438
|
+
onlineSource?: AppSignalSource;
|
|
439
|
+
/**
|
|
440
|
+
* Set it to `true` in the store that loads the data of a request, in server-side rendering:
|
|
441
|
+
* the store the server code creates per request, dispatches the loading actions on, renders
|
|
442
|
+
* from, and disposes (see "Server-side rendering" in the README). The default is `false`.
|
|
443
|
+
*
|
|
444
|
+
* ```ts
|
|
445
|
+
* const store = createStore<State>({ initialState: emptyState, server: true });
|
|
446
|
+
* try {
|
|
447
|
+
* await store.dispatchAndWait(new LoadData());
|
|
448
|
+
* return store.state;
|
|
449
|
+
* } finally {
|
|
450
|
+
* await store.dispose();
|
|
451
|
+
* }
|
|
452
|
+
* ```
|
|
453
|
+
*
|
|
454
|
+
* The server runs the same action classes as the client, and only wants to run their
|
|
455
|
+
* reducers once. So, in a server store, the action features that repeat or wait run the
|
|
456
|
+
* reducer once, now, and stop:
|
|
457
|
+
*
|
|
458
|
+
* - Polling: an action dispatched with `Poll.start` or `Poll.runNowAndRestart` runs once,
|
|
459
|
+
* and no polling is started (as with `Poll.once`).
|
|
460
|
+
* - `retry` and `unlimitedRetryCheckInternet`: the reducer runs once, and if it throws, the
|
|
461
|
+
* action fails with that error right away. The same for the `retry` of an
|
|
462
|
+
* `OptimisticCommand`, which calls `sendCommandToServer` once.
|
|
463
|
+
* - `debounce`: the reducer runs right away, without waiting the debounce period.
|
|
464
|
+
* - The `focusSource` and `onlineSource` are not started: the app counts as focused and
|
|
465
|
+
* online.
|
|
466
|
+
* - A `persistor` is not allowed: the store creation throws a `StoreException`.
|
|
467
|
+
*
|
|
468
|
+
* Everything else is the same, and the validations of the features still run, so an action
|
|
469
|
+
* class that is wrong on the client is also wrong on the server. The store still has to be
|
|
470
|
+
* disposed with `store.dispose()` after rendering, since the props, the pending waits and
|
|
471
|
+
* the dependencies are not affected by this option.
|
|
472
|
+
*
|
|
473
|
+
* Don't set it in the store the components use (the one given to the `StoreProvider`), not
|
|
474
|
+
* even when a framework renders that store on the server: on the server, that store only
|
|
475
|
+
* renders the state it was given, and the server never runs the effects that dispatch. Read
|
|
476
|
+
* it with `store.isServer`.
|
|
477
|
+
*
|
|
478
|
+
* The option is explicit on purpose. The store doesn't detect the server by itself (with
|
|
479
|
+
* `typeof window === 'undefined'`, for example): tests that run in Node would silently get
|
|
480
|
+
* the server behavior, React Native has a `window`, and a store created without the option
|
|
481
|
+
* must behave the same wherever it runs.
|
|
482
|
+
*/
|
|
483
|
+
server?: boolean;
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* A source of the focus or online signal of the app (see `focusSource` and `onlineSource` in
|
|
487
|
+
* `createStore`). It gets a function to call with the new value whenever it changes, and may
|
|
488
|
+
* return a function that stops listening, called when the store is disposed.
|
|
489
|
+
*/
|
|
490
|
+
export type AppSignalSource = (set: (value: boolean) => void) => (() => void) | void;
|
|
491
|
+
/**
|
|
492
|
+
* The store holds the state of the application and allows the state to be updated
|
|
493
|
+
* by dispatching actions. The store is also responsible for showing user exceptions
|
|
494
|
+
* to the user, persisting the state to the local device disk, processing wait states
|
|
495
|
+
* to show spinners while async operations are in progress, and more.
|
|
496
|
+
*
|
|
497
|
+
* You can create a store with `const store = createStore()` or `const store = new Store()`.
|
|
498
|
+
*/
|
|
499
|
+
export declare function createStore<St>(params: ConstructorParams<St>): Store<St>;
|
|
500
|
+
export interface Store<St> {
|
|
501
|
+
/** Same as `dispose()`, without waiting for the async disposals. For `using store = ...`. */
|
|
502
|
+
[Symbol.dispose](): void;
|
|
503
|
+
/** Same as `dispose()`. For `await using store = ...`. */
|
|
504
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
505
|
+
}
|
|
506
|
+
/** A function the store calls when something it publishes may have changed. */
|
|
507
|
+
export type StoreListener = () => void;
|
|
508
|
+
/** Removes a listener. Calling it more than once does nothing. */
|
|
509
|
+
export type Unsubscribe = () => void;
|
|
510
|
+
/**
|
|
511
|
+
* The store holds the state of the application and allows the state to be updated
|
|
512
|
+
* by dispatching actions. The store is also responsible for showing user exceptions
|
|
513
|
+
* to the user, persisting the state to the local device disk, processing wait states
|
|
514
|
+
* to show spinners while async operations are in progress, and more.
|
|
515
|
+
*
|
|
516
|
+
* You can create a store with `const store = createStore()` or `const store = new Store()`.
|
|
517
|
+
*/
|
|
518
|
+
export declare class Store<St> {
|
|
519
|
+
private readonly _notificationBatch;
|
|
520
|
+
private readonly _stateListeners;
|
|
521
|
+
private readonly _statusListeners;
|
|
522
|
+
private readonly _readyListeners;
|
|
523
|
+
private readonly _focusListeners;
|
|
524
|
+
private readonly _onlineListeners;
|
|
525
|
+
private readonly _focus;
|
|
526
|
+
private readonly _online;
|
|
527
|
+
/**
|
|
528
|
+
* Whether the app is focused. In the browser, by default, this means the document is visible:
|
|
529
|
+
* the user is on the tab that shows the app, and the window is not minimized. Where the store
|
|
530
|
+
* can't know (the server, Node, React Native without a `focusSource`), it's always `true`.
|
|
531
|
+
* See `focusSource` in `createStore` to change where it comes from.
|
|
532
|
+
*
|
|
533
|
+
* In components, use `useIsFocused()`, or `useStoreEffect` with `onFocus` and `onBlur` to
|
|
534
|
+
* dispatch actions when the app is focused again (for example, to reload the data on screen).
|
|
535
|
+
*
|
|
536
|
+
* The first read starts listening to the source. In tests, set it with `setFocused`.
|
|
537
|
+
*/
|
|
538
|
+
get isFocused(): boolean;
|
|
539
|
+
/**
|
|
540
|
+
* Whether the app is online. In the browser, by default, it's `navigator.onLine`, which only
|
|
541
|
+
* says if there is a local network connection, not if the internet is reachable. Where the
|
|
542
|
+
* store can't know (the server, Node, React Native without an `onlineSource`), it's always
|
|
543
|
+
* `true`. See `onlineSource` in `createStore` to change where it comes from (for example,
|
|
544
|
+
* `NetInfo` in React Native, or a ping to your server).
|
|
545
|
+
*
|
|
546
|
+
* It's what the `checkInternet` and `unlimitedRetryCheckInternet` action features check by
|
|
547
|
+
* default (through the default `hasInternet()` of the actions). In components, use
|
|
548
|
+
* `useIsOnline()`, or `useStoreEffect` with `onOnline` and `onOffline` to dispatch actions
|
|
549
|
+
* when the internet comes back.
|
|
550
|
+
*
|
|
551
|
+
* The first read starts listening to the source. In tests, set it with `setOnline`.
|
|
552
|
+
*/
|
|
553
|
+
get isOnline(): boolean;
|
|
554
|
+
/**
|
|
555
|
+
* Sets `isFocused` by hand, for tests. The store then stops listening to its `focusSource`,
|
|
556
|
+
* and only changes when you call this again. Pass `null` to listen to the source again:
|
|
557
|
+
*
|
|
558
|
+
* ```ts
|
|
559
|
+
* store.setFocused(false); // The user went to another tab.
|
|
560
|
+
* store.setFocused(true); // The user came back: `onFocus` callbacks run.
|
|
561
|
+
* ```
|
|
562
|
+
*/
|
|
563
|
+
setFocused(isFocused: boolean | null): void;
|
|
564
|
+
/**
|
|
565
|
+
* Sets `isOnline` by hand, for tests. The store then stops listening to its `onlineSource`,
|
|
566
|
+
* and only changes when you call this again. Pass `null` to listen to the source again:
|
|
567
|
+
*
|
|
568
|
+
* ```ts
|
|
569
|
+
* store.setOnline(false); // The internet went down: `checkInternet` actions fail.
|
|
570
|
+
* store.setOnline(true); // It's back: `onOnline` callbacks run.
|
|
571
|
+
* ```
|
|
572
|
+
*
|
|
573
|
+
* See also `forceInternetOnOffSimulation`, which only affects the action features, and takes
|
|
574
|
+
* precedence over this for them.
|
|
575
|
+
*/
|
|
576
|
+
setOnline(isOnline: boolean | null): void;
|
|
577
|
+
/**
|
|
578
|
+
* The `Store.log()` is a static function that Kiss uses internally to log information.
|
|
579
|
+
* If you want, you can also use it yourself, like this:
|
|
580
|
+
*
|
|
581
|
+
* ```ts
|
|
582
|
+
* Store.log('Some information: ' + someVariable);
|
|
583
|
+
* ```
|
|
584
|
+
*
|
|
585
|
+
* The log function is set during the creation of the store,
|
|
586
|
+
* using the `logger` constructor parameter. For example:
|
|
587
|
+
*
|
|
588
|
+
* ```ts
|
|
589
|
+
* const store = new Store<State>({
|
|
590
|
+
* initialState: new State(),
|
|
591
|
+
* logger: (obj) => process.stdout.write(obj + '\n'),
|
|
592
|
+
* });
|
|
593
|
+
* ```
|
|
594
|
+
*
|
|
595
|
+
* Or using a logger library:
|
|
596
|
+
*
|
|
597
|
+
* ```ts
|
|
598
|
+
* import { Logger } from 'my-logger-library';
|
|
599
|
+
*
|
|
600
|
+
* const store = new Store<State>({
|
|
601
|
+
* initialState: new State(),
|
|
602
|
+
* logger: (obj) => Logger.log(obj),
|
|
603
|
+
* });
|
|
604
|
+
* ```
|
|
605
|
+
*
|
|
606
|
+
* Note, if you don't define a logger yourself, the default is to print
|
|
607
|
+
* the log messages to the console with `console.log()`.
|
|
608
|
+
*
|
|
609
|
+
* This is how you may completely turn logging off:
|
|
610
|
+
*
|
|
611
|
+
* ```ts
|
|
612
|
+
* const store = new Store<State>({
|
|
613
|
+
* initialState: new State(),
|
|
614
|
+
* logger: null,
|
|
615
|
+
* });
|
|
616
|
+
* ```
|
|
617
|
+
*
|
|
618
|
+
* Setting the logger to `null` is better than using an empty function, because Kiss then
|
|
619
|
+
* skips building its log messages (for example, the description of each dispatched action).
|
|
620
|
+
*/
|
|
621
|
+
static log: (obj: any) => void;
|
|
622
|
+
private static _noLogger;
|
|
623
|
+
private static _isLogging;
|
|
624
|
+
private _state;
|
|
625
|
+
private _shutDown;
|
|
626
|
+
/**
|
|
627
|
+
* Set this to `true` when you want to abort any new actions that are dispatched.
|
|
628
|
+
* Note this will not stop async actions already running, but you can wait for them to finish:
|
|
629
|
+
*
|
|
630
|
+
* ```ts
|
|
631
|
+
* await store.waitAllActions(null, {
|
|
632
|
+
* timeoutMillis: 5000,
|
|
633
|
+
* completeImmediately: true
|
|
634
|
+
* }).catch((error) => { });
|
|
635
|
+
* ```
|
|
636
|
+
*
|
|
637
|
+
* Set this back to `false` to restart the store accepting new action dispatches.
|
|
638
|
+
*
|
|
639
|
+
* Shutting down the store also calls `clearInternalActionProps()`, which, for example, stops
|
|
640
|
+
* all polling (see `KissAction.poll`), and stops the actions that retry. The polling and the
|
|
641
|
+
* retries are not restarted when you set this back to `false`.
|
|
642
|
+
*/
|
|
643
|
+
setShutDown(shutDown: boolean): void;
|
|
644
|
+
private _logOutsRunning;
|
|
645
|
+
private _isApplyingLogOutState;
|
|
646
|
+
private _isShutDown;
|
|
647
|
+
private _clearCount;
|
|
648
|
+
private _logOutCount;
|
|
649
|
+
private _wasDispatchedBeforeLogOut;
|
|
650
|
+
private static _logOutDiscardError;
|
|
651
|
+
private _throwIfDispatchedBeforeLogOut;
|
|
652
|
+
private _isDispatchedByActionBeforeLogOut;
|
|
653
|
+
private _retryWaitEnders;
|
|
654
|
+
private _stopAllRetries;
|
|
655
|
+
private static _clearedRetryError;
|
|
656
|
+
/**
|
|
657
|
+
* Clears the information the store keeps for the action features, so that actions dispatched
|
|
658
|
+
* from now on behave as if no action had been dispatched before. This is useful during logout,
|
|
659
|
+
* to stop the effects of the actions of the previous user, and to make sure they don't
|
|
660
|
+
* affect the next user. It's also called when the store is shut down (see `setShutDown`).
|
|
661
|
+
*
|
|
662
|
+
* It does the following:
|
|
663
|
+
* - Removes all `fresh` keys, so the next dispatches run even if the data was still fresh.
|
|
664
|
+
* Otherwise, after logging in again, loading the new user's data could be aborted.
|
|
665
|
+
* - Removes all `throttle` locks, for the same reason.
|
|
666
|
+
* - Removes all `debounce` locks. Actions still waiting for their debounce period finish right
|
|
667
|
+
* away, without running their reducer.
|
|
668
|
+
* - Stops all polling (see `KissAction.poll`).
|
|
669
|
+
* - Discards the actions waiting in all `sequential` queues. They are aborted without running,
|
|
670
|
+
* like when a failed action discards its queue (see `discardQueueOnError()`). Actions
|
|
671
|
+
* dispatched from now on start new queues, and don't wait for the actions still running.
|
|
672
|
+
* - Stops the actions that retry, with `retry` or `unlimitedRetryCheckInternet`, so that they
|
|
673
|
+
* don't keep retrying for the previous user. An action waiting to retry is aborted right
|
|
674
|
+
* away, and an action running an attempt is aborted if that attempt fails (if it succeeds,
|
|
675
|
+
* it changes the state as usual). They are aborted with an `AbortDispatchException`, so they
|
|
676
|
+
* don't fail, and don't show errors. This also releases their non-reentrant keys. The same
|
|
677
|
+
* goes for an `OptimisticCommand` with `retry`, which then doesn't roll back its optimistic
|
|
678
|
+
* value, and doesn't reload.
|
|
679
|
+
* - Releases all `OptimisticSync` and `OptimisticSyncWithPush` keys, so the next dispatches
|
|
680
|
+
* send their own requests. An action whose request is in flight stops when that request
|
|
681
|
+
* finishes, without sending follow-up requests, and is aborted with an
|
|
682
|
+
* `AbortDispatchException`.
|
|
683
|
+
* - Removes the revisions that `OptimisticSyncWithPush` and `ServerPush` keep for each key.
|
|
684
|
+
* The server revisions you saved in the state are kept, and still used.
|
|
685
|
+
* - Forgets the `ignoreOld` keys. The actions dispatched from now on don't make the ones
|
|
686
|
+
* dispatched before old.
|
|
687
|
+
* - Clears the failures of all actions (see `clearAllExceptions()`), so that `isFailed` is
|
|
688
|
+
* `false` and `exceptionFor` is `null` for all action types. The new user doesn't see the
|
|
689
|
+
* errors of the previous one.
|
|
690
|
+
*
|
|
691
|
+
* Note:
|
|
692
|
+
* - Actions that are already running keep running, and still change the state when they
|
|
693
|
+
* finish. For example, an action that started loading the data of the previous user before
|
|
694
|
+
* the logout can still put that data into the state after the logout. To avoid that, use
|
|
695
|
+
* `logOut()`, which also discards the result of the actions dispatched before it.
|
|
696
|
+
* - It doesn't affect `nonReentrant` actions or `OptimisticCommand`s, since their keys belong to
|
|
697
|
+
* the actions that are running, and are released when those actions finish.
|
|
698
|
+
*
|
|
699
|
+
* Usage:
|
|
700
|
+
*
|
|
701
|
+
* ```ts
|
|
702
|
+
* class Logout extends KissAction<State> {
|
|
703
|
+
* reduce() {
|
|
704
|
+
* this.store.clearInternalActionProps();
|
|
705
|
+
* return State.initialState();
|
|
706
|
+
* }
|
|
707
|
+
* }
|
|
708
|
+
* ```
|
|
709
|
+
*/
|
|
710
|
+
clearInternalActionProps(): void;
|
|
711
|
+
/**
|
|
712
|
+
* If you are running tests, you can change `forceInternetOnOffSimulation` to simulate the
|
|
713
|
+
* internet connection as ON or OFF for all actions that use `checkInternet` or
|
|
714
|
+
* `unlimitedRetryCheckInternet`.
|
|
715
|
+
*
|
|
716
|
+
* - Return `true` if there IS internet.
|
|
717
|
+
* - Return `false` if there is NO internet.
|
|
718
|
+
* - Return `null` to use the real internet connection status (default).
|
|
719
|
+
*
|
|
720
|
+
* Example:
|
|
721
|
+
*
|
|
722
|
+
* ```ts
|
|
723
|
+
* store.forceInternetOnOffSimulation = () => false;
|
|
724
|
+
* ```
|
|
725
|
+
*
|
|
726
|
+
* This is specially useful during tests, for testing what happens when you have no internet
|
|
727
|
+
* connection. And since it's tied to the store, it automatically resets when the store is
|
|
728
|
+
* recreated.
|
|
729
|
+
*
|
|
730
|
+
* Note the simulation takes precedence over the action's `hasInternet()` method, even if you
|
|
731
|
+
* override it. To simulate the connection for a single action, override its
|
|
732
|
+
* `internetOnOffSimulation` getter instead.
|
|
733
|
+
*/
|
|
734
|
+
forceInternetOnOffSimulation: () => boolean | null;
|
|
735
|
+
/**
|
|
736
|
+
* A queue of errors of type UserException, thrown by actions.
|
|
737
|
+
* They are shown to the user using the function `showUserException`.
|
|
738
|
+
* This is public if you need it for advanced stuff, but you should probably not touch it.
|
|
739
|
+
*/
|
|
740
|
+
readonly userExceptionsQueue: UserException[];
|
|
741
|
+
/**
|
|
742
|
+
* If `true`, will use Store.log() to log all state changes.
|
|
743
|
+
* It uses `Store.describeStateChange()` to create the description of those changes.
|
|
744
|
+
*
|
|
745
|
+
* Note this is true (turned on) by default, as it may be useful for development, testing and
|
|
746
|
+
* debugging. Be sure to turn it off in production, as it may slow down your application:
|
|
747
|
+
*
|
|
748
|
+
* ```ts
|
|
749
|
+
* // In production
|
|
750
|
+
* const store = new Store<State>({
|
|
751
|
+
* initialState: new State(),
|
|
752
|
+
* logStateChanges: false,
|
|
753
|
+
* });
|
|
754
|
+
* ```
|
|
755
|
+
*/
|
|
756
|
+
private readonly _logStateChanges;
|
|
757
|
+
/**
|
|
758
|
+
* The async actions that are currently being processed.
|
|
759
|
+
* Use `isWaiting` to know if an action is currently being processed.
|
|
760
|
+
*/
|
|
761
|
+
private readonly _actionsInProgress;
|
|
762
|
+
/**
|
|
763
|
+
* The non-reentrant actions that are running, which hold their non-reentrant keys.
|
|
764
|
+
* Unlike `_actionsInProgress`, an action only leaves this set after its `after()` method
|
|
765
|
+
* runs, even if its state was already applied.
|
|
766
|
+
*/
|
|
767
|
+
private readonly _nonReentrantActionsRunning;
|
|
768
|
+
/**
|
|
769
|
+
* The promises given to `useSuspense`, one per action type and `statusKey` that a suspended
|
|
770
|
+
* component waits for. The list is only filled when `useSuspense` is called while the action
|
|
771
|
+
* is in progress, and emptied as the actions finish, so a store whose components never call
|
|
772
|
+
* `useSuspense` keeps it empty, and only checks its size when an action finishes.
|
|
773
|
+
*/
|
|
774
|
+
private readonly _suspenseWaits;
|
|
775
|
+
/**
|
|
776
|
+
* The failures of the actions that failed recently.
|
|
777
|
+
* When an action fails with any error (that was not swallowed, and is not an
|
|
778
|
+
* `AbortDispatchException`), its error is added here, indexed by the action type, together
|
|
779
|
+
* with the action's `statusKey`. Only the type, the key and the error are kept, not the action.
|
|
780
|
+
* A failure is removed when an action of the same type and `statusKey` is dispatched again,
|
|
781
|
+
* by `clearExceptionFor`, or by `clearAllExceptions` (which `clearInternalActionProps` calls).
|
|
782
|
+
*
|
|
783
|
+
* Use `isFailed`, `exceptionFor` and `clearExceptionFor` to know if you should display
|
|
784
|
+
* some error message due to an action failure.
|
|
785
|
+
*
|
|
786
|
+
* Note: Throwing an `UserException` can show a modal dialog to the user, and also show the error
|
|
787
|
+
* as a message in the UI. If you don't want to show the dialog you can use the `noDialog`
|
|
788
|
+
* getter in the error message: `throw new UserException('Invalid input').noDialog`.
|
|
789
|
+
*/
|
|
790
|
+
private readonly _failedActions;
|
|
791
|
+
/**
|
|
792
|
+
* Helps implement the waitCondition method.
|
|
793
|
+
*/
|
|
794
|
+
private _waitConditions;
|
|
795
|
+
/**
|
|
796
|
+
* Helps implement the `dispatchWhen` method. One function for each `dispatchWhen` whose
|
|
797
|
+
* condition was met by the current state change, called when `_registerState` finishes.
|
|
798
|
+
* It checks the condition again, and dispatches the action if it's still met.
|
|
799
|
+
*/
|
|
800
|
+
private _dispatchWhenQueue;
|
|
801
|
+
/**
|
|
802
|
+
* Helps implement the `dispatchWhen` method. The functions that cancel the `dispatchWhen`s
|
|
803
|
+
* that are still waiting for their condition. See `_cancelAllDispatchWhen`.
|
|
804
|
+
*/
|
|
805
|
+
private _dispatchWhenCancels;
|
|
806
|
+
private _cancelAllDispatchWhen;
|
|
807
|
+
private _abortAllWaits;
|
|
808
|
+
/**
|
|
809
|
+
* Helps implement the `waitActionCondition` method.
|
|
810
|
+
*/
|
|
811
|
+
private _waitActionConditions;
|
|
812
|
+
private _debounceLocks;
|
|
813
|
+
private _throttleLocks;
|
|
814
|
+
private _freshKeys;
|
|
815
|
+
private _optimisticSyncKeys;
|
|
816
|
+
private _optimisticSyncRevisions;
|
|
817
|
+
private _sequentialQueues;
|
|
818
|
+
private _pollingCycles;
|
|
819
|
+
private readonly _notSyncErrors;
|
|
820
|
+
private readonly _props;
|
|
821
|
+
private readonly _propDisposers;
|
|
822
|
+
private _disposed;
|
|
823
|
+
private readonly _environment;
|
|
824
|
+
private readonly _configuration;
|
|
825
|
+
private readonly _dependencies;
|
|
826
|
+
private readonly _processPersistence;
|
|
827
|
+
private _dispatchCount;
|
|
828
|
+
private readonly _showUserException;
|
|
829
|
+
private readonly _mergeServerStateFn;
|
|
830
|
+
private _pendingServerSlices;
|
|
831
|
+
private _applyingServerSlice;
|
|
832
|
+
private readonly _errorObserver?;
|
|
833
|
+
private readonly _actionObserver?;
|
|
834
|
+
private readonly _stateObserver?;
|
|
835
|
+
/**
|
|
836
|
+
* You can use `store.mocks` to mock actions. You should use this for testing purposes, only.
|
|
837
|
+
*
|
|
838
|
+
* To mock actions and their reducers, use `store.mocks.add(actionType, mockFunction)`
|
|
839
|
+
* multiple times, one for each **action type** you want to mock.
|
|
840
|
+
*
|
|
841
|
+
* By adding mocks to the store, the `mockFunction` will be called
|
|
842
|
+
* to return a mocked action that will be used instead of the original actions, when dispatching.
|
|
843
|
+
* You can mock an action to return a specific state, to throw an error,
|
|
844
|
+
* or even to abort the action.
|
|
845
|
+
*
|
|
846
|
+
* Usage:
|
|
847
|
+
*
|
|
848
|
+
* - `store.mocks.add(actionType, (actionType) => mockAction)`:
|
|
849
|
+
* Adds a mock action to the store. Whenever an action of type `actionType` is
|
|
850
|
+
* dispatched, `mockAction` will be dispatched instead. If you pass `mockAction` as null,
|
|
851
|
+
* actions of type `actionType` will be ignored when dispatched.
|
|
852
|
+
*
|
|
853
|
+
* - `remove(actionType)`:
|
|
854
|
+
* Removes a mock action from the store. Note you should pass the
|
|
855
|
+
* type of the action that was mocked, and NOT the type of the mock.
|
|
856
|
+
*
|
|
857
|
+
* - `clear()`:
|
|
858
|
+
* Removes all mock actions from the store.
|
|
859
|
+
*
|
|
860
|
+
* Examples:
|
|
861
|
+
*
|
|
862
|
+
* ```ts
|
|
863
|
+
* // When an IncrementAction is dispatched, AddAction(1) will be dispatched instead.
|
|
864
|
+
* store.mocks.add(IncrementAction, (action) => new AddAction(1));
|
|
865
|
+
*
|
|
866
|
+
* // When an IncrementAction is dispatched, it will be ignored.
|
|
867
|
+
* store.mocks.add(IncrementAction, null);
|
|
868
|
+
*
|
|
869
|
+
* // Removes the mock for Increment.
|
|
870
|
+
* store.mocks.remove(IncrementAction);
|
|
871
|
+
*
|
|
872
|
+
* // Removes all mocks.
|
|
873
|
+
* store.mocks.clear();
|
|
874
|
+
*
|
|
875
|
+
* // When an AddAction is dispatched, the value will be subtracted instead.
|
|
876
|
+
* store.mocks.add(AddAction, (action) => new AddAction(-action.value));
|
|
877
|
+
* ```
|
|
878
|
+
*/
|
|
879
|
+
mocks: Mocks<St>;
|
|
880
|
+
readonly record: {
|
|
881
|
+
isRecording: boolean;
|
|
882
|
+
changes: Array<{
|
|
883
|
+
action: KissAction<St>;
|
|
884
|
+
ini: boolean;
|
|
885
|
+
prevState: St;
|
|
886
|
+
newState: St;
|
|
887
|
+
error: any;
|
|
888
|
+
dispatchCount: number;
|
|
889
|
+
}>;
|
|
890
|
+
start: () => void;
|
|
891
|
+
stop: () => void;
|
|
892
|
+
result: () => {
|
|
893
|
+
action: KissAction<St>;
|
|
894
|
+
ini: boolean;
|
|
895
|
+
prevState: St;
|
|
896
|
+
newState: St;
|
|
897
|
+
error: any;
|
|
898
|
+
dispatchCount: number;
|
|
899
|
+
}[];
|
|
900
|
+
toString: () => string;
|
|
901
|
+
};
|
|
902
|
+
private _defaultLogger;
|
|
903
|
+
private _defaultShowUserException;
|
|
904
|
+
constructor({ initialState, environment, configuration, dependencies, showUserException, persistor, mergeServerState, errorObserver, actionObserver, stateObserver, logger, logStateChanges, focusSource, onlineSource, server, }: ConstructorParams<St>);
|
|
905
|
+
private readonly _ready;
|
|
906
|
+
/**
|
|
907
|
+
* Returns a promise that resolves when the store has finished everything it needs to do at
|
|
908
|
+
* startup. Currently, this means reading the persisted state (if there is a persistor) and
|
|
909
|
+
* putting it into the store. In the future, other startup work may be added here.
|
|
910
|
+
*
|
|
911
|
+
* While the store is not ready, its state is the `initialState` passed to the constructor.
|
|
912
|
+
* Your UI can show that state normally (for example, showing loading indicators where data
|
|
913
|
+
* is missing). However, you can't change the state before the store is ready, since it
|
|
914
|
+
* would be overwritten when the persisted state is loaded. Dispatching an action before the
|
|
915
|
+
* store is ready is a developer error, and throws a `StoreException`. So, you must wait for
|
|
916
|
+
* the store to be ready before dispatching any actions:
|
|
917
|
+
*
|
|
918
|
+
* ```ts
|
|
919
|
+
* const store = createStore<AppState>({
|
|
920
|
+
* initialState: AppState.initialState(),
|
|
921
|
+
* persistor: new MyPersistor(),
|
|
922
|
+
* });
|
|
923
|
+
*
|
|
924
|
+
* await store.ready();
|
|
925
|
+
* store.dispatch(new InitAppAction());
|
|
926
|
+
* ```
|
|
927
|
+
*
|
|
928
|
+
* The promise never rejects. If reading the persisted state fails, the error is reported
|
|
929
|
+
* (see `errorObserver`) and the store keeps the initial state. It always returns the same
|
|
930
|
+
* promise, so it can be called many times, and does not read the persisted state again.
|
|
931
|
+
* A store without a persistor is ready right away.
|
|
932
|
+
*
|
|
933
|
+
* In components, use `useIsStoreReady()` to know if the store is ready, for example to show
|
|
934
|
+
* a loading state, or to disable buttons that dispatch actions.
|
|
935
|
+
*/
|
|
936
|
+
ready(): Promise<void>;
|
|
937
|
+
get state(): St;
|
|
938
|
+
get dispatchCount(): number;
|
|
939
|
+
/**
|
|
940
|
+
* Dispatches the action to the Redux store, to potentially change the state.
|
|
941
|
+
*
|
|
942
|
+
* If the action fails with an error that is not swallowed (see `dispatchAndWait`), a SYNC
|
|
943
|
+
* action throws the error from `dispatch`. An ASYNC action can't throw it, since `dispatch`
|
|
944
|
+
* already returned, so it becomes an unhandled promise rejection. To catch errors of async
|
|
945
|
+
* actions, use `dispatchAndWait` instead.
|
|
946
|
+
*
|
|
947
|
+
* An action can only be dispatched once. Dispatching an action that was already dispatched
|
|
948
|
+
* (running or finished) is a developer error, and throws a `StoreException`. Create a new
|
|
949
|
+
* action each time instead.
|
|
950
|
+
*
|
|
951
|
+
* Dispatching an action before the store is ready (see `ready`) also throws a `StoreException`.
|
|
952
|
+
*
|
|
953
|
+
* See also:
|
|
954
|
+
* - `dispatchAll` which dispatches all given actions in parallel.
|
|
955
|
+
* - `dispatchSync` which dispatches sync actions, and throws if the action is async.
|
|
956
|
+
* - `dispatchAndWait` which dispatches both sync and async actions, and returns a Promise.
|
|
957
|
+
* - `dispatchAndWaitAll` which dispatches all given actions, and returns a Promise.
|
|
958
|
+
*/
|
|
959
|
+
dispatch(action: KissAction<St>): void;
|
|
960
|
+
/**
|
|
961
|
+
* Dispatches an action and returns a promise that resolves when the action finishes.
|
|
962
|
+
* While the state change from the action's reducer will have been applied when the promise
|
|
963
|
+
* resolves, other independent processes that the action may have started may still be in
|
|
964
|
+
* progress.
|
|
965
|
+
*
|
|
966
|
+
* Usage: `await store.dispatchAndWait(new MyAction())`.
|
|
967
|
+
*
|
|
968
|
+
* If the action fails, the error is processed as usual (`wrapError`, `after`,
|
|
969
|
+
* `errorObserver`, etc). If the error is swallowed (a `UserException`, or when
|
|
970
|
+
* `wrapError` or the `errorObserver` returns `null`), the promise resolves
|
|
971
|
+
* with the action status. Otherwise, the promise REJECTS with the error, for both sync and
|
|
972
|
+
* async actions. So you can use `try { await dispatchAndWait(...) } catch` or `.catch()`.
|
|
973
|
+
*
|
|
974
|
+
* An action can only be dispatched once. Dispatching an action that was already dispatched
|
|
975
|
+
* (running or finished) is a developer error: `dispatchAndWait` throws a `StoreException`
|
|
976
|
+
* synchronously, instead of returning a rejected promise. A previous `dispatchAndWait` of
|
|
977
|
+
* that same action is not affected.
|
|
978
|
+
*
|
|
979
|
+
* See also:
|
|
980
|
+
* - `dispatch` which dispatches both sync and async actions.
|
|
981
|
+
* - `dispatchSync` which dispatches sync actions, and throws if the action is async.
|
|
982
|
+
* - `dispatchAll` which dispatches all given actions in parallel.
|
|
983
|
+
* - `dispatchAndWaitAll` which dispatches all given actions, and returns a Promise.
|
|
984
|
+
*/
|
|
985
|
+
dispatchAndWait(action: KissAction<St>): Promise<ActionStatus>;
|
|
986
|
+
/**
|
|
987
|
+
* Dispatches all given actions in parallel, applying their reducers, and possibly changing
|
|
988
|
+
* the store state. The actions may be sync or async. It returns a Promise that resolves when
|
|
989
|
+
* ALL actions finish.
|
|
990
|
+
*
|
|
991
|
+
* ```ts
|
|
992
|
+
* let actions = await store.dispatchAndWaitAll([new BuyAction('IBM'), new SellAction('TSLA')]);
|
|
993
|
+
* ```
|
|
994
|
+
*
|
|
995
|
+
* Note this is exactly the same as doing:
|
|
996
|
+
*
|
|
997
|
+
* ```ts
|
|
998
|
+
* let action1 = new BuyAction('IBM');
|
|
999
|
+
* let action2 = new SellAction('TSLA');
|
|
1000
|
+
* store.dispatch(action1);
|
|
1001
|
+
* store.dispatch(action2);
|
|
1002
|
+
* await store.waitAllActions([action1, action2], { completeImmediately: true });
|
|
1003
|
+
* let actions = [action1, action2];
|
|
1004
|
+
* ```
|
|
1005
|
+
*
|
|
1006
|
+
* Note: While the state change from the action's reducers will have been applied when the
|
|
1007
|
+
* Promise resolves, other independent processes that the action may have started may still
|
|
1008
|
+
* be in progress.
|
|
1009
|
+
*
|
|
1010
|
+
* If some actions fail with an error that is not swallowed (see `dispatchAndWait`), all
|
|
1011
|
+
* actions are still dispatched and allowed to finish. Only then the Promise rejects, with
|
|
1012
|
+
* the error of the first failed action in the list.
|
|
1013
|
+
*
|
|
1014
|
+
* This is also true when the dispatch of an action throws right away (for example, when the
|
|
1015
|
+
* action was already dispatched, or the store is not ready): the other actions are still
|
|
1016
|
+
* dispatched and allowed to finish, and that error counts as the error of that action.
|
|
1017
|
+
* Note `dispatchAndWaitAll` itself never throws, but returns a Promise that rejects.
|
|
1018
|
+
*
|
|
1019
|
+
* See also:
|
|
1020
|
+
* - `dispatch` which dispatches both sync and async actions.
|
|
1021
|
+
* - `dispatchAndWait` which dispatches both sync and async actions, and returns a Promise.
|
|
1022
|
+
* - `dispatchSync` which dispatches sync actions, and throws if the action is async.
|
|
1023
|
+
* - `dispatchAll` which dispatches all given actions in parallel.
|
|
1024
|
+
*/
|
|
1025
|
+
dispatchAndWaitAll(actions: KissAction<St>[]): Promise<KissAction<St>[]>;
|
|
1026
|
+
/**
|
|
1027
|
+
* Dispatches all given actions in parallel, applying their reducer, and possibly changing
|
|
1028
|
+
* the store state. It returns the same list of actions, so that you can instantiate them
|
|
1029
|
+
* inline, but still get a list of them.
|
|
1030
|
+
*
|
|
1031
|
+
* ```ts
|
|
1032
|
+
* let actions = store.dispatchAll([new BuyAction('IBM'), new SellAction('TSLA')]);
|
|
1033
|
+
* ```
|
|
1034
|
+
*
|
|
1035
|
+
* See also:
|
|
1036
|
+
* - `dispatch` which dispatches both sync and async actions.
|
|
1037
|
+
* - `dispatchAndWait` which dispatches both sync and async actions, and returns a Promise.
|
|
1038
|
+
* - `dispatchAndWaitAll` which dispatches all given actions, and returns a Promise.
|
|
1039
|
+
* - `dispatchSync` which dispatches sync actions, and throws if the action is async. */
|
|
1040
|
+
dispatchAll(actions: KissAction<St>[]): KissAction<St>[];
|
|
1041
|
+
/**
|
|
1042
|
+
* Dispatches the given action to the Redux store, to potentially change the state.
|
|
1043
|
+
*
|
|
1044
|
+
* This is exactly the same as the regular `dispatch`, except for the fact it
|
|
1045
|
+
* will throw a `StoreException` if the action is ASYNC. Note an action is ASYNC
|
|
1046
|
+
* if any of its `reduce()` or `before()` methods return a Promise.
|
|
1047
|
+
*
|
|
1048
|
+
* The only use for `dispatchSync` is when you need to guarantee (in runtime) that your
|
|
1049
|
+
* action is SYNC, which means the state gets changed right after the dispatch call.
|
|
1050
|
+
*
|
|
1051
|
+
* Like `dispatch`, it throws a `StoreException` if the action was already dispatched.
|
|
1052
|
+
*
|
|
1053
|
+
* The `StoreException` for an ASYNC action is always thrown. It's not given to the action's
|
|
1054
|
+
* `wrapError` or to the `errorObserver`, so they can't swallow it or convert it (for example,
|
|
1055
|
+
* into a `UserException`), and the action doesn't count as failed for `isFailed` and
|
|
1056
|
+
* `exceptionFor`. The action still finishes: its `after()` runs, and its status is
|
|
1057
|
+
* `isCompletedFailed`, with that `StoreException` as its error.
|
|
1058
|
+
*
|
|
1059
|
+
* Note Kiss can only know `before()` or `reduce()` is async after calling it. So, when
|
|
1060
|
+
* `dispatchSync` throws because of an async action, the async method has already started,
|
|
1061
|
+
* and any side effects it has will still happen. Its result is ignored (it never changes the
|
|
1062
|
+
* state), and its errors are only logged. If the action uses `retry`, it's not retried. With
|
|
1063
|
+
* `unlimitedRetryCheckInternet`, the reducer doesn't run at all, since `dispatchSync` throws
|
|
1064
|
+
* while the internet is being checked.
|
|
1065
|
+
*
|
|
1066
|
+
* See also:
|
|
1067
|
+
* - `dispatch` which dispatches both sync and async actions.
|
|
1068
|
+
* - `dispatchAndWait` which dispatches both sync and async actions, and returns a Promise.
|
|
1069
|
+
* - `dispatchAndWaitAll` which dispatches all given actions, and returns a Promise.
|
|
1070
|
+
* - `dispatchAll` which dispatches all given actions in parallel.
|
|
1071
|
+
*/
|
|
1072
|
+
dispatchSync(action: KissAction<St>): void;
|
|
1073
|
+
private _throwIfNotReady;
|
|
1074
|
+
private _throwIfAlreadyDispatched;
|
|
1075
|
+
private _mustAbortDispatch;
|
|
1076
|
+
private _mustAbortAfterDebounce;
|
|
1077
|
+
private _isBlockedByKey;
|
|
1078
|
+
private static _isNonReentrant;
|
|
1079
|
+
private _isNonReentrantKeyRunning;
|
|
1080
|
+
private _isThrottled;
|
|
1081
|
+
private _removeExpiredThrottleLocks;
|
|
1082
|
+
private _releaseThrottleLock;
|
|
1083
|
+
private _isFresh;
|
|
1084
|
+
private _removeExpiredFreshKeys;
|
|
1085
|
+
private _releaseFreshKey;
|
|
1086
|
+
private _removeAllOptimisticSyncKeys;
|
|
1087
|
+
private _enterSequentialQueue;
|
|
1088
|
+
private _runOnSequentialTurn;
|
|
1089
|
+
private _releaseSequentialQueue;
|
|
1090
|
+
private _discardAllSequentialQueues;
|
|
1091
|
+
private _mockActionOrNot;
|
|
1092
|
+
private _processDispatch;
|
|
1093
|
+
private _wraps;
|
|
1094
|
+
private _wrapsAsync;
|
|
1095
|
+
private _processWrapsError;
|
|
1096
|
+
/**
|
|
1097
|
+
* Aborts the action silently, because of an `AbortDispatchException`: the action doesn't
|
|
1098
|
+
* count as failed, nothing is thrown, and its status has no `wrappedError`.
|
|
1099
|
+
*/
|
|
1100
|
+
private _abortSilently;
|
|
1101
|
+
/**
|
|
1102
|
+
* Gives the error to the `errorObserver`, and returns the error it returns, or the error
|
|
1103
|
+
* it throws. Returns `null` if the error is meant to be swallowed. The `action` is `null` for
|
|
1104
|
+
* the errors of the `Persistor`.
|
|
1105
|
+
*/
|
|
1106
|
+
private _observeError;
|
|
1107
|
+
private _processWrapsFinally;
|
|
1108
|
+
private _observeAction;
|
|
1109
|
+
private _observeState;
|
|
1110
|
+
private _checkAllActionConditions;
|
|
1111
|
+
private _discardAsyncResultOfDispatchSync;
|
|
1112
|
+
private _notSyncError;
|
|
1113
|
+
private _runFromStart;
|
|
1114
|
+
/**
|
|
1115
|
+
* Returns the action's own `wrapReduce`, or, when polling, debounce, retry or
|
|
1116
|
+
* unlimitedRetryCheckInternet is on, a wrapper that calls the action's own `wrapReduce`.
|
|
1117
|
+
* With `ignoreOld`, the result is then checked by `_ignoreOldResult`.
|
|
1118
|
+
* Never replaces `action.wrapReduce`.
|
|
1119
|
+
*
|
|
1120
|
+
* Note: An `OptimisticCommand` handles its own retry, by retrying only `sendCommandToServer`,
|
|
1121
|
+
* so that the optimistic state is not applied and rolled back on each attempt.
|
|
1122
|
+
* Debounce can't be combined with retry, nor used in an `OptimisticCommand`.
|
|
1123
|
+
* Polling can't be combined with debounce or retry, nor used in an `OptimisticCommand`.
|
|
1124
|
+
* unlimitedRetryCheckInternet can't be combined with any of these.
|
|
1125
|
+
*/
|
|
1126
|
+
private _getWrapReduce;
|
|
1127
|
+
private _abortError;
|
|
1128
|
+
private _getFeaturesWrapReduce;
|
|
1129
|
+
private static _syncRetryError;
|
|
1130
|
+
private static _nullThrownError;
|
|
1131
|
+
private _abortOlderIgnoreOld;
|
|
1132
|
+
private _pollSkipsReduce;
|
|
1133
|
+
/**
|
|
1134
|
+
* Checks the result of the reducer, so that if the action was aborted, it doesn't change the
|
|
1135
|
+
* state. The check is done right when the state is applied: now, for the state returned by
|
|
1136
|
+
* a SYNC reducer, or, for the function `(state) => newState` returned by an ASYNC reducer,
|
|
1137
|
+
* when the store calls it.
|
|
1138
|
+
*/
|
|
1139
|
+
private _discardIfAborted;
|
|
1140
|
+
/**
|
|
1141
|
+
* Waits for the debounce period, and then calls the action's own `wrapReduce`. If another
|
|
1142
|
+
* action with the same lock is dispatched meanwhile, returns `null` right away instead, so
|
|
1143
|
+
* that the action finishes without running its reducer. When the debounce period ends, the
|
|
1144
|
+
* action is aborted if its non-reentrant key, throttle lock or fresh-key is taken.
|
|
1145
|
+
*
|
|
1146
|
+
* The result is always ASYNC. A SYNC reducer's new state is returned as `() => newState`.
|
|
1147
|
+
*/
|
|
1148
|
+
private _debounceWrapReduce;
|
|
1149
|
+
/**
|
|
1150
|
+
* Resolves with `true` after the debounce period, if no other action with the same lock was
|
|
1151
|
+
* dispatched meanwhile. Otherwise, resolves with `false` as soon as the other action is
|
|
1152
|
+
* dispatched (or the locks are removed).
|
|
1153
|
+
*/
|
|
1154
|
+
private _waitDebounce;
|
|
1155
|
+
/**
|
|
1156
|
+
* Starts, stops or restarts the polling of the action's polling key, according to its `poll`
|
|
1157
|
+
* value, and runs the action's own `wrapReduce` when the `poll` value says the reducer runs.
|
|
1158
|
+
* Otherwise, returns `null` synchronously, so that the action finishes without changing the
|
|
1159
|
+
* state.
|
|
1160
|
+
*/
|
|
1161
|
+
private _pollingWrapReduce;
|
|
1162
|
+
/**
|
|
1163
|
+
* Starts a new polling cycle for the given `key`, and runs the `reduce` once, immediately.
|
|
1164
|
+
*
|
|
1165
|
+
* When `pollWaitsForRun` is true, the first tick is only scheduled after that immediate run
|
|
1166
|
+
* finishes (even if it fails). When it's false, the first tick is scheduled right away.
|
|
1167
|
+
*/
|
|
1168
|
+
private _runNowAndStartPollingCycle;
|
|
1169
|
+
/**
|
|
1170
|
+
* Schedules a one-shot timer that dispatches the action returned by `createPollingAction()`,
|
|
1171
|
+
* and then schedules the next tick. If `pollWaitsForRun` is true, the next tick is only
|
|
1172
|
+
* scheduled when the dispatched action finishes. Otherwise, it's scheduled as soon as the
|
|
1173
|
+
* action is dispatched.
|
|
1174
|
+
*
|
|
1175
|
+
* If the `cycle` is no longer active (because polling was stopped or restarted in the
|
|
1176
|
+
* meantime), the tick is abandoned.
|
|
1177
|
+
*/
|
|
1178
|
+
private _scheduleNextPollingTick;
|
|
1179
|
+
private _pollingCanTick;
|
|
1180
|
+
private _waitToTick;
|
|
1181
|
+
private _stopPollingCycle;
|
|
1182
|
+
private _retryWrapReduce;
|
|
1183
|
+
private _record;
|
|
1184
|
+
private _runAsyncBeforeOnwards;
|
|
1185
|
+
private _runAsyncReduceOnwards;
|
|
1186
|
+
private _isFunction;
|
|
1187
|
+
/**
|
|
1188
|
+
* Called when the action finished with no error, but did not change the state: the reducer
|
|
1189
|
+
* returned null or the unchanged state, or `abortReduce()` returned true. The state-observer
|
|
1190
|
+
* is still called, with the same `prevState` and `newState`. And if the action is a
|
|
1191
|
+
* `PersistAction`, the state is persisted right away.
|
|
1192
|
+
*/
|
|
1193
|
+
private _registerUnchangedState;
|
|
1194
|
+
private _registerState;
|
|
1195
|
+
/**
|
|
1196
|
+
* For this to work, we have to pass a function `showUserException` in the constructor
|
|
1197
|
+
* of the Store, that will open some UI to show the error to the user.
|
|
1198
|
+
*
|
|
1199
|
+
* 1) If the UI is still open, don't do anything.
|
|
1200
|
+
* 2) If no UI is open, check to see if any errors are in the queue.
|
|
1201
|
+
* If so, remove the first error from the queue and show it in the UI.
|
|
1202
|
+
* 3) In this case, we mark the UI as open, and count the number of errors still in the queue.
|
|
1203
|
+
* 4) When the UI calls `next()`, we mark the UI as closed, and call the method again, to
|
|
1204
|
+
* process the next error in the queue, if any.
|
|
1205
|
+
*
|
|
1206
|
+
* IMPORTANT: Failing to call `next()` will prevent the UI to ever opening again, because
|
|
1207
|
+
* Kiss will think the previous UI is still open. In contrast, calling `next()` as soon
|
|
1208
|
+
* as the UI opens may result in opening more than one UI on top pf each other.
|
|
1209
|
+
* Also, calling `next()` when there are no more errors in the queue will do nothing.
|
|
1210
|
+
*
|
|
1211
|
+
* An example using React Native:
|
|
1212
|
+
*
|
|
1213
|
+
* ```ts
|
|
1214
|
+
* const showUserException = (exception: UserException, count: number, next: () => void) => {
|
|
1215
|
+
* Alert.alert(
|
|
1216
|
+
* exception.title || exception.message,
|
|
1217
|
+
* exception.title ? exception.message : '',
|
|
1218
|
+
* [{ text: 'OK', onPress: (_value?: string) => next() }]
|
|
1219
|
+
* );
|
|
1220
|
+
* };
|
|
1221
|
+
* ```
|
|
1222
|
+
*/
|
|
1223
|
+
private _openSomeUiToShowUserException;
|
|
1224
|
+
private _isUserExceptionUiOpen;
|
|
1225
|
+
private _processPersistorError;
|
|
1226
|
+
private _addUserException;
|
|
1227
|
+
/**
|
|
1228
|
+
* Aborts all the actions of the given `type` (or of a subclass) that are in progress. Each
|
|
1229
|
+
* one finishes silently: its `abortSignal` is aborted, its result is ignored (it doesn't
|
|
1230
|
+
* change the state), and so are its errors (no error dialog, it doesn't count as failed, and
|
|
1231
|
+
* it isn't retried). Its status has `isDispatchAborted: true`, and `dispatchAndWait`
|
|
1232
|
+
* resolves. Inside the action, `isAborted` is `true`.
|
|
1233
|
+
*
|
|
1234
|
+
* For example, to cancel the request a component started, when the component unmounts:
|
|
1235
|
+
*
|
|
1236
|
+
* ```ts
|
|
1237
|
+
* useStoreEffect({
|
|
1238
|
+
* onMount: (store) => store.dispatch(new LoadUser(userId)),
|
|
1239
|
+
* onUnmount: (store) => store.abort(LoadUser),
|
|
1240
|
+
* });
|
|
1241
|
+
* ```
|
|
1242
|
+
*
|
|
1243
|
+
* Or from a "Cancel" button: `onClick={() => store.abort(UploadFile)}`.
|
|
1244
|
+
*
|
|
1245
|
+
* - A request that uses the action's `abortSignal` is cancelled right away. Otherwise, the
|
|
1246
|
+
* action is still in progress (`isWaiting` is `true`) until the request finishes, and
|
|
1247
|
+
* only then its result is ignored.
|
|
1248
|
+
* - An action aborted during its `before` method doesn't run its reducer. One aborted while
|
|
1249
|
+
* waiting to retry stops retrying.
|
|
1250
|
+
* - It does nothing if no action of the type is in progress.
|
|
1251
|
+
*
|
|
1252
|
+
* Pass `{ statusKey }` to abort only the actions with that `statusKey` (see
|
|
1253
|
+
* `KissAction.statusKey`): `store.abort(LoadUser, { statusKey: userId })`.
|
|
1254
|
+
*
|
|
1255
|
+
* To abort a single action instance, use its `abort()` method.
|
|
1256
|
+
*/
|
|
1257
|
+
abort<T extends KissAction<St>>(type: abstract new (...args: any[]) => T, options?: StatusOptions): void;
|
|
1258
|
+
/**
|
|
1259
|
+
* You can use `isWaiting` and pass it an action `type`:
|
|
1260
|
+
* - It returns true if an action of the given type (or of a subclass) is currently being processed.
|
|
1261
|
+
* - It returns false if no action of the given type is currently being processed.
|
|
1262
|
+
* - This is only useful for ASYNC actions. A SYNC action finishes before `dispatch` returns, so
|
|
1263
|
+
* outside of it, `isWaiting` is always `false` for it, and components never see it as `true`.
|
|
1264
|
+
* - Inside an action, `isWaiting` of the action's own type is always `true` (even when the action
|
|
1265
|
+
* is SYNC), since the action itself is being processed.
|
|
1266
|
+
*
|
|
1267
|
+
* Note an action is ASYNC if it returns a promise from its `before` OR its `reduce` methods.
|
|
1268
|
+
*
|
|
1269
|
+
* ```ts
|
|
1270
|
+
* store.dispatch(new MyAction());
|
|
1271
|
+
* if (store.isWaiting(MyAction)) { ... } // Show a spinner
|
|
1272
|
+
* ```
|
|
1273
|
+
*
|
|
1274
|
+
* Pass `{ statusKey }` to consider only the actions with that `statusKey` (see
|
|
1275
|
+
* `KissAction.statusKey`): `store.isWaiting(LoadUser, { statusKey: userId })`.
|
|
1276
|
+
*/
|
|
1277
|
+
isWaiting<T extends KissAction<St>>(type: abstract new (...args: any[]) => T, options?: StatusOptions): boolean;
|
|
1278
|
+
/**
|
|
1279
|
+
* Returns true if the given action `type` failed, with any error, not only with a
|
|
1280
|
+
* `UserException`. Use `exceptionFor` to get the error, if it was a `UserException`.
|
|
1281
|
+
* An error swallowed by `wrapError` or the `errorObserver` (they returned `null`), and
|
|
1282
|
+
* an `AbortDispatchException`, don't count as failures.
|
|
1283
|
+
* Note: This method uses the EXACT action type. Subtypes are not considered.
|
|
1284
|
+
*
|
|
1285
|
+
* Pass `{ statusKey }` to consider only the failures with that `statusKey` (see
|
|
1286
|
+
* `KissAction.statusKey`): `store.isFailed(LoadUser, { statusKey: userId })`.
|
|
1287
|
+
*/
|
|
1288
|
+
isFailed<T extends KissAction<any>>(type: {
|
|
1289
|
+
new (...args: any[]): T;
|
|
1290
|
+
}, options?: StatusOptions): boolean;
|
|
1291
|
+
/**
|
|
1292
|
+
* Returns the `UserException` of the `type` that failed. Returns `null` if it didn't fail,
|
|
1293
|
+
* or if it failed with an error that is not a `UserException` (in that case, `isFailed` is
|
|
1294
|
+
* still true).
|
|
1295
|
+
* Note: This method uses the EXACT type in `type`. Subtypes are not considered.
|
|
1296
|
+
*
|
|
1297
|
+
* Pass `{ statusKey }` to get the exception of the failure with that `statusKey` (see
|
|
1298
|
+
* `KissAction.statusKey`): `store.exceptionFor(LoadUser, { statusKey: userId })`. Without it,
|
|
1299
|
+
* if actions with different keys failed, you get the `UserException` of the most recent
|
|
1300
|
+
* failure.
|
|
1301
|
+
*/
|
|
1302
|
+
exceptionFor<T extends KissAction<St>>(type: {
|
|
1303
|
+
new (...args: any[]): T;
|
|
1304
|
+
}, options?: StatusOptions): (UserException | null);
|
|
1305
|
+
/**
|
|
1306
|
+
* Removes the exact given action `type` from the list of action types that failed.
|
|
1307
|
+
* Note it clears the EXACT given type. Subtypes are not considered.
|
|
1308
|
+
*
|
|
1309
|
+
* Even if you never call this method explicitly, just dispatching an action already clears that action type
|
|
1310
|
+
* from the list of failing action types. But you can call this method explicitly if you want to clear the
|
|
1311
|
+
* action type before it's used again.
|
|
1312
|
+
*
|
|
1313
|
+
* Usage:
|
|
1314
|
+
* ```ts
|
|
1315
|
+
* store.clearExceptionFor(MyAction);
|
|
1316
|
+
* ```
|
|
1317
|
+
*
|
|
1318
|
+
* Pass `{ statusKey }` to clear only the failure with that `statusKey` (see
|
|
1319
|
+
* `KissAction.statusKey`): `store.clearExceptionFor(LoadUser, { statusKey: userId })`.
|
|
1320
|
+
* Without it, all the failures of the type are cleared, whatever their keys.
|
|
1321
|
+
*/
|
|
1322
|
+
clearExceptionFor<T extends KissAction<St>>(type: {
|
|
1323
|
+
new (...args: any[]): T;
|
|
1324
|
+
}, options?: StatusOptions): void;
|
|
1325
|
+
/**
|
|
1326
|
+
* Clears the failures of all action types, so that `isFailed` is `false` and `exceptionFor`
|
|
1327
|
+
* is `null` for all of them. The components that use them are updated.
|
|
1328
|
+
*
|
|
1329
|
+
* You rarely need to call this yourself, since `clearInternalActionProps()` already calls it,
|
|
1330
|
+
* and so do `logOut()` and the shutdown of the store (`setShutDown(true)` and `dispose()`).
|
|
1331
|
+
* The failures kept for actions with a `statusKey` (one per key that failed) are also
|
|
1332
|
+
* released here. See `KissAction.statusKey`.
|
|
1333
|
+
*/
|
|
1334
|
+
clearAllExceptions(): void;
|
|
1335
|
+
/**
|
|
1336
|
+
* Resolves and removes the `useSuspense` promises whose action is not in progress anymore.
|
|
1337
|
+
* Called when an action leaves `_actionsInProgress`. Costs nothing while the list is empty.
|
|
1338
|
+
*/
|
|
1339
|
+
private _resolveSuspenseWaits;
|
|
1340
|
+
/**
|
|
1341
|
+
* Returns the failure of the given `type` that matches `options.statusKey`, or the most
|
|
1342
|
+
* recent failure of the type when there is no key. Returns `null` if there is none.
|
|
1343
|
+
*/
|
|
1344
|
+
private _findFailure;
|
|
1345
|
+
/** Records a failure, replacing the previous failure of the same type and `statusKey`, if any. */
|
|
1346
|
+
private _addFailure;
|
|
1347
|
+
/** Removes the failure of the given type and `statusKey`. Returns true if there was one. */
|
|
1348
|
+
private _removeFailure;
|
|
1349
|
+
/**
|
|
1350
|
+
* Returns true if the event was already handled (spent) by a component of this store, with
|
|
1351
|
+
* `useEvent` or `useLayoutEvent`. Also true for `Evt.spent()` and `EvtState.spent()`, and
|
|
1352
|
+
* for restored (persisted) events, which are spent in every store.
|
|
1353
|
+
*
|
|
1354
|
+
* Useful in component tests, to check that a component handled the event:
|
|
1355
|
+
*
|
|
1356
|
+
* ```ts
|
|
1357
|
+
* await store.dispatchAndWait(new ScrollFeedToTop());
|
|
1358
|
+
* expect(store.isEventSpent(store.state.scrollToTopEvt)).toBe(true);
|
|
1359
|
+
* ```
|
|
1360
|
+
*
|
|
1361
|
+
* Note an `EvtState` is never spent by components, so for it this only returns true for
|
|
1362
|
+
* `EvtState.spent()` and restored events.
|
|
1363
|
+
*/
|
|
1364
|
+
isEventSpent(evt: Evt<any> | EvtState<any>): boolean;
|
|
1365
|
+
/**
|
|
1366
|
+
* The environment the app is running in (production, staging, testing, etc), as given to the
|
|
1367
|
+
* store constructor, or `undefined`. It never changes while the app runs. Typed `unknown`:
|
|
1368
|
+
* declare a typed getter in your base action, like
|
|
1369
|
+
* `get env() { return this.store.environment as Environment; }`. In components, use
|
|
1370
|
+
* `useEnvironment()`.
|
|
1371
|
+
*
|
|
1372
|
+
* See also: `configuration` and `dependencies`. And `setProp`, for the values that may
|
|
1373
|
+
* change while the app runs.
|
|
1374
|
+
*/
|
|
1375
|
+
get environment(): unknown;
|
|
1376
|
+
/**
|
|
1377
|
+
* The configuration of the app (feature flags and other configuration values), created by
|
|
1378
|
+
* the `configuration` factory given to the store constructor, or `undefined`. It's meant to
|
|
1379
|
+
* be immutable. Typed `unknown`: declare a typed getter in your base action, like
|
|
1380
|
+
* `get config() { return this.store.configuration as Config; }`. In components, use
|
|
1381
|
+
* `useConfiguration()`.
|
|
1382
|
+
*
|
|
1383
|
+
* `store.dispose()` disposes it, if it has a `[Symbol.asyncDispose]()`, `[Symbol.dispose]()`
|
|
1384
|
+
* or `dispose()` method. `logOut` keeps it.
|
|
1385
|
+
*
|
|
1386
|
+
* See also: `environment` and `dependencies`.
|
|
1387
|
+
*/
|
|
1388
|
+
get configuration(): unknown;
|
|
1389
|
+
/**
|
|
1390
|
+
* The dependencies of the app (services, repositories, API clients, etc), created by the
|
|
1391
|
+
* `dependencies` factory given to the store constructor, or `undefined`. They belong to the
|
|
1392
|
+
* store: each test creates its own store with its own dependencies. Typed `unknown`: declare
|
|
1393
|
+
* a typed getter in your base action, like
|
|
1394
|
+
* `get deps() { return this.store.dependencies as Dependencies; }`. Components should
|
|
1395
|
+
* usually not need them.
|
|
1396
|
+
*
|
|
1397
|
+
* `store.dispose()` disposes them, if they have a `[Symbol.asyncDispose]()`,
|
|
1398
|
+
* `[Symbol.dispose]()` or `dispose()` method. `logOut` keeps them, since they belong to the
|
|
1399
|
+
* app, and not to the user.
|
|
1400
|
+
*
|
|
1401
|
+
* See also: `environment` and `configuration`. And `mocks`, which mocks actions, while the
|
|
1402
|
+
* dependencies mock services.
|
|
1403
|
+
*/
|
|
1404
|
+
get dependencies(): unknown;
|
|
1405
|
+
/**
|
|
1406
|
+
* Internal: Disposes the dependencies or the configuration, when they have a
|
|
1407
|
+
* `[Symbol.asyncDispose]()`, `[Symbol.dispose]()` or `dispose()` method. Returns a promise
|
|
1408
|
+
* if the disposal is async, or `null`. Neither throws nor rejects: the errors are reported
|
|
1409
|
+
* with `_reportDisposeError`.
|
|
1410
|
+
*/
|
|
1411
|
+
private _disposeInjected;
|
|
1412
|
+
/**
|
|
1413
|
+
* Sets a prop in the store. The props hold the long-lived things of the app that are not
|
|
1414
|
+
* state, and must not go in the state: intervals and timeouts, WebSockets, `EventSource`s,
|
|
1415
|
+
* `AbortController`s, listeners (like Firebase's `onSnapshot`, which returns an unsubscribe
|
|
1416
|
+
* function), RxJS subscriptions. The props belong to the store, so that each test, or each
|
|
1417
|
+
* request in server-side rendering, has its own, and so that they can all be closed at
|
|
1418
|
+
* once, with `store.dispose()` (when the app finishes, or at the end of each test), or
|
|
1419
|
+
* with `disposeProps()` and `logOut({ disposeProps: true })`.
|
|
1420
|
+
*
|
|
1421
|
+
* The `key` can be any value. Keys are compared by identity, like the keys of a `Map`, so
|
|
1422
|
+
* equal strings are the same key, but two different objects are not. Read the prop with
|
|
1423
|
+
* `prop(key)`, and remove it with `disposeProp(key)`, which also disposes its value.
|
|
1424
|
+
*
|
|
1425
|
+
* Setting a prop with a key that already exists replaces its value, and disposes the previous
|
|
1426
|
+
* value (if it's a different one), so that dispatching a "start" action twice doesn't leak the
|
|
1427
|
+
* first timer or listener. Setting the same value again only updates its `dispose` option.
|
|
1428
|
+
*
|
|
1429
|
+
* This is also available in actions, as `this.setProp()`:
|
|
1430
|
+
*
|
|
1431
|
+
* ```ts
|
|
1432
|
+
* class StartClock extends Action {
|
|
1433
|
+
* reduce() {
|
|
1434
|
+
* const interval = setInterval(() => this.dispatch(new Tick()), 1000);
|
|
1435
|
+
* this.setProp('clock', interval, { dispose: clearInterval });
|
|
1436
|
+
* return null;
|
|
1437
|
+
* }
|
|
1438
|
+
* }
|
|
1439
|
+
*
|
|
1440
|
+
* class StopClock extends Action {
|
|
1441
|
+
* reduce() {
|
|
1442
|
+
* this.disposeProp('clock');
|
|
1443
|
+
* return null;
|
|
1444
|
+
* }
|
|
1445
|
+
* }
|
|
1446
|
+
* ```
|
|
1447
|
+
*
|
|
1448
|
+
* How a prop is disposed, in order:
|
|
1449
|
+
*
|
|
1450
|
+
* 1. The `dispose` option given to `setProp`, if any, which gets the value.
|
|
1451
|
+
* 2. The value's `[Symbol.asyncDispose]()` or `[Symbol.dispose]()` method (TC39 explicit
|
|
1452
|
+
* resource management, supported by TypeScript's `using`).
|
|
1453
|
+
* 3. An `AbortController` is aborted.
|
|
1454
|
+
* 4. A `WebSocket`, `EventSource`, `BroadcastChannel` or `MessagePort` is closed, and a
|
|
1455
|
+
* `Worker` is terminated.
|
|
1456
|
+
* 5. An object with an `unsubscribe()` method (like an RxJS subscription) is unsubscribed.
|
|
1457
|
+
* 6. Otherwise, the value can't be disposed, and it's only removed from the props.
|
|
1458
|
+
*
|
|
1459
|
+
* Note:
|
|
1460
|
+
* - Timers need the `dispose` option, since in browsers `setTimeout` and `setInterval` return
|
|
1461
|
+
* numbers, which can't be told apart from other numbers. Use `{ dispose: clearInterval }` or
|
|
1462
|
+
* `{ dispose: clearTimeout }`.
|
|
1463
|
+
* - Functions are not called automatically, since an unsubscribe function looks the same as a
|
|
1464
|
+
* callback kept in a prop. Use `{ dispose: (unsubscribe) => unsubscribe() }`.
|
|
1465
|
+
* - Promises can't be cancelled, so they're only removed. Keep an `AbortController` in a prop
|
|
1466
|
+
* to cancel the work.
|
|
1467
|
+
* - A `dispose` that throws, or returns a promise that rejects, doesn't stop the other props
|
|
1468
|
+
* from being disposed. The error is given to the `errorObserver` (with a `null` action),
|
|
1469
|
+
* and if there is none, or if it returns an error, the error is logged.
|
|
1470
|
+
* - After `store.dispose()`, a prop that is set (for example, by an async action that was
|
|
1471
|
+
* still running) is disposed right away, and not kept.
|
|
1472
|
+
*
|
|
1473
|
+
* See also: `prop`, `props`, `disposeProp`, `disposeProps` and `dispose`.
|
|
1474
|
+
*/
|
|
1475
|
+
setProp<V>(key: unknown, value: V, options?: SetPropOptions<V>): void;
|
|
1476
|
+
/**
|
|
1477
|
+
* Returns the prop with the given key, or `undefined` if there is none. See `setProp`.
|
|
1478
|
+
*
|
|
1479
|
+
* Give it the type of the value: `const interval = store.prop<number>('clock')`.
|
|
1480
|
+
*
|
|
1481
|
+
* This is also available in actions, as `this.prop()`.
|
|
1482
|
+
*/
|
|
1483
|
+
prop<V = unknown>(key: unknown): V | undefined;
|
|
1484
|
+
/**
|
|
1485
|
+
* All the props of the store (see `setProp`), with their keys and values, in the order they
|
|
1486
|
+
* were set. It's a read-only view: use `setProp`, `disposeProp` and `disposeProps` to change
|
|
1487
|
+
* them. Useful in tests.
|
|
1488
|
+
*
|
|
1489
|
+
* This is also available in actions, as `this.props`.
|
|
1490
|
+
*/
|
|
1491
|
+
get props(): ReadonlyMap<unknown, unknown>;
|
|
1492
|
+
/**
|
|
1493
|
+
* Disposes the prop with the given key, and removes it from the props. See `setProp` for
|
|
1494
|
+
* how the value is disposed. Does nothing if there is no prop with that key.
|
|
1495
|
+
*
|
|
1496
|
+
* It doesn't wait for an async disposal (one that returns a promise) to finish. If that
|
|
1497
|
+
* disposal fails, the error is given to the `errorObserver` (with a `null` action), or logged.
|
|
1498
|
+
* Use `store.dispose()` to wait for all the async disposals.
|
|
1499
|
+
*
|
|
1500
|
+
* This is also available in actions, as `this.disposeProp()`.
|
|
1501
|
+
*
|
|
1502
|
+
* ```ts
|
|
1503
|
+
* class StopClock extends Action {
|
|
1504
|
+
* reduce() {
|
|
1505
|
+
* this.disposeProp('clock');
|
|
1506
|
+
* return null;
|
|
1507
|
+
* }
|
|
1508
|
+
* }
|
|
1509
|
+
* ```
|
|
1510
|
+
*/
|
|
1511
|
+
disposeProp(key: unknown): void;
|
|
1512
|
+
/**
|
|
1513
|
+
* Disposes the props, and removes them from the props. See `setProp` for how the values are
|
|
1514
|
+
* disposed. The values that can't be disposed are only removed.
|
|
1515
|
+
*
|
|
1516
|
+
* - Without a `predicate`, all the props are disposed and removed.
|
|
1517
|
+
* - With a `predicate`, it's called with the key and the value of each prop, and only the
|
|
1518
|
+
* props for which it returns `true` are disposed and removed. The others are kept, and not
|
|
1519
|
+
* disposed.
|
|
1520
|
+
*
|
|
1521
|
+
* The props are disposed in the order they were set. A disposal that throws doesn't stop the
|
|
1522
|
+
* others. It doesn't wait for the async disposals (the ones that return a promise) to finish.
|
|
1523
|
+
* Use `store.dispose()` to wait for them.
|
|
1524
|
+
*
|
|
1525
|
+
* A `predicate` that throws for a prop is treated like a disposal that throws: the error is
|
|
1526
|
+
* given to the `errorObserver` (or logged), the prop is kept, and the other props are still
|
|
1527
|
+
* disposed. This method never throws.
|
|
1528
|
+
*
|
|
1529
|
+
* This is also available in actions, as `this.disposeProps()`.
|
|
1530
|
+
*
|
|
1531
|
+
* ```ts
|
|
1532
|
+
* // Disposes all props.
|
|
1533
|
+
* store.disposeProps();
|
|
1534
|
+
*
|
|
1535
|
+
* // Disposes only the props whose key starts with 'user'.
|
|
1536
|
+
* store.disposeProps((key, value) => String(key).startsWith('user'));
|
|
1537
|
+
* ```
|
|
1538
|
+
*
|
|
1539
|
+
* See also: `disposeProp`, to dispose a single prop by its key, and `logOut`, which can
|
|
1540
|
+
* dispose the props with its `disposeProps` option.
|
|
1541
|
+
*/
|
|
1542
|
+
disposeProps(predicate?: PropPredicate): void;
|
|
1543
|
+
/**
|
|
1544
|
+
* Internal: Disposes the selected props (all of them, without a predicate), and returns a
|
|
1545
|
+
* promise that resolves when their async disposals finish. It never rejects: the errors are
|
|
1546
|
+
* reported with `_reportDisposeError`.
|
|
1547
|
+
*/
|
|
1548
|
+
private _disposeProps;
|
|
1549
|
+
/**
|
|
1550
|
+
* Internal: Disposes the value of a prop, as described in `setProp`. Returns a promise if the
|
|
1551
|
+
* disposal is async, or `null`. Neither throws nor rejects: the errors are reported with
|
|
1552
|
+
* `_reportDisposeError`.
|
|
1553
|
+
*/
|
|
1554
|
+
private _disposeValue;
|
|
1555
|
+
/**
|
|
1556
|
+
* Internal: Disposes the value with the TC39 explicit resource management protocol, if it
|
|
1557
|
+
* has a `[Symbol.asyncDispose]()` or `[Symbol.dispose]()` method, and returns the result of
|
|
1558
|
+
* the disposal (a promise, if it's async), wrapped. Returns `null` if it has neither method.
|
|
1559
|
+
* The wrapper is needed because a disposal may legitimately return `undefined`.
|
|
1560
|
+
*/
|
|
1561
|
+
private static _disposeBySymbol;
|
|
1562
|
+
/**
|
|
1563
|
+
* Internal: Disposes the value by its type (steps 2 to 5 of `setProp`), and returns the
|
|
1564
|
+
* result of the disposal (a promise, if it's async). Returns `undefined` if the value can't
|
|
1565
|
+
* be disposed.
|
|
1566
|
+
*/
|
|
1567
|
+
private static _disposeByType;
|
|
1568
|
+
private static _isInstanceOfGlobal;
|
|
1569
|
+
private static _isPromiseLike;
|
|
1570
|
+
private _reportDisposeError;
|
|
1571
|
+
private static _describeKey;
|
|
1572
|
+
/**
|
|
1573
|
+
* Merges a slice of state, loaded on the server for a route, into the store state,
|
|
1574
|
+
* synchronously, by dispatching a `MergeServerStateAction`. In a server-rendered app with
|
|
1575
|
+
* several routes, each route's server code loads its own data (a server component in the
|
|
1576
|
+
* Next.js app router, `getServerSideProps` in the pages router, a loader in Remix), and the
|
|
1577
|
+
* `StoreSlice` component gives it to the store with this method, on the server and on the
|
|
1578
|
+
* client, so that the HTML shows it, hydration matches, and client-side navigation shows the
|
|
1579
|
+
* new route's data in its first render. See "Server-side rendering" in the README.
|
|
1580
|
+
*
|
|
1581
|
+
* How the slice is merged: the `mergeServerState` option of the constructor, or, without it,
|
|
1582
|
+
* a shallow merge for plain-object states (`{ ...state, ...slice }`). When nothing changes,
|
|
1583
|
+
* the state is kept (same reference), and nothing is published.
|
|
1584
|
+
*
|
|
1585
|
+
* The server data wins: a field in the slice replaces the field in the state, even if the
|
|
1586
|
+
* client changed it. To keep client fields, use the `mergeServerState` option.
|
|
1587
|
+
*
|
|
1588
|
+
* Before the store is ready (a persistor still reading the persisted state), the slice is
|
|
1589
|
+
* merged right away, and merged again into the persisted state when it arrives, so that the
|
|
1590
|
+
* route's server data is not lost. The persistor may persist the merged state.
|
|
1591
|
+
*/
|
|
1592
|
+
mergeServerState(slice: object): void;
|
|
1593
|
+
private _mergeServerState;
|
|
1594
|
+
/**
|
|
1595
|
+
* True after `dispose()` was called. A disposed store doesn't accept new actions, and
|
|
1596
|
+
* doesn't keep new props.
|
|
1597
|
+
*/
|
|
1598
|
+
private readonly _isServer;
|
|
1599
|
+
/**
|
|
1600
|
+
* True if the store was created with `server: true` (see the `server` option in
|
|
1601
|
+
* `createStore`): it's the store that loads the data of a request, in server-side rendering.
|
|
1602
|
+
* In actions, `this.store.isServer` lets you skip client-only work on the server, for example
|
|
1603
|
+
* not opening a WebSocket, or not starting an interval with `setProp`. It's `false` by
|
|
1604
|
+
* default, and in the store the components use.
|
|
1605
|
+
*/
|
|
1606
|
+
get isServer(): boolean;
|
|
1607
|
+
get isDisposed(): boolean;
|
|
1608
|
+
/**
|
|
1609
|
+
* Ends the store, for tests and app shutdown:
|
|
1610
|
+
*
|
|
1611
|
+
* ```ts
|
|
1612
|
+
* afterEach(() => store.dispose());
|
|
1613
|
+
* ```
|
|
1614
|
+
*
|
|
1615
|
+
* It does the following, in order:
|
|
1616
|
+
*
|
|
1617
|
+
* 1. Shuts down the store (`setShutDown(true)`), so that the actions dispatched from now on
|
|
1618
|
+
* are aborted. This also calls `clearInternalActionProps()`, which stops the polling, the
|
|
1619
|
+
* retries, the debounce timers, the sequential queues, and the other action features.
|
|
1620
|
+
* 2. Cancels the `dispatchWhen`s that are still waiting, so their actions are not dispatched,
|
|
1621
|
+
* and their `onTimeout` is not called. The `waitCondition`s, `waitActionCondition`s,
|
|
1622
|
+
* `waitAllActions`s and the other waits still pending are rejected with an
|
|
1623
|
+
* `AbortDispatchException`, and their timeouts are cancelled, so that no timer is left
|
|
1624
|
+
* behind. Inside an action, this aborts the action. Elsewhere, catch the rejection, or
|
|
1625
|
+
* don't dispose the store while you still wait for it.
|
|
1626
|
+
* 3. Pauses the persistor, and cancels its pending save (a state change waiting for the
|
|
1627
|
+
* throttle). A save that is already running still finishes. If you want the current state
|
|
1628
|
+
* saved first, call `await store.persistAndPause()` before.
|
|
1629
|
+
* 4. Stops listening to the focus and online sources (see `focusSource` and `onlineSource`
|
|
1630
|
+
* in the constructor), if they were started. `isFocused` and `isOnline` keep their last
|
|
1631
|
+
* values.
|
|
1632
|
+
* 5. Disposes all the props (see `setProp`), and removes them.
|
|
1633
|
+
* 6. Disposes the `dependencies`, and then the `configuration` (see the constructor), when
|
|
1634
|
+
* they have a `[Symbol.asyncDispose]()`, `[Symbol.dispose]()` or `dispose()` method. They
|
|
1635
|
+
* can still be read from the store after that.
|
|
1636
|
+
*
|
|
1637
|
+
* The returned promise resolves when the async disposals finish. It never rejects: a
|
|
1638
|
+
* disposal that fails is given to the `errorObserver`, or logged, and the other disposals
|
|
1639
|
+
* are still done.
|
|
1640
|
+
*
|
|
1641
|
+
* The store can't be used again: `setShutDown(false)` doesn't turn it back on, and a prop
|
|
1642
|
+
* set after it is disposed right away. Calling `dispose()` again does nothing. Note the
|
|
1643
|
+
* actions that are already running still finish, and may still change the state.
|
|
1644
|
+
*
|
|
1645
|
+
* The store also implements `[Symbol.dispose]` and `[Symbol.asyncDispose]`, so that tests
|
|
1646
|
+
* can use TypeScript's explicit resource management: `await using store = createStore(...)`.
|
|
1647
|
+
*/
|
|
1648
|
+
dispose(): Promise<void>;
|
|
1649
|
+
/**
|
|
1650
|
+
* Function `Store.describeStateChange()` returns a string describing only the differences between
|
|
1651
|
+
* two given objects.
|
|
1652
|
+
*
|
|
1653
|
+
* By default, when `logStateChanges` is set to `true` in the Store constructor, function
|
|
1654
|
+
* `describeStateChange` is used together with `Store.log()` to print all state changes to
|
|
1655
|
+
* the console. Note you should turn this off in production.
|
|
1656
|
+
*
|
|
1657
|
+
* The values can be of any type, including primitives (for a state that is a number or a
|
|
1658
|
+
* string), and objects with `Date`, `Map` and `Set` fields:
|
|
1659
|
+
*
|
|
1660
|
+
* ```ts
|
|
1661
|
+
* Store.describeStateChange(0, 1); // 'State: 0 → 1; '
|
|
1662
|
+
* Store.describeStateChange({ user: { name: 'Ann' } }, { user: { name: 'Bob' } }); // 'State user.name: Ann → Bob; '
|
|
1663
|
+
* Store.describeStateChange({ prices: new Map([['IBM', 1]]) }, { prices: new Map([['IBM', 2]]) }); // 'State prices[IBM]: 1 → 2; '
|
|
1664
|
+
* ```
|
|
1665
|
+
*/
|
|
1666
|
+
static describeStateChange(obj1: any, obj2: any, path?: string): string;
|
|
1667
|
+
private static _isObject;
|
|
1668
|
+
private static _describeValue;
|
|
1669
|
+
/**
|
|
1670
|
+
* Call `logOut()` when you want to delete the persisted state, and return the store
|
|
1671
|
+
* state to the given initial-state. That's usually necessary when the user logs out
|
|
1672
|
+
* of your app, or the user deletes its account, so that another user may log in,
|
|
1673
|
+
* or start a new sign-up process.
|
|
1674
|
+
*
|
|
1675
|
+
* Note: If you know about any timers or async processes that you may have started,
|
|
1676
|
+
* you should stop/cancel them all before calling this method.
|
|
1677
|
+
*
|
|
1678
|
+
* You may opt to:
|
|
1679
|
+
*
|
|
1680
|
+
* - Wait for `throttle` milliseconds to make sure all async processes that the app may
|
|
1681
|
+
* have started have time to finish. The default `throttle` is 3000 milliseconds (3 seconds).
|
|
1682
|
+
*
|
|
1683
|
+
* - Wait for all actions currently running to finish, but wait at most `actionsThrottle`
|
|
1684
|
+
* milliseconds. If the actions are not finished by then, the state will be deleted anyway.
|
|
1685
|
+
* The default `actionsThrottle` is 6000 milliseconds (6 seconds). An `actionsThrottle` of
|
|
1686
|
+
* zero (or negative) doesn't wait for the actions at all. Note: If you call logOut from inside
|
|
1687
|
+
* an action, and wait for it, that action is still running, so logOut waits the full
|
|
1688
|
+
* `actionsThrottle` for it to finish.
|
|
1689
|
+
*
|
|
1690
|
+
* The actions dispatched before logOut belong to the previous user, so they can't change the
|
|
1691
|
+
* state of the next user. When one of them finishes, during or after logOut, it's aborted
|
|
1692
|
+
* (`status.isDispatchAborted`): its reducer's result is discarded, and its errors are ignored
|
|
1693
|
+
* (they don't go to `wrapError` or the `errorObserver`, and are not shown to the user). Errors
|
|
1694
|
+
* that point to a bug (`StoreException`) are still thrown. Also, the store doesn't accept the
|
|
1695
|
+
* actions they dispatch after logOut, with `this.dispatch`, `this.dispatchAndWait`,
|
|
1696
|
+
* `this.dispatchWhen`, and the others. This includes the `UpdateStateAction`s of the
|
|
1697
|
+
* optimistic actions (like `OptimisticCommand`), so they don't apply the server response,
|
|
1698
|
+
* roll back, or reload, after logOut. Note this means an action that calls logOut can't
|
|
1699
|
+
* change the state or dispatch actions after it. If you need that, call logOut from outside
|
|
1700
|
+
* of actions, and then dispatch the actions you need. Also note it can't undo what the
|
|
1701
|
+
* actions do outside the store, like calling the server, or saving to local storage.
|
|
1702
|
+
*
|
|
1703
|
+
* While logOut is running, the store doesn't accept new actions, and the persistor doesn't
|
|
1704
|
+
* persist anything but the initial-state. This includes the actions dispatched by the
|
|
1705
|
+
* `stateObserver` and the `actionObserver` when logOut changes the state to the
|
|
1706
|
+
* initial-state. Also, logOut cancels the `dispatchWhen`s that are still waiting, so their
|
|
1707
|
+
* actions are not dispatched, and their `onTimeout` is not called. If the app pauses the persistor (`pausePersistor`)
|
|
1708
|
+
* or shuts down the store (`setShutDown`), before or during logOut, they stay that way after
|
|
1709
|
+
* it. Note logOut deletes the persisted state, and persists the initial-state, even if the
|
|
1710
|
+
* persistor is paused.
|
|
1711
|
+
*
|
|
1712
|
+
* If the store has no persistor, logOut does the same, except deleting and persisting.
|
|
1713
|
+
*
|
|
1714
|
+
* The store state is always changed to the initial-state, even if deleting the persisted
|
|
1715
|
+
* state fails, and logOut then still tries to persist the initial-state. If deleting or
|
|
1716
|
+
* persisting fails, the error is given to the `errorObserver` (with a `null` action), so it
|
|
1717
|
+
* can be logged, and when logOut finishes, it rejects with the error. If both fail, it rejects
|
|
1718
|
+
* with the delete error. In this case, the old user's data may still be in the persistence,
|
|
1719
|
+
* so you should check if logOut rejected, and if so, tell the user it was not possible to log
|
|
1720
|
+
* out correctly. For example:
|
|
1721
|
+
*
|
|
1722
|
+
* ```ts
|
|
1723
|
+
* try {
|
|
1724
|
+
* await store.logOut({ initialState: AppState.initialState() });
|
|
1725
|
+
* goToLoginScreen();
|
|
1726
|
+
* } catch (error) {
|
|
1727
|
+
* goToLogOutFailedScreen(error);
|
|
1728
|
+
* }
|
|
1729
|
+
* ```
|
|
1730
|
+
*
|
|
1731
|
+
* Note the save errors are processed by `Persistor.wrapError` first, and if it returns `null`,
|
|
1732
|
+
* the save doesn't count as failed. The errors the persistor reports with `Persistor.addError`
|
|
1733
|
+
* are given to the `errorObserver` as usual, and don't make logOut reject.
|
|
1734
|
+
*
|
|
1735
|
+
* Throws a `StoreException` if called while the persisted state is still being read at
|
|
1736
|
+
* startup (before `store.ready()` resolves). In this case, nothing is changed.
|
|
1737
|
+
*/
|
|
1738
|
+
logOut({ initialState, throttle, actionsThrottle, disposeProps, }: {
|
|
1739
|
+
/** @deprecated Ignored. The store is always `this`. */
|
|
1740
|
+
store?: Store<St>;
|
|
1741
|
+
initialState: St;
|
|
1742
|
+
throttle?: number;
|
|
1743
|
+
actionsThrottle?: number;
|
|
1744
|
+
/**
|
|
1745
|
+
* Whether to dispose the store props (see `setProp`). By default, they are kept, since
|
|
1746
|
+
* some of them belong to the app, and not to the user (a connectivity listener, for
|
|
1747
|
+
* example). Pass `true` to dispose all of them, like `disposeProps()`, or a predicate to
|
|
1748
|
+
* dispose only the ones it selects, like `disposeProps(predicate)`. They are disposed
|
|
1749
|
+
* right away, when logOut starts, so that the previous user's timers and listeners stop,
|
|
1750
|
+
* and logOut waits for the async disposals. A disposal, or a predicate, that throws
|
|
1751
|
+
* doesn't stop the logOut: the error is given to the `errorObserver` (or logged).
|
|
1752
|
+
*/
|
|
1753
|
+
disposeProps?: boolean | PropPredicate;
|
|
1754
|
+
}): Promise<void>;
|
|
1755
|
+
/**
|
|
1756
|
+
* Pause the Persistor temporarily.
|
|
1757
|
+
*
|
|
1758
|
+
* When pausePersistor is called, the Persistor will not start a new persistence process, until method
|
|
1759
|
+
* resumePersistor is called. This will not affect the current persistence process, if one is currently
|
|
1760
|
+
* running.
|
|
1761
|
+
*
|
|
1762
|
+
* Note: A persistence process starts when the Persistor.persistDifference method is called,
|
|
1763
|
+
* and finishes when the promise returned by that method completes.
|
|
1764
|
+
*/
|
|
1765
|
+
pausePersistor(): void;
|
|
1766
|
+
/**
|
|
1767
|
+
* Persists the current state (if it's not yet persisted), then pauses the Persistor temporarily.
|
|
1768
|
+
*
|
|
1769
|
+
* When persistAndPausePersistor is called while the persisted state is still being read,
|
|
1770
|
+
* while a `logOut` is running, or while a persistence process is running, it waits for them
|
|
1771
|
+
* to finish. Note logOut persists its initial-state, and the state of the previous user is
|
|
1772
|
+
* never persisted while logOut is running. Then, if the current
|
|
1773
|
+
* state is not yet persisted, it immediately starts a new persistence process (ignoring
|
|
1774
|
+
* Persistor.throttle). The returned promise completes when the current state is persisted.
|
|
1775
|
+
*
|
|
1776
|
+
* If the save fails, the returned promise still resolves. It never rejects. This is on
|
|
1777
|
+
* purpose, not a bug: this method is usually called when the app is shutting down or going to
|
|
1778
|
+
* the background, and then the app can't recover from a failed save anyway. The error is not
|
|
1779
|
+
* lost: it's given to the `errorObserver` (with a `null` action) before the promise
|
|
1780
|
+
* resolves, so it can be logged.
|
|
1781
|
+
*
|
|
1782
|
+
* Then, the Persistor will not start another persistence process, until method
|
|
1783
|
+
* resumePersistor is called.
|
|
1784
|
+
*
|
|
1785
|
+
* Note: A persistence process starts when the Persistor.persistDifference method is called,
|
|
1786
|
+
* and finishes when the promise returned by that method completes.
|
|
1787
|
+
*/
|
|
1788
|
+
persistAndPausePersistor(): Promise<void>;
|
|
1789
|
+
/**
|
|
1790
|
+
* Resumes persistence by the Persistor,
|
|
1791
|
+
* after calling pausePersistor or persistAndPausePersistor.
|
|
1792
|
+
*/
|
|
1793
|
+
resumePersistor(): void;
|
|
1794
|
+
/**
|
|
1795
|
+
* Asks the Persistor to save the initialState in the local persistence.
|
|
1796
|
+
* If the save fails, this rejects, and nothing is considered persisted
|
|
1797
|
+
* (`getLastPersistedStateFromPersistor` returns `null`).
|
|
1798
|
+
*
|
|
1799
|
+
* If a save is running, it waits for it to finish first, so that it can't overwrite the
|
|
1800
|
+
* initialState later. State changes made meanwhile are saved after it finishes.
|
|
1801
|
+
*/
|
|
1802
|
+
saveInitialStateInPersistence(initialState: St): Promise<void>;
|
|
1803
|
+
/**
|
|
1804
|
+
* Asks the Persistor to read the state from the local persistence.
|
|
1805
|
+
* Important: If you use this, you MUST put this state into the store.
|
|
1806
|
+
* Kiss will assume that's the case, and will not work properly otherwise.
|
|
1807
|
+
*
|
|
1808
|
+
* If a save is running, it waits for it to finish first, so that it reads the saved state.
|
|
1809
|
+
* State changes made meanwhile are saved after the state is read.
|
|
1810
|
+
*/
|
|
1811
|
+
readStateFromPersistence(): Promise<St | null>;
|
|
1812
|
+
/**
|
|
1813
|
+
* Asks the Persistor to delete the saved state from the local persistence.
|
|
1814
|
+
*
|
|
1815
|
+
* If a save is running, it waits for it to finish first, so that it can't put the deleted
|
|
1816
|
+
* state back. State changes made meanwhile are saved after the state is deleted.
|
|
1817
|
+
*/
|
|
1818
|
+
deleteStateFromPersistence(): Promise<void>;
|
|
1819
|
+
/**
|
|
1820
|
+
* Gets, from the Persistor, the last state that was saved to the local persistence.
|
|
1821
|
+
*/
|
|
1822
|
+
getLastPersistedStateFromPersistor(): St | null;
|
|
1823
|
+
/**
|
|
1824
|
+
* Returns a promise which will resolve when the given state `condition` is true.
|
|
1825
|
+
* If the condition is already true when the method is called, the promise resolves immediately.
|
|
1826
|
+
*
|
|
1827
|
+
* Timeout: You must always give a `timeoutMillis`, since there is no default. If the condition
|
|
1828
|
+
* is not met in `timeoutMillis` milliseconds, the promise rejects with a `TimeoutException`,
|
|
1829
|
+
* and the condition stops being checked. To wait with NO timeout, pass `{ timeoutMillis: 0 }`
|
|
1830
|
+
* (or -1, or `Infinity`):
|
|
1831
|
+
*
|
|
1832
|
+
* ```ts
|
|
1833
|
+
* await store.waitCondition((state) => state.name == "Bill", { timeoutMillis: 1000 }); // 1 second.
|
|
1834
|
+
* await store.waitCondition((state) => state.name == "Bill", { timeoutMillis: 0 }); // No timeout.
|
|
1835
|
+
* ```
|
|
1836
|
+
*
|
|
1837
|
+
* To handle the timeout, you can catch the `TimeoutException`:
|
|
1838
|
+
*
|
|
1839
|
+
* ```ts
|
|
1840
|
+
* try { await store.waitCondition(condition, { timeoutMillis: 5000 }); }
|
|
1841
|
+
* catch (error) { if (error instanceof TimeoutException) { ... } else throw error; }
|
|
1842
|
+
* ```
|
|
1843
|
+
*
|
|
1844
|
+
* Or, you can pass an `onTimeout` callback. If the timeout expires, `onTimeout` is called,
|
|
1845
|
+
* and the promise resolves with `null` instead of rejecting. If `onTimeout` throws, the
|
|
1846
|
+
* promise rejects with that error:
|
|
1847
|
+
*
|
|
1848
|
+
* ```ts
|
|
1849
|
+
* await store.waitCondition(condition, { timeoutMillis: 5000, onTimeout: () => { ... } });
|
|
1850
|
+
* ```
|
|
1851
|
+
*
|
|
1852
|
+
* This method is useful in tests, and it returns the action which changed
|
|
1853
|
+
* the store state into the condition, in case you need it:
|
|
1854
|
+
*
|
|
1855
|
+
* ```typescript
|
|
1856
|
+
* let action = await store.waitCondition((state) => state.name == "Bill", { timeoutMillis: 1000 });
|
|
1857
|
+
* expect(action instanceof ChangeNameAction).toBe(true);
|
|
1858
|
+
* ```
|
|
1859
|
+
*
|
|
1860
|
+
* This method is also eventually useful in production code, but note that while it waits,
|
|
1861
|
+
* the condition runs on EVERY state change. A few short-lived waits are fine. But many waits,
|
|
1862
|
+
* or waits that may never complete (especially with no timeout), add a cost to every state
|
|
1863
|
+
* change, for as long as the store lives, since there is no way to cancel a wait. In
|
|
1864
|
+
* production, it's often better to put the logic in an action, or to react to the selected
|
|
1865
|
+
* state in your components.
|
|
1866
|
+
*
|
|
1867
|
+
* Examples:
|
|
1868
|
+
*
|
|
1869
|
+
* ```ts
|
|
1870
|
+
* // Dispatch actions and wait for the state change:
|
|
1871
|
+
* expect(store.state.user.isLoggedIn).toBe(false);
|
|
1872
|
+
* dispatch(new LogInUser("Mary"));
|
|
1873
|
+
* await store.waitCondition((state) => state.user.isLoggedIn, { timeoutMillis: 1000 });
|
|
1874
|
+
* expect(store.state.user.name).toBe("Mary");
|
|
1875
|
+
* ```
|
|
1876
|
+
*
|
|
1877
|
+
* If the condition throws, the returned promise rejects with that error, and the wait ends.
|
|
1878
|
+
* The action whose state change ran the condition is not affected: it still completes
|
|
1879
|
+
* normally, and `dispatch` doesn't throw.
|
|
1880
|
+
*
|
|
1881
|
+
* See also:
|
|
1882
|
+
* `waitActionCondition` - Waits until the actions in progress meet a given condition.
|
|
1883
|
+
* `waitAllActions` - Waits until the given actions are NOT in progress, or no actions are in progress.
|
|
1884
|
+
* `waitActionType` - Waits until an action of a given type is NOT in progress.
|
|
1885
|
+
* `waitAllActionTypes` - Waits until all actions of the given type are NOT in progress.
|
|
1886
|
+
* `waitAnyActionTypeFinishes` - Waits until ANY action of the given types finish dispatching.
|
|
1887
|
+
*/
|
|
1888
|
+
waitCondition(condition: (state: St) => boolean, { timeoutMillis, onTimeout, }: {
|
|
1889
|
+
timeoutMillis: number;
|
|
1890
|
+
onTimeout?: () => void;
|
|
1891
|
+
}): Promise<KissAction<St> | null>;
|
|
1892
|
+
/**
|
|
1893
|
+
* Returns a Promise that resolves when some actions meet the given `condition`.
|
|
1894
|
+
*
|
|
1895
|
+
* If `completeImmediately` is false (the default), this method will throw an error if the
|
|
1896
|
+
* condition was already true when the method was called. Otherwise, the promise will complete
|
|
1897
|
+
* immediately and throw no error.
|
|
1898
|
+
*
|
|
1899
|
+
* The `condition` is a function that takes the set of actions "in progress", as well as an
|
|
1900
|
+
* action that just entered the set (by being dispatched) or left the set (by finishing
|
|
1901
|
+
* dispatching). The function should return `true` when the condition is met, and `false`
|
|
1902
|
+
* otherwise. For example:
|
|
1903
|
+
*
|
|
1904
|
+
* ```ts
|
|
1905
|
+
* let action = await store.waitActionCondition((actionsInProgress, triggerAction) => { ... });
|
|
1906
|
+
* ```
|
|
1907
|
+
*
|
|
1908
|
+
* Note: Each time the condition is checked, it gets a new copy of the actions in progress.
|
|
1909
|
+
* Changing that set doesn't affect the store, and the set doesn't change when other actions
|
|
1910
|
+
* are later dispatched or finish.
|
|
1911
|
+
*
|
|
1912
|
+
* You get back the set of the actions being dispatched that met the condition, as well as
|
|
1913
|
+
* the action that triggered the condition by being added or removed from the set.
|
|
1914
|
+
* The returned set is a copy, taken when the condition was met (or when it timed out),
|
|
1915
|
+
* so it doesn't change when other actions are later dispatched or finish. Changing it
|
|
1916
|
+
* doesn't affect the store.
|
|
1917
|
+
*
|
|
1918
|
+
* Note: The condition is only checked when some action is dispatched or finishes dispatching.
|
|
1919
|
+
* It's not checked every time action statuses change.
|
|
1920
|
+
*
|
|
1921
|
+
* Timeout: If the condition is not met in `timeoutMillis` milliseconds, the promise rejects
|
|
1922
|
+
* with a `TimeoutException`, and the condition stops being checked. The default is 3 seconds,
|
|
1923
|
+
* which is shorter than the default test timeout of Jest and Vitest (5 seconds). You can change
|
|
1924
|
+
* it globally with `TimeoutException.defaultTimeoutMillis`.
|
|
1925
|
+
* To wait with NO timeout, pass `{ timeoutMillis: 0 }` (or -1, or `Infinity`).
|
|
1926
|
+
* To handle the timeout without an error, pass an `onTimeout` callback. If the timeout
|
|
1927
|
+
* expires, `onTimeout` is called, and the promise resolves with no trigger action (`null`),
|
|
1928
|
+
* instead of rejecting. If `onTimeout` throws, the promise rejects with that error.
|
|
1929
|
+
*
|
|
1930
|
+
* If the condition throws, the promise rejects with that error, and the wait ends.
|
|
1931
|
+
* The action that triggered the check is not affected.
|
|
1932
|
+
*
|
|
1933
|
+
* See also:
|
|
1934
|
+
* `waitCondition` - Waits until the state is in a given condition.
|
|
1935
|
+
* `waitAllActions` - Waits until the given actions are NOT in progress, or no actions are in progress.
|
|
1936
|
+
* `waitActionType` - Waits until an action of a given type is NOT in progress.
|
|
1937
|
+
* `waitAllActionTypes` - Waits until all actions of the given type are NOT in progress.
|
|
1938
|
+
* `waitAnyActionTypeFinishes` - Waits until ANY action of the given types finish dispatching.
|
|
1939
|
+
*
|
|
1940
|
+
* You should only use this method in tests.
|
|
1941
|
+
*/
|
|
1942
|
+
waitActionCondition(condition: (actions: Set<KissAction<St>>, triggerAction: KissAction<St> | null) => boolean, { completeImmediately, completedErrorMessage, timeoutMillis, onTimeout, }?: {
|
|
1943
|
+
completeImmediately?: boolean;
|
|
1944
|
+
completedErrorMessage?: string;
|
|
1945
|
+
timeoutMillis?: number | null;
|
|
1946
|
+
onTimeout?: () => void;
|
|
1947
|
+
}): Promise<{
|
|
1948
|
+
actions: Set<KissAction<St>>;
|
|
1949
|
+
triggerAction: KissAction<St> | null;
|
|
1950
|
+
}>;
|
|
1951
|
+
private _waitActionCondition;
|
|
1952
|
+
/**
|
|
1953
|
+
* Returns a promise that resolves when ALL given actions finished dispatching.
|
|
1954
|
+
*
|
|
1955
|
+
* If `completeImmediately` is false (the default), this method will throw an error if none
|
|
1956
|
+
* of the given actions are in progress when the method is called. Otherwise, the promise will
|
|
1957
|
+
* complete immediately and throw no error.
|
|
1958
|
+
*
|
|
1959
|
+
* However, if you don't provide any actions (empty list or `null`), the promise will complete
|
|
1960
|
+
* when ALL current actions in progress finish dispatching. In other words, when no actions are
|
|
1961
|
+
* currently in progress. In this case, if [completeImmediately] is `false`, the method will
|
|
1962
|
+
* throw an error if no actions are in progress when the method is called.
|
|
1963
|
+
*
|
|
1964
|
+
* Note: Waiting until no actions are in progress should only be done in test, never in
|
|
1965
|
+
* production, as it's very easy to create a deadlock. However, waiting for specific actions to
|
|
1966
|
+
* finish is safe in production, as long as you're waiting for actions you just dispatched.
|
|
1967
|
+
*
|
|
1968
|
+
* Timeout: If the condition is not met in `timeoutMillis` milliseconds, the promise rejects
|
|
1969
|
+
* with a `TimeoutException`, and the condition stops being checked. The default is 3 seconds,
|
|
1970
|
+
* which is shorter than the default test timeout of Jest and Vitest (5 seconds). You can change
|
|
1971
|
+
* it globally with `TimeoutException.defaultTimeoutMillis`.
|
|
1972
|
+
* To wait with NO timeout, pass `{ timeoutMillis: 0 }` (or -1, or `Infinity`).
|
|
1973
|
+
* To handle the timeout without an error, pass an `onTimeout` callback. If the timeout
|
|
1974
|
+
* expires, `onTimeout` is called, and the promise resolves with no trigger action (`null`),
|
|
1975
|
+
* instead of rejecting. If `onTimeout` throws, the promise rejects with that error.
|
|
1976
|
+
*
|
|
1977
|
+
* Examples:
|
|
1978
|
+
*
|
|
1979
|
+
* ```ts
|
|
1980
|
+
* // Dispatch actions in PARALLEL and wait until no actions are in progress.
|
|
1981
|
+
* expect(state.stocks).toEqual(['TSLA']);
|
|
1982
|
+
* dispatch(new BuyAction('IBM'));
|
|
1983
|
+
* dispatch(new SellAction('TSLA'));
|
|
1984
|
+
* await store.waitAllActions([]);
|
|
1985
|
+
* expect(state.stocks).toEqual(['IBM']);
|
|
1986
|
+
*
|
|
1987
|
+
* // Dispatch two actions in PARALLEL and wait for them to finish:
|
|
1988
|
+
* expect(state.stocks).toEqual(['TSLA']);
|
|
1989
|
+
* let action1 = new BuyAction('IBM');
|
|
1990
|
+
* let action2 = new SellAction('TSLA');
|
|
1991
|
+
* dispatch(action1);
|
|
1992
|
+
* dispatch(action2);
|
|
1993
|
+
* await store.waitAllActions([action1, action2]);
|
|
1994
|
+
* expect(state.stocks).toEqual(['IBM']);
|
|
1995
|
+
*
|
|
1996
|
+
* // This could also have been achieved by dispatching two actions in PARALLEL
|
|
1997
|
+
* // by using with `dispatchAndWaitAll` to wait for them:
|
|
1998
|
+
* expect(state.stocks).toEqual(['TSLA']);
|
|
1999
|
+
* await dispatchAndWaitAll([
|
|
2000
|
+
* new BuyAction('IBM'),
|
|
2001
|
+
* new SellAction('TSLA')
|
|
2002
|
+
* ]);
|
|
2003
|
+
* expect(state.stocks).toEqual(['IBM']);
|
|
2004
|
+
*
|
|
2005
|
+
* // This could also have been achieved by dispatching two actions in SERIES
|
|
2006
|
+
* // by using `dispatchAndWait` to wait for them:
|
|
2007
|
+
* expect(state.stocks).toEqual(['TSLA']);
|
|
2008
|
+
* await dispatchAndWait(new BuyAction('IBM'));
|
|
2009
|
+
* await dispatchAndWait(new SellAction('TSLA'));
|
|
2010
|
+
* expect(state.stocks).toEqual(['IBM']);
|
|
2011
|
+
* ```
|
|
2012
|
+
*
|
|
2013
|
+
* See also:
|
|
2014
|
+
* `waitCondition` - Waits until the state is in a given condition.
|
|
2015
|
+
* `waitActionCondition` - Waits until the actions in progress meet a given condition.
|
|
2016
|
+
* `waitActionType` - Waits until an action of a given type is NOT in progress.
|
|
2017
|
+
* `waitAllActionTypes` - Waits until all actions of the given type are NOT in progress.
|
|
2018
|
+
* `waitAnyActionTypeFinishes` - Waits until ANY action of the given types finish dispatching.
|
|
2019
|
+
*
|
|
2020
|
+
* You should only use this method in tests.
|
|
2021
|
+
*/
|
|
2022
|
+
waitAllActions(actions: KissAction<St>[] | null, { completeImmediately, timeoutMillis, onTimeout, }?: {
|
|
2023
|
+
completeImmediately?: boolean;
|
|
2024
|
+
timeoutMillis?: number | null;
|
|
2025
|
+
onTimeout?: () => void;
|
|
2026
|
+
}): Promise<{
|
|
2027
|
+
actions: Set<KissAction<St>>;
|
|
2028
|
+
triggerAction: KissAction<St> | null;
|
|
2029
|
+
}>;
|
|
2030
|
+
/**
|
|
2031
|
+
* Returns a promise that completes when an action of the given type in NOT in progress
|
|
2032
|
+
* (it's not being dispatched):
|
|
2033
|
+
*
|
|
2034
|
+
* - If NO action of the given type is currently in progress when the method is called,
|
|
2035
|
+
* and `completeImmediately` is false (the default), this method will throw an error.
|
|
2036
|
+
*
|
|
2037
|
+
* - If NO action of the given type is currently in progress when the method is called,
|
|
2038
|
+
* and `completeImmediately` is true, the promise completes immediately, returns `null`,
|
|
2039
|
+
* and throws no error.
|
|
2040
|
+
*
|
|
2041
|
+
* - If an action of the given type is in progress, the promise completes when the action
|
|
2042
|
+
* finishes, and returns the action. You can use the returned action to check its `status`:
|
|
2043
|
+
*
|
|
2044
|
+
* ```ts
|
|
2045
|
+
* let action = await store.waitActionType(MyAction);
|
|
2046
|
+
* expect(action?.status.originalError).toBeInstanceOf(UserException);
|
|
2047
|
+
* ```
|
|
2048
|
+
*
|
|
2049
|
+
* Timeout: If the condition is not met in `timeoutMillis` milliseconds, the promise rejects
|
|
2050
|
+
* with a `TimeoutException`, and the condition stops being checked. The default is 3 seconds,
|
|
2051
|
+
* which is shorter than the default test timeout of Jest and Vitest (5 seconds). You can change
|
|
2052
|
+
* it globally with `TimeoutException.defaultTimeoutMillis`.
|
|
2053
|
+
* To wait with NO timeout, pass `{ timeoutMillis: 0 }` (or -1, or `Infinity`).
|
|
2054
|
+
* To handle the timeout without an error, pass an `onTimeout` callback. If the timeout
|
|
2055
|
+
* expires, `onTimeout` is called, and the promise resolves with no trigger action (`null`),
|
|
2056
|
+
* instead of rejecting. If `onTimeout` throws, the promise rejects with that error.
|
|
2057
|
+
*
|
|
2058
|
+
* Examples:
|
|
2059
|
+
*
|
|
2060
|
+
* ```ts
|
|
2061
|
+
* // Dispatches an action and waits until no action of its type is in progress.
|
|
2062
|
+
* dispatch(new ChangeNameAction('Bill'));
|
|
2063
|
+
* let action = await store.waitActionType(ChangeNameAction);
|
|
2064
|
+
* expect(action instanceof ChangeNameAction).toBe(true);
|
|
2065
|
+
* expect(action?.status.isCompletedOk).toBe(true);
|
|
2066
|
+
* expect(store.state.name).toBe('Bill');
|
|
2067
|
+
* ```
|
|
2068
|
+
*
|
|
2069
|
+
* See also:
|
|
2070
|
+
* `waitCondition` - Waits until the state is in a given condition.
|
|
2071
|
+
* `waitActionCondition` - Waits until the actions in progress meet a given condition.
|
|
2072
|
+
* `waitAllActions` - Waits until the given actions are NOT in progress, or no actions are in progress.
|
|
2073
|
+
* `waitActionType` - Waits until an action of a given type is NOT in progress.
|
|
2074
|
+
* `waitAllActionTypes` - Waits until all actions of the given type are NOT in progress.
|
|
2075
|
+
* `waitAnyActionTypeFinishes` - Waits until ANY action of the given types finish dispatching.
|
|
2076
|
+
*
|
|
2077
|
+
* You should only use this method in tests.
|
|
2078
|
+
*/
|
|
2079
|
+
waitActionType(actionType: {
|
|
2080
|
+
new (...args: any[]): KissAction<St>;
|
|
2081
|
+
}, { completeImmediately, timeoutMillis, onTimeout, }?: {
|
|
2082
|
+
completeImmediately?: boolean;
|
|
2083
|
+
timeoutMillis?: number | null;
|
|
2084
|
+
onTimeout?: () => void;
|
|
2085
|
+
}): Promise<KissAction<St> | null>;
|
|
2086
|
+
/**
|
|
2087
|
+
* Returns a promise that completes when ALL actions of the given type are NOT in progress
|
|
2088
|
+
* (none of them is being dispatched):
|
|
2089
|
+
*
|
|
2090
|
+
* - If NO action of the given types is currently in progress when the method is called,
|
|
2091
|
+
* and `completeImmediately` is false (the default), this method will throw an error.
|
|
2092
|
+
*
|
|
2093
|
+
* - If NO action of the given type is currently in progress when the method is called,
|
|
2094
|
+
* and `completeImmediately` is true, the promise completes immediately and throws no error.
|
|
2095
|
+
*
|
|
2096
|
+
* - If any action of the given types is in progress, the promise completes only when
|
|
2097
|
+
* no action of the given types is in progress anymore.
|
|
2098
|
+
*
|
|
2099
|
+
* Timeout: If the condition is not met in `timeoutMillis` milliseconds, the promise rejects
|
|
2100
|
+
* with a `TimeoutException`, and the condition stops being checked. The default is 3 seconds,
|
|
2101
|
+
* which is shorter than the default test timeout of Jest and Vitest (5 seconds). You can change
|
|
2102
|
+
* it globally with `TimeoutException.defaultTimeoutMillis`.
|
|
2103
|
+
* To wait with NO timeout, pass `{ timeoutMillis: 0 }` (or -1, or `Infinity`).
|
|
2104
|
+
* To handle the timeout without an error, pass an `onTimeout` callback. If the timeout
|
|
2105
|
+
* expires, `onTimeout` is called, and the promise resolves with no trigger action (`null`),
|
|
2106
|
+
* instead of rejecting. If `onTimeout` throws, the promise rejects with that error.
|
|
2107
|
+
*
|
|
2108
|
+
* Examples:
|
|
2109
|
+
*
|
|
2110
|
+
* ```ts
|
|
2111
|
+
* // Dispatches two actions in PARALLEL and wait for their TYPES:
|
|
2112
|
+
* expect(store.state.portfolio).toEqual(['TSLA']);
|
|
2113
|
+
* dispatch(new BuyAction('IBM'));
|
|
2114
|
+
* dispatch(new SellAction('TSLA'));
|
|
2115
|
+
* await store.waitAllActionTypes([BuyAction, SellAction]);
|
|
2116
|
+
* expect(store.state.portfolio).toEqual(['IBM']);
|
|
2117
|
+
* ```
|
|
2118
|
+
*
|
|
2119
|
+
* See also:
|
|
2120
|
+
* `waitCondition` - Waits until the state is in a given condition.
|
|
2121
|
+
* `waitActionCondition` - Waits until the actions in progress meet a given condition.
|
|
2122
|
+
* `waitAllActions` - Waits until the given actions are NOT in progress, or no actions are in progress.
|
|
2123
|
+
* `waitActionType` - Waits until an action of a given type is NOT in progress.
|
|
2124
|
+
* `waitAllActionTypes` - Waits until all actions of the given type are NOT in progress.
|
|
2125
|
+
* `waitAnyActionTypeFinishes` - Waits until ANY action of the given types finish dispatching.
|
|
2126
|
+
*
|
|
2127
|
+
* You should only use this method in tests.
|
|
2128
|
+
*/
|
|
2129
|
+
waitAllActionTypes(actionTypes: {
|
|
2130
|
+
new (...args: any[]): KissAction<St>;
|
|
2131
|
+
}[], { completeImmediately, timeoutMillis, onTimeout, }?: {
|
|
2132
|
+
completeImmediately?: boolean;
|
|
2133
|
+
timeoutMillis?: number | null;
|
|
2134
|
+
onTimeout?: () => void;
|
|
2135
|
+
}): Promise<void>;
|
|
2136
|
+
/**
|
|
2137
|
+
* Returns a promise which will complete when ANY action of the given types FINISHES
|
|
2138
|
+
* dispatching. IMPORTANT: This method is different from the other similar methods, because
|
|
2139
|
+
* it does NOT complete immediately if no action of the given types is in progress. Instead,
|
|
2140
|
+
* it waits until an action of the given types finishes dispatching, even if they
|
|
2141
|
+
* were not yet in progress when the method was called.
|
|
2142
|
+
*
|
|
2143
|
+
* This method returns the action that completed the promise, which you can use to check
|
|
2144
|
+
* its `status`.
|
|
2145
|
+
*
|
|
2146
|
+
* It's useful when the actions you are waiting for are not yet dispatched when you call this
|
|
2147
|
+
* method. For example, suppose action `StartAction` starts a process that takes some time
|
|
2148
|
+
* to run and then dispatches an action called `MyFinalAction`. You can then write:
|
|
2149
|
+
*
|
|
2150
|
+
* ```ts
|
|
2151
|
+
* dispatch(new StartAction());
|
|
2152
|
+
* let action = await store.waitAnyActionTypeFinishes([MyFinalAction]);
|
|
2153
|
+
* expect(action.status.originalError).toBeInstanceOf(UserException);
|
|
2154
|
+
* ```
|
|
2155
|
+
*
|
|
2156
|
+
* Timeout: If the condition is not met in `timeoutMillis` milliseconds, the promise rejects
|
|
2157
|
+
* with a `TimeoutException`, and the condition stops being checked. The default is 3 seconds,
|
|
2158
|
+
* which is shorter than the default test timeout of Jest and Vitest (5 seconds). You can change
|
|
2159
|
+
* it globally with `TimeoutException.defaultTimeoutMillis`.
|
|
2160
|
+
* To wait with NO timeout, pass `{ timeoutMillis: 0 }` (or -1, or `Infinity`).
|
|
2161
|
+
* To handle the timeout without an error, pass an `onTimeout` callback. If the timeout
|
|
2162
|
+
* expires, `onTimeout` is called, and the promise resolves with no trigger action (`null`),
|
|
2163
|
+
* instead of rejecting. If `onTimeout` throws, the promise rejects with that error.
|
|
2164
|
+
*
|
|
2165
|
+
* Examples:
|
|
2166
|
+
*
|
|
2167
|
+
* ```ts
|
|
2168
|
+
* // Wait until some action of the given types is dispatched.
|
|
2169
|
+
* dispatch(new ProcessStocksAction());
|
|
2170
|
+
* let action = await store.waitAnyActionTypeFinishes([BuyAction, SellAction]);
|
|
2171
|
+
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
2172
|
+
* ```
|
|
2173
|
+
*
|
|
2174
|
+
* See also:
|
|
2175
|
+
* `waitCondition` - Waits until the state is in a given condition.
|
|
2176
|
+
* `waitActionCondition` - Waits until the actions in progress meet a given condition.
|
|
2177
|
+
* `waitAllActions` - Waits until the given actions are NOT in progress, or no actions are in progress.
|
|
2178
|
+
* `waitActionType` - Waits until an action of a given type is NOT in progress.
|
|
2179
|
+
* `waitAllActionTypes` - Waits until all actions of the given type are NOT in progress.
|
|
2180
|
+
* `waitAnyActionTypeFinishes` - Waits until ANY action of the given types finish dispatching.
|
|
2181
|
+
*
|
|
2182
|
+
* You should only use this method in tests.
|
|
2183
|
+
*/
|
|
2184
|
+
waitAnyActionTypeFinishes(actionTypes: {
|
|
2185
|
+
new (...args: any[]): KissAction<St>;
|
|
2186
|
+
}[], options?: {
|
|
2187
|
+
timeoutMillis?: number | null;
|
|
2188
|
+
onTimeout?: undefined;
|
|
2189
|
+
}): Promise<KissAction<St>>;
|
|
2190
|
+
waitAnyActionTypeFinishes(actionTypes: {
|
|
2191
|
+
new (...args: any[]): KissAction<St>;
|
|
2192
|
+
}[], options: {
|
|
2193
|
+
timeoutMillis?: number | null;
|
|
2194
|
+
onTimeout?: () => void;
|
|
2195
|
+
}): Promise<KissAction<St> | null>;
|
|
2196
|
+
/**
|
|
2197
|
+
* Returns a copy of the set of the actions in progress.
|
|
2198
|
+
*
|
|
2199
|
+
* Each call returns a new copy. It doesn't change when actions are later dispatched or
|
|
2200
|
+
* finish, and changing it doesn't affect the store.
|
|
2201
|
+
*
|
|
2202
|
+
* For debug purposes, the `toString` method of the returned set will
|
|
2203
|
+
* call `KissAction.toString()` for all actions.
|
|
2204
|
+
*/
|
|
2205
|
+
actionsInProgress(): Set<KissAction<St>>;
|
|
2206
|
+
/**
|
|
2207
|
+
* Waits until the store state meets a certain condition, and then dispatches an action.
|
|
2208
|
+
* If the condition is already true, the action is dispatched right away, before `dispatchWhen`
|
|
2209
|
+
* returns. Otherwise, it is dispatched synchronously, right after the state change that meets
|
|
2210
|
+
* the condition, so no other action can change the state in between.
|
|
2211
|
+
* If the same state change meets the condition of several `dispatchWhen`, their actions are
|
|
2212
|
+
* dispatched one after the other, and each condition is checked again right before its
|
|
2213
|
+
* action is dispatched. If an earlier action made it false, it keeps waiting.
|
|
2214
|
+
*
|
|
2215
|
+
* ```ts
|
|
2216
|
+
* store.dispatchWhen(new BuyStock('IBM'), (state) => state.stocks.getPrice('IBM') >= 100, { timeoutMillis: 0 });
|
|
2217
|
+
* ```
|
|
2218
|
+
*
|
|
2219
|
+
* Timeout: You must always give a `timeoutMillis`, since there is no default. If the condition
|
|
2220
|
+
* is not met in `timeoutMillis` milliseconds, the action is NOT dispatched, and the condition
|
|
2221
|
+
* stops being checked. In this case, `onTimeout` is called if you provided it. Otherwise, the
|
|
2222
|
+
* timeout is logged with `Store.log()`. To wait with NO timeout, pass `{ timeoutMillis: 0 }`
|
|
2223
|
+
* (or -1, or `Infinity`), but note that while it waits, the condition runs on every state change.
|
|
2224
|
+
* A `logOut()` cancels the `dispatchWhen`s that are still waiting: their action is not
|
|
2225
|
+
* dispatched, and their `onTimeout` is not called.
|
|
2226
|
+
*
|
|
2227
|
+
* ```ts
|
|
2228
|
+
* store.dispatchWhen(
|
|
2229
|
+
* new BuyStock('IBM'),
|
|
2230
|
+
* (state) => state.stocks.getPrice('IBM') >= 100,
|
|
2231
|
+
* { timeoutMillis: 60 * 1000, onTimeout: () => console.log('IBM never reached 100.') },
|
|
2232
|
+
* );
|
|
2233
|
+
* ```
|
|
2234
|
+
*
|
|
2235
|
+
* See also:
|
|
2236
|
+
* - `this.dispatchWhen` inside actions.
|
|
2237
|
+
* - `useDispatchWhen` (or `useStore().dispatchWhen`) in components.
|
|
2238
|
+
*/
|
|
2239
|
+
dispatchWhen(action: KissAction<St>, condition: (state: St) => boolean, { timeoutMillis, onTimeout, }: {
|
|
2240
|
+
timeoutMillis: number;
|
|
2241
|
+
onTimeout?: () => void;
|
|
2242
|
+
}): void;
|
|
2243
|
+
private _dispatchWhenAction;
|
|
2244
|
+
}
|
|
2245
|
+
declare class Mocks<St> {
|
|
2246
|
+
private mocks;
|
|
2247
|
+
add<T extends KissAction<St>>(actionType: new (...args: any[]) => T, mockFunction: ((action: T) => KissAction<St> | null) | null): void;
|
|
2248
|
+
remove(actionType: new (...args: any[]) => KissAction<St>): void;
|
|
2249
|
+
clear(): void;
|
|
2250
|
+
get<T extends KissAction<St>>(actionType: new (...args: any[]) => T): ((action: T) => KissAction<St> | null) | undefined;
|
|
2251
|
+
}
|
|
2252
|
+
export type ShowUserException = (exception: UserException, count: number, next: () => void) => void;
|
|
2253
|
+
export {};
|
|
1043
2254
|
//# sourceMappingURL=Store.d.ts.map
|