kiss-for-react 1.1.0 → 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 +54 -30
- 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/esm/Hooks.js
DELETED
|
@@ -1,955 +0,0 @@
|
|
|
1
|
-
import { useContext, useEffect, useRef, useState } from 'react';
|
|
2
|
-
import { StoreException } from './StoreException';
|
|
3
|
-
import { StoreContext } from "./Store";
|
|
4
|
-
/**
|
|
5
|
-
* Returns a part of the store state:
|
|
6
|
-
*
|
|
7
|
-
* ```ts
|
|
8
|
-
* const name = useSelect((state: State) => state.user.name);
|
|
9
|
-
* ```
|
|
10
|
-
*
|
|
11
|
-
* The component will rebuild only when the `name` changes, ignoring the
|
|
12
|
-
* change in other parts of the state.
|
|
13
|
-
*
|
|
14
|
-
* Note: You can use `useSelect` and `useSelector` interchangeably.
|
|
15
|
-
* Prefer `useSelect` because it's shorter.
|
|
16
|
-
*/
|
|
17
|
-
export function useSelect(selector) {
|
|
18
|
-
// This ref will persist for the full lifetime of the component.
|
|
19
|
-
let ref = useRef(undefined);
|
|
20
|
-
// The initial value is the value selected from the current state in the store.
|
|
21
|
-
let store = _useStoreFromContext();
|
|
22
|
-
const [value, setValue] = useState(() => selector(store.state));
|
|
23
|
-
// Only once when the component mounts.
|
|
24
|
-
useEffect(() => {
|
|
25
|
-
// We save in the ref:
|
|
26
|
-
// - The selector
|
|
27
|
-
// - The currently selected value
|
|
28
|
-
// - The setValue function
|
|
29
|
-
//
|
|
30
|
-
// Whenever the state changes, the store will:
|
|
31
|
-
// 1. Retrieve all refs
|
|
32
|
-
// 2. Apply each selector to the current state to calculate the selected value.
|
|
33
|
-
// 3. And compare the selected value with the last selected value.
|
|
34
|
-
// 4. If it changed, it calls setValue.
|
|
35
|
-
ref.current = new RefState([selector, value, setValue]);
|
|
36
|
-
// Save the new ref.
|
|
37
|
-
store._refStateHooks.add(ref);
|
|
38
|
-
return () => {
|
|
39
|
-
// When the component unmounts, delete the ref.
|
|
40
|
-
store._refStateHooks.delete(ref);
|
|
41
|
-
};
|
|
42
|
-
}, []);
|
|
43
|
-
return value;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Returns a part of the store state:
|
|
47
|
-
*
|
|
48
|
-
* ```ts
|
|
49
|
-
* const name = useSelector((state: State) => state.user.name);
|
|
50
|
-
* ```
|
|
51
|
-
*
|
|
52
|
-
* The component will rebuild only when the `name` changes, ignoring the
|
|
53
|
-
* change in other parts of the state.
|
|
54
|
-
*
|
|
55
|
-
* Note: You can use `useSelect` and `useSelector` interchangeably.
|
|
56
|
-
* Prefer `useSelect` because it's shorter.
|
|
57
|
-
*/
|
|
58
|
-
export function useSelector(selector) {
|
|
59
|
-
return useSelect(selector);
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Returns the whole store state:
|
|
63
|
-
*
|
|
64
|
-
* ```ts
|
|
65
|
-
* const state = useAllState<State>();
|
|
66
|
-
* const name = state.user.name;
|
|
67
|
-
* ```
|
|
68
|
-
*
|
|
69
|
-
* While that's convenient, the above code will rebuild the component for all state changes,
|
|
70
|
-
* no matter if the name changed or not. For this reason, prefer `useSelector`:
|
|
71
|
-
*
|
|
72
|
-
* ```ts
|
|
73
|
-
* const name = useSelector((state: State) => state.user.name);
|
|
74
|
-
* ```
|
|
75
|
-
*
|
|
76
|
-
* Now it will rebuild only when the name changes.
|
|
77
|
-
*/
|
|
78
|
-
export function useAllState() {
|
|
79
|
-
return useSelect((state) => state);
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* You can get the store to use all dispatch and wait methods:
|
|
83
|
-
*
|
|
84
|
-
* - `dispatch` - Dispatches the action to the Redux store, to potentially change the state.
|
|
85
|
-
* - `dispatchAndWait` - Dispatches an action and returns a promise that resolves when the action finishes.
|
|
86
|
-
* - `dispatchSync` - Same as the regular dispatch, except it throws an error if the action is ASYNC.
|
|
87
|
-
* - `waitCondition` - Waits until the state is in a given condition.
|
|
88
|
-
* - `waitActionCondition` - Waits until the actions in progress meet a given condition.
|
|
89
|
-
* - `waitAllActions` - Waits until the given actions are NOT in progress, or no actions are in progress.
|
|
90
|
-
* - `waitActionType` - Waits until an action of a given type is NOT in progress.
|
|
91
|
-
* - `waitAllActionTypes` - Waits until all actions of the given type are NOT in progress.
|
|
92
|
-
* - `waitAnyActionTypeFinishes` - Waits until ANY action of the given types finish dispatching.
|
|
93
|
-
*
|
|
94
|
-
* Example:
|
|
95
|
-
*
|
|
96
|
-
* ```js
|
|
97
|
-
* const store = useStore();
|
|
98
|
-
* store.dispatch(new MyAction());
|
|
99
|
-
* await store.dispatchAndWait(new MyAction());
|
|
100
|
-
* store.dispatchSync(new MyAction());
|
|
101
|
-
* await waitCondition((state) => state.user.name === "Bill");
|
|
102
|
-
* await waitAllActionTypes(['BuyAction', 'SellAction']);
|
|
103
|
-
* ```
|
|
104
|
-
*
|
|
105
|
-
* IMPORTANT:
|
|
106
|
-
* The store `state` and other store methods are NOT available when you get the store with `useStore`.
|
|
107
|
-
* - To get the state: `useSelect`
|
|
108
|
-
* - To wait for actions: `useIsWaiting`
|
|
109
|
-
* - To deal with exceptions: `useIsFailed`, `useExceptionFor` and `clearExceptionFor`
|
|
110
|
-
*
|
|
111
|
-
* See also:
|
|
112
|
-
* - `useDispatch()` - Hook equivalent to `useStore().dispatch`
|
|
113
|
-
* - `useDispatchAndWait` - Hook equivalent to `useStore().dispatchAndWait`
|
|
114
|
-
* - `useDispatchSync` - Hook equivalent to `useStore().dispatchSync`
|
|
115
|
-
*/
|
|
116
|
-
export function useStore() {
|
|
117
|
-
let store = _useStoreFromContext();
|
|
118
|
-
return new StoreDispatchers(store);
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* @deprecated Use `useDispatch`. Note you also have `useDispatchAndWait` and `useDispatchSync`.
|
|
122
|
-
*/
|
|
123
|
-
export function useDispatcher() {
|
|
124
|
-
return useDispatch();
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Dispatches the action to the Redux store, to potentially change the state:
|
|
128
|
-
*
|
|
129
|
-
* ```ts
|
|
130
|
-
* const dispatch = useDispatch();
|
|
131
|
-
* dispatch(MyAction());
|
|
132
|
-
* ```
|
|
133
|
-
*
|
|
134
|
-
* This also works:
|
|
135
|
-
*
|
|
136
|
-
* ```ts
|
|
137
|
-
* const store = useStore();
|
|
138
|
-
* dispatch(MyAction());
|
|
139
|
-
* ```
|
|
140
|
-
*
|
|
141
|
-
* See also:
|
|
142
|
-
* - `dispatchAll` which dispatches all given actions in parallel.
|
|
143
|
-
* - `dispatchSync` which dispatches sync actions, and throws if the action is async.
|
|
144
|
-
* - `dispatchAndWait` which dispatches both sync and async actions, and returns a Promise.
|
|
145
|
-
* - `dispatchAndWaitAll` which dispatches all given actions, and returns a Promise.
|
|
146
|
-
*/
|
|
147
|
-
export function useDispatch() {
|
|
148
|
-
let store = _useStoreFromContext();
|
|
149
|
-
return store.dispatch.bind(store);
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Dispatches an action and returns a promise that resolves when the action finishes.
|
|
153
|
-
* While the state change from the action's reducer will have been applied when the promise
|
|
154
|
-
* resolves, other independent processes that the action may have started may still be in
|
|
155
|
-
* progress.
|
|
156
|
-
*
|
|
157
|
-
* Usage:
|
|
158
|
-
* ```ts
|
|
159
|
-
* const dispatchAndWait = useDispatchAndWait();
|
|
160
|
-
* await dispatchAndWait(new MyAction())`.
|
|
161
|
-
* ```
|
|
162
|
-
*
|
|
163
|
-
* See also:
|
|
164
|
-
* - `dispatch` which dispatches both sync and async actions.
|
|
165
|
-
* - `dispatchSync` which dispatches sync actions, and throws if the action is async.
|
|
166
|
-
* - `dispatchAll` which dispatches all given actions in parallel.
|
|
167
|
-
* - `dispatchAndWaitAll` which dispatches all given actions, and returns a Promise.
|
|
168
|
-
*/
|
|
169
|
-
export function useDispatchAndWait() {
|
|
170
|
-
let store = _useStoreFromContext();
|
|
171
|
-
return store.dispatchAndWait.bind(store);
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Dispatches all given actions in parallel, applying their reducers, and possibly changing
|
|
175
|
-
* the store state. The actions may be sync or async. It returns a Promise that resolves when
|
|
176
|
-
* ALL actions finish.
|
|
177
|
-
*
|
|
178
|
-
* ```ts
|
|
179
|
-
* const dispatchAndWaitAll = useDispatchAndWaitAll();
|
|
180
|
-
* await dispatchAndWaitAll([new BuyAction('IBM'), new SellAction('TSLA')]);
|
|
181
|
-
* ```
|
|
182
|
-
*
|
|
183
|
-
* Note: While the state change from the action's reducers will have been applied when the
|
|
184
|
-
* Promise resolves, other independent processes that the action may have started may still
|
|
185
|
-
* be in progress.
|
|
186
|
-
*
|
|
187
|
-
* See also:
|
|
188
|
-
* - `dispatch` which dispatches both sync and async actions.
|
|
189
|
-
* - `dispatchAndWait` which dispatches both sync and async actions, and returns a Promise.
|
|
190
|
-
* - `dispatchSync` which dispatches sync actions, and throws if the action is async.
|
|
191
|
-
* - `dispatchAll` which dispatches all given actions in parallel.
|
|
192
|
-
*/
|
|
193
|
-
export function useDispatchAndWaitAll() {
|
|
194
|
-
let store = _useStoreFromContext();
|
|
195
|
-
return store.dispatchAndWaitAll.bind(store);
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Dispatches all given actions in parallel, applying their reducer, and possibly changing
|
|
199
|
-
* the store state.
|
|
200
|
-
*
|
|
201
|
-
* ```ts
|
|
202
|
-
* const dispatchAll = useDispatchAll();
|
|
203
|
-
* dispatchAll([new BuyAction('IBM'), new SellAction('TSLA')]);
|
|
204
|
-
* ```
|
|
205
|
-
*
|
|
206
|
-
* See also:
|
|
207
|
-
* - `dispatch` which dispatches both sync and async actions.
|
|
208
|
-
* - `dispatchAndWait` which dispatches both sync and async actions, and returns a Promise.
|
|
209
|
-
* - `dispatchAndWaitAll` which dispatches all given actions, and returns a Promise.
|
|
210
|
-
* - `dispatchSync` which dispatches sync actions, and throws if the action is async.
|
|
211
|
-
*/
|
|
212
|
-
export function useDispatchAll() {
|
|
213
|
-
let store = _useStoreFromContext();
|
|
214
|
-
return store.dispatchAll.bind(store);
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* Dispatches the given action to the Redux store, to potentially change the state.
|
|
218
|
-
*
|
|
219
|
-
* This is exactly the same as the regular `dispatch`, except for the fact it
|
|
220
|
-
* will throw a `StoreException` if the action is ASYNC. Note an action is ASYNC
|
|
221
|
-
* if any of its `reduce()` or `before()` methods return a Promise.
|
|
222
|
-
*
|
|
223
|
-
* The only use for `dispatchSync` is when you need to guarantee (in runtime) that your
|
|
224
|
-
* action is SYNC, which means the state gets changed right after the dispatch call.
|
|
225
|
-
*
|
|
226
|
-
* See also:
|
|
227
|
-
* - `dispatch` which dispatches both sync and async actions.
|
|
228
|
-
* - `dispatchAndWait` which dispatches both sync and async actions, and returns a Promise.
|
|
229
|
-
* - `dispatchAndWaitAll` which dispatches all given actions, and returns a Promise.
|
|
230
|
-
* - `dispatchAll` which dispatches all given actions in parallel.
|
|
231
|
-
*/
|
|
232
|
-
export function useDispatchSync() {
|
|
233
|
-
let store = _useStoreFromContext();
|
|
234
|
-
return store.dispatchSync.bind(store);
|
|
235
|
-
}
|
|
236
|
-
/**
|
|
237
|
-
* You can use `isWaiting` and pass it an action `type`:
|
|
238
|
-
* - It returns true if an ASYNC action of the specific type is currently being processed.
|
|
239
|
-
* - It returns false if an ASYNC action of the specific type is NOT currently being processed.
|
|
240
|
-
* - This is only useful for ASYNC actions, since it always returns `false` when the action is SYNC.
|
|
241
|
-
*
|
|
242
|
-
* Note an action is ASYNC if it returns a promise from its `before` OR its `reduce` methods.
|
|
243
|
-
*
|
|
244
|
-
* ```ts
|
|
245
|
-
* const isWaiting = useIsWaiting(MyAction);
|
|
246
|
-
* if (isWaiting) { // Show a spinner }
|
|
247
|
-
* ```
|
|
248
|
-
*/
|
|
249
|
-
export function useIsWaiting(type) {
|
|
250
|
-
return _useStoreSelector((store) => store.isWaiting(type));
|
|
251
|
-
}
|
|
252
|
-
/**
|
|
253
|
-
* Returns true if the given action `type` failed with an `UserException`.
|
|
254
|
-
* Note: This method uses the EXACT action type. Subtypes are not considered.
|
|
255
|
-
*/
|
|
256
|
-
export function useIsFailed(type) {
|
|
257
|
-
return _useStoreSelector((store) => store.isFailed(type));
|
|
258
|
-
}
|
|
259
|
-
/**
|
|
260
|
-
* Returns the `UserException` of the `type` that failed.
|
|
261
|
-
* Note: This method uses the EXACT type in `type`. Subtypes are not considered.
|
|
262
|
-
*/
|
|
263
|
-
export function useExceptionFor(type) {
|
|
264
|
-
return _useStoreSelector((store) => store.exceptionFor(type));
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* Removes the exact given action `type` from the list of action types that failed.
|
|
268
|
-
* Note it clears the EXACT given type. Subtypes are not considered.
|
|
269
|
-
*
|
|
270
|
-
* Even if you never call this method explicitly, just dispatching an action already clears that action type
|
|
271
|
-
* from the list of failing action types. But you can call this method explicitly if you want to clear the
|
|
272
|
-
* action type before it's used again.
|
|
273
|
-
*
|
|
274
|
-
* Usage:
|
|
275
|
-
* ```ts
|
|
276
|
-
* const clearExceptionFor = useClearExceptionFor();
|
|
277
|
-
* clearExceptionFor(MyAction);
|
|
278
|
-
* clearExceptionFor(AnotherAction);
|
|
279
|
-
* ```
|
|
280
|
-
*/
|
|
281
|
-
export function useClearExceptionFor() {
|
|
282
|
-
const store = _useStoreFromContext();
|
|
283
|
-
return (type) => {
|
|
284
|
-
store.clearExceptionFor(type);
|
|
285
|
-
};
|
|
286
|
-
}
|
|
287
|
-
function _useStoreSelector(selector) {
|
|
288
|
-
// This ref will persist for the full lifetime of the component.
|
|
289
|
-
let ref = useRef(undefined);
|
|
290
|
-
// The initial value is the value selected from the current state in the store.
|
|
291
|
-
let store = _useStoreFromContext();
|
|
292
|
-
const [value, setValue] = useState(() => selector(store));
|
|
293
|
-
// Only once when the component mounts.
|
|
294
|
-
useEffect(() => {
|
|
295
|
-
// We save in the ref:
|
|
296
|
-
// - The selector
|
|
297
|
-
// - The currently selected value
|
|
298
|
-
// - The setValue function
|
|
299
|
-
//
|
|
300
|
-
// Whenever the state changes, the store will:
|
|
301
|
-
// 1. Retrieve all refs
|
|
302
|
-
// 2. Apply each selector to the current state to calculate the selected value.
|
|
303
|
-
// 3. And compare the selected value with the last selected value.
|
|
304
|
-
// 4. If it changed, it calls setValue.
|
|
305
|
-
ref.current = new RefStore([selector, value, setValue]);
|
|
306
|
-
// Save the new ref.
|
|
307
|
-
store._refStoreHooks.add(ref);
|
|
308
|
-
return () => {
|
|
309
|
-
// When the component unmounts, delete the ref.
|
|
310
|
-
store._refStoreHooks.delete(ref);
|
|
311
|
-
};
|
|
312
|
-
}, []);
|
|
313
|
-
return value;
|
|
314
|
-
}
|
|
315
|
-
function _useStoreFromContext() {
|
|
316
|
-
const context = useContext(StoreContext);
|
|
317
|
-
if (context === undefined) {
|
|
318
|
-
throw new StoreException('useStore must be used within a StoreProvider');
|
|
319
|
-
}
|
|
320
|
-
return context.store;
|
|
321
|
-
}
|
|
322
|
-
/**
|
|
323
|
-
* Helper class that allows us to write:
|
|
324
|
-
*
|
|
325
|
-
* ```js
|
|
326
|
-
* const store = useStore();
|
|
327
|
-
* store.dispatch(new MyAction());
|
|
328
|
-
* store.dispatchSync(new MyAction());
|
|
329
|
-
* await store.dispatchAndWait(new MyAction());
|
|
330
|
-
* ```
|
|
331
|
-
*/
|
|
332
|
-
class StoreDispatchers {
|
|
333
|
-
store;
|
|
334
|
-
constructor(store) {
|
|
335
|
-
this.store = store;
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* Dispatches the action to the Redux store, to potentially change the state.
|
|
339
|
-
*/
|
|
340
|
-
dispatch(action) {
|
|
341
|
-
return this.store.dispatch(action);
|
|
342
|
-
}
|
|
343
|
-
/**
|
|
344
|
-
* Dispatches an action and returns a promise that resolves when the action finishes.
|
|
345
|
-
* While the state change from the action's reducer will have been applied when the promise
|
|
346
|
-
* resolves, other independent processes that the action may have started may still be in
|
|
347
|
-
* progress.
|
|
348
|
-
*
|
|
349
|
-
* Usage: `await store.dispatchAndWait(new MyAction())`.
|
|
350
|
-
*/
|
|
351
|
-
dispatchAndWait(action) {
|
|
352
|
-
return this.store.dispatchAndWait(action);
|
|
353
|
-
}
|
|
354
|
-
/**
|
|
355
|
-
* Dispatches the given action to the Redux store, to potentially change the state.
|
|
356
|
-
*
|
|
357
|
-
* This is exactly the same as the regular `dispatch`, except for the fact it
|
|
358
|
-
* will throw a `StoreException` if the action is ASYNC. Note an action is ASYNC
|
|
359
|
-
* if any of its `reduce()` or `before()` methods return a Promise.
|
|
360
|
-
*
|
|
361
|
-
* The only use for `dispatchSync` is when you need to guarantee (in runtime) that your
|
|
362
|
-
* action is SYNC, which means the state gets changed right after the dispatch call.
|
|
363
|
-
*/
|
|
364
|
-
dispatchSync(action) {
|
|
365
|
-
return this.store.dispatchSync(action);
|
|
366
|
-
}
|
|
367
|
-
/**
|
|
368
|
-
* Returns a promise which will resolve when the given state `condition` is true.
|
|
369
|
-
* If the condition is already true when the method is called, the promise resolves immediately.
|
|
370
|
-
*
|
|
371
|
-
* You may also provide a `timeoutMillis`, which by default is 10 minutes. If you want, you
|
|
372
|
-
* can modify `TimeoutException.defaultTimeoutMillis` to change the default timeout.
|
|
373
|
-
* To disable the timeout, make it 0 or -1.
|
|
374
|
-
*
|
|
375
|
-
* This method is useful in tests, and it returns the action which changed
|
|
376
|
-
* the store state into the condition, in case you need it:
|
|
377
|
-
*
|
|
378
|
-
* ```typescript
|
|
379
|
-
* let action = await store.waitCondition((state) => state.name == "Bill");
|
|
380
|
-
* expect(action instanceof ChangeNameAction).toBe(true);
|
|
381
|
-
*
|
|
382
|
-
* This method is also eventually useful in production code, in which case you
|
|
383
|
-
* should avoid waiting for conditions that may take a very long time to complete,
|
|
384
|
-
* as checking the condition is an overhead to every state change.
|
|
385
|
-
*
|
|
386
|
-
* Examples:
|
|
387
|
-
*
|
|
388
|
-
* // Dispatches an actions that changes the state, then await for the state change:
|
|
389
|
-
* expect(store.state.name, 'John')
|
|
390
|
-
* dispatch(new ChangeNameAction("Bill"));
|
|
391
|
-
* let action = await store.waitCondition((state) => state.name == "Bill");
|
|
392
|
-
* expect(action instanceof ChangeNameAction).toBe(true);
|
|
393
|
-
* expect(store.state.name, 'Bill');
|
|
394
|
-
*
|
|
395
|
-
* // Dispatches actions and wait until no actions are in progress.
|
|
396
|
-
* dispatch(new BuyAction('IBM'));
|
|
397
|
-
* dispatch(new BuyAction('TSLA'));
|
|
398
|
-
* await waitAllActions([]);
|
|
399
|
-
* expect(state.stocks, ['IBM', 'TSLA']);
|
|
400
|
-
*
|
|
401
|
-
* // Dispatches two actions in PARALLEL and wait for their TYPES:
|
|
402
|
-
* expect(store.state.portfolio, ['TSLA']);
|
|
403
|
-
* dispatch(new BuyAction('IBM'));
|
|
404
|
-
* dispatch(new SellAction('TSLA'));
|
|
405
|
-
* await store.waitAllActionTypes([BuyAction, SellAction]);
|
|
406
|
-
* expect(store.state.portfolio, ['IBM']);
|
|
407
|
-
*
|
|
408
|
-
* // Dispatches actions in PARALLEL and wait until no actions are in progress.
|
|
409
|
-
* dispatch(new BuyAction('IBM'));
|
|
410
|
-
* dispatch(new BuyAction('TSLA'));
|
|
411
|
-
* await store.waitAllActions([]);
|
|
412
|
-
* expect(store.state.portfolio.includes('IBM', 'TSLA')).toBe(false);
|
|
413
|
-
*
|
|
414
|
-
* // Dispatches two actions in PARALLEL and wait for them:
|
|
415
|
-
* let action1 = new BuyAction('IBM');
|
|
416
|
-
* let action2 = new SellAction('TSLA');
|
|
417
|
-
* dispatch(action1);
|
|
418
|
-
* dispatch(action2);
|
|
419
|
-
* await store.waitAllActions([action1, action2]);
|
|
420
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
421
|
-
* expect(store.state.portfolio.includes('TSLA')).toBe(false);
|
|
422
|
-
*
|
|
423
|
-
* // Dispatches two actions in SERIES and wait for them:
|
|
424
|
-
* await dispatchAndWait(new BuyAction('IBM'));
|
|
425
|
-
* await dispatchAndWait(new SellAction('TSLA'));
|
|
426
|
-
* expect(store.state.portfolio.includes('IBM', 'TSLA')).toBe(false);
|
|
427
|
-
*
|
|
428
|
-
* // Wait until some action of a given type is dispatched.
|
|
429
|
-
* dispatch(new DoALotOfStuffAction());
|
|
430
|
-
* let action = store.waitActionType(ChangeNameAction);
|
|
431
|
-
* expect(action instanceof ChangeNameAction).toBe(true);
|
|
432
|
-
* expect(action.status.isCompletedOk).toBe(true);
|
|
433
|
-
* expect(store.state.name, 'Bill');
|
|
434
|
-
*
|
|
435
|
-
* // Wait until some action of the given types is dispatched.
|
|
436
|
-
* dispatch(new ProcessStocksAction());
|
|
437
|
-
* let action = store.waitAnyActionTypeFinishes([BuyAction, SellAction]);
|
|
438
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
439
|
-
*
|
|
440
|
-
* See also:
|
|
441
|
-
* `waitCondition` - Waits until the state is in a given condition.
|
|
442
|
-
* `waitActionCondition` - Waits until the actions in progress meet a given condition.
|
|
443
|
-
* `waitAllActions` - Waits until the given actions are NOT in progress, or no actions are in progress.
|
|
444
|
-
* `waitActionType` - Waits until an action of a given type is NOT in progress.
|
|
445
|
-
* `waitAllActionTypes` - Waits until all actions of the given type are NOT in progress.
|
|
446
|
-
* `waitAnyActionTypeFinishes` - Waits until ANY action of the given types finish dispatching.
|
|
447
|
-
*/
|
|
448
|
-
waitCondition(condition, timeoutMillis = null) {
|
|
449
|
-
return this.store.waitCondition(condition, timeoutMillis);
|
|
450
|
-
}
|
|
451
|
-
/**
|
|
452
|
-
* Returns a Promise that resolves when some actions meet the given `condition`.
|
|
453
|
-
*
|
|
454
|
-
* If `completeImmediately` is false (the default), this method will throw an error if the
|
|
455
|
-
* condition was already true when the method was called. Otherwise, the promise will complete
|
|
456
|
-
* immediately and throw no error.
|
|
457
|
-
*
|
|
458
|
-
* The `condition` is a function that takes the set of actions "in progress", as well as an
|
|
459
|
-
* action that just entered the set (by being dispatched) or left the set (by finishing
|
|
460
|
-
* dispatching). The function should return `true` when the condition is met, and `false`
|
|
461
|
-
* otherwise. For example:
|
|
462
|
-
*
|
|
463
|
-
* ```ts
|
|
464
|
-
* let action = await store.waitActionCondition((actionsInProgress, triggerAction) => { ... });
|
|
465
|
-
* ```
|
|
466
|
-
*
|
|
467
|
-
* Important: Your condition function should NOT modify the set of actions.
|
|
468
|
-
*
|
|
469
|
-
* You get back the set of the actions being dispatched that met the condition, as well as
|
|
470
|
-
* the action that triggered the condition by being added or removed from the set.
|
|
471
|
-
*
|
|
472
|
-
* Note: The condition is only checked when some action is dispatched or finishes dispatching.
|
|
473
|
-
* It's not checked every time action statuses change.
|
|
474
|
-
*
|
|
475
|
-
* You may also provide a `timeoutMillis`, which by default is 10 minutes.
|
|
476
|
-
* To disable the timeout, make it 0 or -1.
|
|
477
|
-
* If you want, you can modify `TimeoutException.defaultTimeoutMillis` to change the default timeout.
|
|
478
|
-
*
|
|
479
|
-
* Examples:
|
|
480
|
-
*
|
|
481
|
-
* ```ts
|
|
482
|
-
* // Dispatches an actions that changes the state, then await for the state change:
|
|
483
|
-
* expect(store.state.name).toBe('John');
|
|
484
|
-
* dispatch(new ChangeNameAction("Bill"));
|
|
485
|
-
* let action = await store.waitCondition((state) => state.name == "Bill");
|
|
486
|
-
* expect(action instanceof ChangeNameAction).toBe(true);
|
|
487
|
-
* expect(store.state.name).toBe('Bill');
|
|
488
|
-
*
|
|
489
|
-
* // Dispatches actions and wait until no actions are in progress.
|
|
490
|
-
* dispatch(new BuyAction('IBM'));
|
|
491
|
-
* dispatch(new BuyAction('TSLA'));
|
|
492
|
-
* await waitAllActions();
|
|
493
|
-
* expect(state.stocks).toEqual(['IBM', 'TSLA']);
|
|
494
|
-
*
|
|
495
|
-
* // Dispatches two actions in PARALLEL and wait for their TYPES:
|
|
496
|
-
* expect(store.state.portfolio).toEqual(['TSLA']);
|
|
497
|
-
* dispatch(new BuyAction('IBM'));
|
|
498
|
-
* dispatch(new SellAction('TSLA'));
|
|
499
|
-
* await store.waitAllActionTypes([BuyAction, SellAction]);
|
|
500
|
-
* expect(store.state.portfolio).toEqual(['IBM']);
|
|
501
|
-
*
|
|
502
|
-
* // Dispatches actions in PARALLEL and wait until no actions are in progress.
|
|
503
|
-
* dispatch(new BuyAction('IBM'));
|
|
504
|
-
* dispatch(new BuyAction('TSLA'));
|
|
505
|
-
* await store.waitAllActions();
|
|
506
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
507
|
-
* expect(store.state.portfolio.includes('TSLA')).toBe(true);
|
|
508
|
-
*
|
|
509
|
-
* // Dispatches two actions in PARALLEL and wait for them:
|
|
510
|
-
* let action1 = new BuyAction('IBM');
|
|
511
|
-
* let action2 = new SellAction('TSLA');
|
|
512
|
-
* dispatch(action1);
|
|
513
|
-
* dispatch(action2);
|
|
514
|
-
* await store.waitAllActions([action1, action2]);
|
|
515
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
516
|
-
* expect(store.state.portfolio.includes('TSLA')).toBe(false);
|
|
517
|
-
*
|
|
518
|
-
* // Dispatches two actions in SERIES and wait for them:
|
|
519
|
-
* await dispatchAndWait(new BuyAction('IBM'));
|
|
520
|
-
* await dispatchAndWait(new SellAction('TSLA'));
|
|
521
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
522
|
-
* expect(store.state.portfolio.includes('TSLA')).toBe(false);
|
|
523
|
-
*
|
|
524
|
-
* // Wait until some action of a given type is dispatched.
|
|
525
|
-
* dispatch(new DoALotOfStuffAction());
|
|
526
|
-
* let action = await store.waitActionType(ChangeNameAction);
|
|
527
|
-
* expect(action instanceof ChangeNameAction).toBe(true);
|
|
528
|
-
* expect(action.status.isCompletedOk).toBe(true);
|
|
529
|
-
* expect(store.state.name).toBe('Bill');
|
|
530
|
-
*
|
|
531
|
-
* // Wait until some action of the given types is dispatched.
|
|
532
|
-
* dispatch(new ProcessStocksAction());
|
|
533
|
-
* let action = await store.waitAnyActionTypeFinishes([BuyAction, SellAction]);
|
|
534
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
535
|
-
* ```
|
|
536
|
-
*
|
|
537
|
-
* See also:
|
|
538
|
-
* `waitCondition` - Waits until the state is in a given condition.
|
|
539
|
-
* `waitActionCondition` - Waits until the actions in progress meet a given condition.
|
|
540
|
-
* `waitAllActions` - Waits until the given actions are NOT in progress, or no actions are in progress.
|
|
541
|
-
* `waitActionType` - Waits until an action of a given type is NOT in progress.
|
|
542
|
-
* `waitAllActionTypes` - Waits until all actions of the given type are NOT in progress.
|
|
543
|
-
* `waitAnyActionTypeFinishes` - Waits until ANY action of the given types finish dispatching.
|
|
544
|
-
*
|
|
545
|
-
* You should only use this method in tests.
|
|
546
|
-
*/
|
|
547
|
-
async waitActionCondition(
|
|
548
|
-
//
|
|
549
|
-
/// The condition receives the current actions in progress, and the action that triggered the condition.
|
|
550
|
-
condition, {
|
|
551
|
-
// If `completeImmediately` is `false` (the default), this method will throw an error if the
|
|
552
|
-
// condition is already true when the method is called. Otherwise, the promise will complete
|
|
553
|
-
// immediately and throw no error.
|
|
554
|
-
completeImmediately = false,
|
|
555
|
-
//
|
|
556
|
-
// The maximum time to wait for the condition to be met. The default is 10 minutes.
|
|
557
|
-
// To disable the timeout, make it 0 or -1.
|
|
558
|
-
timeoutMillis = null, } = {}) {
|
|
559
|
-
return this.store.waitActionCondition(condition, {
|
|
560
|
-
completeImmediately: completeImmediately,
|
|
561
|
-
timeoutMillis: timeoutMillis
|
|
562
|
-
});
|
|
563
|
-
}
|
|
564
|
-
/**
|
|
565
|
-
* Returns a promise that resolves when ALL given actions finished dispatching.
|
|
566
|
-
*
|
|
567
|
-
* If `completeImmediately` is false (the default), this method will throw an error if none
|
|
568
|
-
* of the given actions are in progress when the method is called. Otherwise, the promise will
|
|
569
|
-
* complete immediately and throw no error.
|
|
570
|
-
*
|
|
571
|
-
* However, if you don't provide any actions (empty list or `null`), the promise will complete
|
|
572
|
-
* when ALL current actions in progress finish dispatching. In other words, when no actions are
|
|
573
|
-
* currently in progress. In this case, if [completeImmediately] is `false`, the method will
|
|
574
|
-
* throw an error if no actions are in progress when the method is called.
|
|
575
|
-
*
|
|
576
|
-
* Note: Waiting until no actions are in progress should only be done in test, never in
|
|
577
|
-
* production, as it's very easy to create a deadlock. However, waiting for specific actions to
|
|
578
|
-
* finish is safe in production, as long as you're waiting for actions you just dispatched.
|
|
579
|
-
*
|
|
580
|
-
* You may also provide a [timeoutMillis], which by default is 10 minutes.
|
|
581
|
-
* To disable the timeout, make it 0 or -1.
|
|
582
|
-
* If you want, you can modify `TimeoutException.defaultTimeoutMillis` to change the default timeout.
|
|
583
|
-
*
|
|
584
|
-
* Examples:
|
|
585
|
-
*
|
|
586
|
-
* ```ts
|
|
587
|
-
* // Dispatches an actions that changes the state, then await for the state change:
|
|
588
|
-
* expect(store.state.name).toBe('John');
|
|
589
|
-
* dispatch(new ChangeNameAction("Bill"));
|
|
590
|
-
* let action = await store.waitCondition((state) => state.name == "Bill");
|
|
591
|
-
* expect(action instanceof ChangeNameAction).toBe(true);
|
|
592
|
-
* expect(store.state.name).toBe('Bill');
|
|
593
|
-
*
|
|
594
|
-
* // Dispatches actions and wait until no actions are in progress.
|
|
595
|
-
* dispatch(new BuyAction('IBM'));
|
|
596
|
-
* dispatch(new BuyAction('TSLA'));
|
|
597
|
-
* await waitAllActions();
|
|
598
|
-
* expect(state.stocks).toEqual(['IBM', 'TSLA']);
|
|
599
|
-
*
|
|
600
|
-
* // Dispatches two actions in PARALLEL and wait for their TYPES:
|
|
601
|
-
* expect(store.state.portfolio).toEqual(['TSLA']);
|
|
602
|
-
* dispatch(new BuyAction('IBM'));
|
|
603
|
-
* dispatch(new SellAction('TSLA'));
|
|
604
|
-
* await store.waitAllActionTypes([BuyAction, SellAction]);
|
|
605
|
-
* expect(store.state.portfolio).toEqual(['IBM']);
|
|
606
|
-
*
|
|
607
|
-
* // Dispatches actions in PARALLEL and wait until no actions are in progress.
|
|
608
|
-
* dispatch(new BuyAction('IBM'));
|
|
609
|
-
* dispatch(new BuyAction('TSLA'));
|
|
610
|
-
* await store.waitAllActions();
|
|
611
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
612
|
-
* expect(store.state.portfolio.includes('TSLA')).toBe(true);
|
|
613
|
-
*
|
|
614
|
-
* // Dispatches two actions in PARALLEL and wait for them:
|
|
615
|
-
* let action1 = new BuyAction('IBM');
|
|
616
|
-
* let action2 = new SellAction('TSLA');
|
|
617
|
-
* dispatch(action1);
|
|
618
|
-
* dispatch(action2);
|
|
619
|
-
* await store.waitAllActions([action1, action2]);
|
|
620
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
621
|
-
* expect(store.state.portfolio.includes('TSLA')).toBe(false);
|
|
622
|
-
*
|
|
623
|
-
* // Dispatches two actions in SERIES and wait for them:
|
|
624
|
-
* await dispatchAndWait(new BuyAction('IBM'));
|
|
625
|
-
* await dispatchAndWait(new SellAction('TSLA'));
|
|
626
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
627
|
-
* expect(store.state.portfolio.includes('TSLA')).toBe(false);
|
|
628
|
-
*
|
|
629
|
-
* // Wait until some action of a given type is dispatched.
|
|
630
|
-
* dispatch(new DoALotOfStuffAction());
|
|
631
|
-
* let action = await store.waitActionType(ChangeNameAction);
|
|
632
|
-
* expect(action instanceof ChangeNameAction).toBe(true);
|
|
633
|
-
* expect(action.status.isCompletedOk).toBe(true);
|
|
634
|
-
* expect(store.state.name).toBe('Bill');
|
|
635
|
-
*
|
|
636
|
-
* // Wait until some action of the given types is dispatched.
|
|
637
|
-
* dispatch(new ProcessStocksAction());
|
|
638
|
-
* let action = await store.waitAnyActionTypeFinishes([BuyAction, SellAction]);
|
|
639
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
640
|
-
* ```
|
|
641
|
-
*
|
|
642
|
-
* See also:
|
|
643
|
-
* `waitCondition` - Waits until the state is in a given condition.
|
|
644
|
-
* `waitActionCondition` - Waits until the actions in progress meet a given condition.
|
|
645
|
-
* `waitAllActions` - Waits until the given actions are NOT in progress, or no actions are in progress.
|
|
646
|
-
* `waitActionType` - Waits until an action of a given type is NOT in progress.
|
|
647
|
-
* `waitAllActionTypes` - Waits until all actions of the given type are NOT in progress.
|
|
648
|
-
* `waitAnyActionTypeFinishes` - Waits until ANY action of the given types finish dispatching.
|
|
649
|
-
*
|
|
650
|
-
* You should only use this method in tests.
|
|
651
|
-
*/
|
|
652
|
-
waitAllActions(actions, { completeImmediately = false, timeoutMillis = null } = {}) {
|
|
653
|
-
return this.store.waitAllActions(actions, {
|
|
654
|
-
completeImmediately: completeImmediately,
|
|
655
|
-
timeoutMillis: timeoutMillis
|
|
656
|
-
});
|
|
657
|
-
}
|
|
658
|
-
/**
|
|
659
|
-
* Returns a promise that completes when an action of the given type in NOT in progress
|
|
660
|
-
* (it's not being dispatched):
|
|
661
|
-
*
|
|
662
|
-
* - If NO action of the given type is currently in progress when the method is called,
|
|
663
|
-
* and `completeImmediately` is false (the default), this method will throw an error.
|
|
664
|
-
*
|
|
665
|
-
* - If NO action of the given type is currently in progress when the method is called,
|
|
666
|
-
* and `completeImmediately` is true, the promise completes immediately, returns `null`,
|
|
667
|
-
* and throws no error.
|
|
668
|
-
*
|
|
669
|
-
* - If an action of the given type is in progress, the promise completes when the action
|
|
670
|
-
* finishes, and returns the action. You can use the returned action to check its `status`:
|
|
671
|
-
*
|
|
672
|
-
* ```dart
|
|
673
|
-
* var action = await store.waitActionType(MyAction);
|
|
674
|
-
* expect(action.status.originalError, isA<UserException>());
|
|
675
|
-
* ```
|
|
676
|
-
*
|
|
677
|
-
* You may also provide a `timeoutMillis`, which by default is 10 minutes.
|
|
678
|
-
* To disable the timeout, make it 0 or -1.
|
|
679
|
-
* If you want, you can modify `TimeoutException.defaultTimeoutMillis` to change the default timeout.
|
|
680
|
-
*
|
|
681
|
-
* Examples:
|
|
682
|
-
*
|
|
683
|
-
* ```ts
|
|
684
|
-
* // Dispatches an actions that changes the state, then await for the state change:
|
|
685
|
-
* expect(store.state.name).toBe('John');
|
|
686
|
-
* dispatch(new ChangeNameAction("Bill"));
|
|
687
|
-
* let action = await store.waitCondition((state) => state.name == "Bill");
|
|
688
|
-
* expect(action instanceof ChangeNameAction).toBe(true);
|
|
689
|
-
* expect(store.state.name).toBe('Bill');
|
|
690
|
-
*
|
|
691
|
-
* // Dispatches actions and wait until no actions are in progress.
|
|
692
|
-
* dispatch(new BuyAction('IBM'));
|
|
693
|
-
* dispatch(new BuyAction('TSLA'));
|
|
694
|
-
* await waitAllActions();
|
|
695
|
-
* expect(state.stocks).toEqual(['IBM', 'TSLA']);
|
|
696
|
-
*
|
|
697
|
-
* // Dispatches two actions in PARALLEL and wait for their TYPES:
|
|
698
|
-
* expect(store.state.portfolio).toEqual(['TSLA']);
|
|
699
|
-
* dispatch(new BuyAction('IBM'));
|
|
700
|
-
* dispatch(new SellAction('TSLA'));
|
|
701
|
-
* await store.waitAllActionTypes([BuyAction, SellAction]);
|
|
702
|
-
* expect(store.state.portfolio).toEqual(['IBM']);
|
|
703
|
-
*
|
|
704
|
-
* // Dispatches actions in PARALLEL and wait until no actions are in progress.
|
|
705
|
-
* dispatch(new BuyAction('IBM'));
|
|
706
|
-
* dispatch(new BuyAction('TSLA'));
|
|
707
|
-
* await store.waitAllActions();
|
|
708
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
709
|
-
* expect(store.state.portfolio.includes('TSLA')).toBe(true);
|
|
710
|
-
*
|
|
711
|
-
* // Dispatches two actions in PARALLEL and wait for them:
|
|
712
|
-
* let action1 = new BuyAction('IBM');
|
|
713
|
-
* let action2 = new SellAction('TSLA');
|
|
714
|
-
* dispatch(action1);
|
|
715
|
-
* dispatch(action2);
|
|
716
|
-
* await store.waitAllActions([action1, action2]);
|
|
717
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
718
|
-
* expect(store.state.portfolio.includes('TSLA')).toBe(false);
|
|
719
|
-
*
|
|
720
|
-
* // Dispatches two actions in SERIES and wait for them:
|
|
721
|
-
* await dispatchAndWait(new BuyAction('IBM'));
|
|
722
|
-
* await dispatchAndWait(new SellAction('TSLA'));
|
|
723
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
724
|
-
* expect(store.state.portfolio.includes('TSLA')).toBe(false);
|
|
725
|
-
*
|
|
726
|
-
* // Wait until some action of a given type is dispatched.
|
|
727
|
-
* dispatch(new DoALotOfStuffAction());
|
|
728
|
-
* let action = await store.waitActionType(ChangeNameAction);
|
|
729
|
-
* expect(action instanceof ChangeNameAction).toBe(true);
|
|
730
|
-
* expect(action.status.isCompletedOk).toBe(true);
|
|
731
|
-
* expect(store.state.name).toBe('Bill');
|
|
732
|
-
*
|
|
733
|
-
* // Wait until some action of the given types is dispatched.
|
|
734
|
-
* dispatch(new ProcessStocksAction());
|
|
735
|
-
* let action = await store.waitAnyActionTypeFinishes([BuyAction, SellAction]);
|
|
736
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
737
|
-
* ```
|
|
738
|
-
*
|
|
739
|
-
* See also:
|
|
740
|
-
* `waitCondition` - Waits until the state is in a given condition.
|
|
741
|
-
* `waitActionCondition` - Waits until the actions in progress meet a given condition.
|
|
742
|
-
* `waitAllActions` - Waits until the given actions are NOT in progress, or no actions are in progress.
|
|
743
|
-
* `waitActionType` - Waits until an action of a given type is NOT in progress.
|
|
744
|
-
* `waitAllActionTypes` - Waits until all actions of the given type are NOT in progress.
|
|
745
|
-
* `waitAnyActionTypeFinishes` - Waits until ANY action of the given types finish dispatching.
|
|
746
|
-
*
|
|
747
|
-
* You should only use this method in tests.
|
|
748
|
-
*/
|
|
749
|
-
async waitActionType(actionType, { completeImmediately = false, timeoutMillis = null } = {}) {
|
|
750
|
-
return this.store.waitActionType(actionType, {
|
|
751
|
-
completeImmediately: completeImmediately,
|
|
752
|
-
timeoutMillis: timeoutMillis
|
|
753
|
-
});
|
|
754
|
-
}
|
|
755
|
-
/**
|
|
756
|
-
* Returns a promise that completes when ALL actions of the given type are NOT in progress
|
|
757
|
-
* (none of them is being dispatched):
|
|
758
|
-
*
|
|
759
|
-
* - If NO action of the given types is currently in progress when the method is called,
|
|
760
|
-
* and `completeImmediately` is false (the default), this method will throw an error.
|
|
761
|
-
*
|
|
762
|
-
* - If NO action of the given type is currently in progress when the method is called,
|
|
763
|
-
* and `completeImmediately` is true, the promise completes immediately and throws no error.
|
|
764
|
-
*
|
|
765
|
-
* - If any action of the given types is in progress, the promise completes only when
|
|
766
|
-
* no action of the given types is in progress anymore.
|
|
767
|
-
*
|
|
768
|
-
* You may also provide a `timeoutMillis`, which by default is 10 minutes.
|
|
769
|
-
* To disable the timeout, make it 0 or -1.
|
|
770
|
-
* If you want, you can modify `TimeoutException.defaultTimeoutMillis` to change the default timeout.
|
|
771
|
-
*
|
|
772
|
-
* Examples:
|
|
773
|
-
*
|
|
774
|
-
* ```ts
|
|
775
|
-
* // Dispatches an actions that changes the state, then await for the state change:
|
|
776
|
-
* expect(store.state.name).toBe('John');
|
|
777
|
-
* dispatch(new ChangeNameAction("Bill"));
|
|
778
|
-
* let action = await store.waitCondition((state) => state.name == "Bill");
|
|
779
|
-
* expect(action instanceof ChangeNameAction).toBe(true);
|
|
780
|
-
* expect(store.state.name).toBe('Bill');
|
|
781
|
-
*
|
|
782
|
-
* // Dispatches actions and wait until no actions are in progress.
|
|
783
|
-
* dispatch(new BuyAction('IBM'));
|
|
784
|
-
* dispatch(new BuyAction('TSLA'));
|
|
785
|
-
* await waitAllActions();
|
|
786
|
-
* expect(state.stocks).toEqual(['IBM', 'TSLA']);
|
|
787
|
-
*
|
|
788
|
-
* // Dispatches two actions in PARALLEL and wait for their TYPES:
|
|
789
|
-
* expect(store.state.portfolio).toEqual(['TSLA']);
|
|
790
|
-
* dispatch(new BuyAction('IBM'));
|
|
791
|
-
* dispatch(new SellAction('TSLA'));
|
|
792
|
-
* await store.waitAllActionTypes([BuyAction, SellAction]);
|
|
793
|
-
* expect(store.state.portfolio).toEqual(['IBM']);
|
|
794
|
-
*
|
|
795
|
-
* // Dispatches actions in PARALLEL and wait until no actions are in progress.
|
|
796
|
-
* dispatch(new BuyAction('IBM'));
|
|
797
|
-
* dispatch(new BuyAction('TSLA'));
|
|
798
|
-
* await store.waitAllActions();
|
|
799
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
800
|
-
* expect(store.state.portfolio.includes('TSLA')).toBe(true);
|
|
801
|
-
*
|
|
802
|
-
* // Dispatches two actions in PARALLEL and wait for them:
|
|
803
|
-
* let action1 = new BuyAction('IBM');
|
|
804
|
-
* let action2 = new SellAction('TSLA');
|
|
805
|
-
* dispatch(action1);
|
|
806
|
-
* dispatch(action2);
|
|
807
|
-
* await store.waitAllActions([action1, action2]);
|
|
808
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
809
|
-
* expect(store.state.portfolio.includes('TSLA')).toBe(false);
|
|
810
|
-
*
|
|
811
|
-
* // Dispatches two actions in SERIES and wait for them:
|
|
812
|
-
* await dispatchAndWait(new BuyAction('IBM'));
|
|
813
|
-
* await dispatchAndWait(new SellAction('TSLA'));
|
|
814
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
815
|
-
* expect(store.state.portfolio.includes('TSLA')).toBe(false);
|
|
816
|
-
*
|
|
817
|
-
* // Wait until some action of a given type is dispatched.
|
|
818
|
-
* dispatch(new DoALotOfStuffAction());
|
|
819
|
-
* let action = await store.waitActionType(ChangeNameAction);
|
|
820
|
-
* expect(action instanceof ChangeNameAction).toBe(true);
|
|
821
|
-
* expect(action.status.isCompletedOk).toBe(true);
|
|
822
|
-
* expect(store.state.name).toBe('Bill');
|
|
823
|
-
*
|
|
824
|
-
* // Wait until some action of the given types is dispatched.
|
|
825
|
-
* dispatch(new ProcessStocksAction());
|
|
826
|
-
* let action = await store.waitAnyActionTypeFinishes([BuyAction, SellAction]);
|
|
827
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
828
|
-
* ```
|
|
829
|
-
*
|
|
830
|
-
* See also:
|
|
831
|
-
* `waitCondition` - Waits until the state is in a given condition.
|
|
832
|
-
* `waitActionCondition` - Waits until the actions in progress meet a given condition.
|
|
833
|
-
* `waitAllActions` - Waits until the given actions are NOT in progress, or no actions are in progress.
|
|
834
|
-
* `waitActionType` - Waits until an action of a given type is NOT in progress.
|
|
835
|
-
* `waitAllActionTypes` - Waits until all actions of the given type are NOT in progress.
|
|
836
|
-
* `waitAnyActionTypeFinishes` - Waits until ANY action of the given types finish dispatching.
|
|
837
|
-
*
|
|
838
|
-
* You should only use this method in tests.
|
|
839
|
-
*/
|
|
840
|
-
async waitAllActionTypes(actionTypes, { completeImmediately = false, timeoutMillis = null } = {}) {
|
|
841
|
-
return this.store.waitAllActionTypes(actionTypes, {
|
|
842
|
-
completeImmediately: completeImmediately,
|
|
843
|
-
timeoutMillis: timeoutMillis
|
|
844
|
-
});
|
|
845
|
-
}
|
|
846
|
-
/**
|
|
847
|
-
* Returns a promise which will complete when ANY action of the given types FINISHES
|
|
848
|
-
* dispatching. IMPORTANT: This method is different from the other similar methods, because
|
|
849
|
-
* it does NOT complete immediately if no action of the given types is in progress. Instead,
|
|
850
|
-
* it waits until an action of the given types finishes dispatching, even if they
|
|
851
|
-
* were not yet in progress when the method was called.
|
|
852
|
-
*
|
|
853
|
-
* This method returns the action that completed the promise, which you can use to check
|
|
854
|
-
* its `status`.
|
|
855
|
-
*
|
|
856
|
-
* It's useful when the actions you are waiting for are not yet dispatched when you call this
|
|
857
|
-
* method. For example, suppose action `StartAction` starts a process that takes some time
|
|
858
|
-
* to run and then dispatches an action called `MyFinalAction`. You can then write:
|
|
859
|
-
*
|
|
860
|
-
* ```dart
|
|
861
|
-
* dispatch(StartAction());
|
|
862
|
-
* let action = await store.waitAnyActionTypeFinishes([MyFinalAction]);
|
|
863
|
-
* expect(action.status.originalError).toBeInstanceOf(UserException>);
|
|
864
|
-
* ```
|
|
865
|
-
*
|
|
866
|
-
* You may also provide a `timeoutMillis`, which by default is 10 minutes.
|
|
867
|
-
* To disable the timeout, make it 0 or -1.
|
|
868
|
-
* If you want, you can modify `TimeoutException.defaultTimeoutMillis` to change the default timeout.
|
|
869
|
-
*
|
|
870
|
-
* Examples:
|
|
871
|
-
*
|
|
872
|
-
* ```ts
|
|
873
|
-
* // Dispatches an actions that changes the state, then await for the state change:
|
|
874
|
-
* expect(store.state.name).toBe('John');
|
|
875
|
-
* dispatch(new ChangeNameAction("Bill"));
|
|
876
|
-
* let action = await store.waitCondition((state) => state.name == "Bill");
|
|
877
|
-
* expect(action instanceof ChangeNameAction).toBe(true);
|
|
878
|
-
* expect(store.state.name).toBe('Bill');
|
|
879
|
-
*
|
|
880
|
-
* // Dispatches actions and wait until no actions are in progress.
|
|
881
|
-
* dispatch(new BuyAction('IBM'));
|
|
882
|
-
* dispatch(new BuyAction('TSLA'));
|
|
883
|
-
* await waitAllActions();
|
|
884
|
-
* expect(state.stocks).toEqual(['IBM', 'TSLA']);
|
|
885
|
-
*
|
|
886
|
-
* // Dispatches two actions in PARALLEL and wait for their TYPES:
|
|
887
|
-
* expect(store.state.portfolio).toEqual(['TSLA']);
|
|
888
|
-
* dispatch(new BuyAction('IBM'));
|
|
889
|
-
* dispatch(new SellAction('TSLA'));
|
|
890
|
-
* await store.waitAllActionTypes([BuyAction, SellAction]);
|
|
891
|
-
* expect(store.state.portfolio).toEqual(['IBM']);
|
|
892
|
-
*
|
|
893
|
-
* // Dispatches actions in PARALLEL and wait until no actions are in progress.
|
|
894
|
-
* dispatch(new BuyAction('IBM'));
|
|
895
|
-
* dispatch(new BuyAction('TSLA'));
|
|
896
|
-
* await store.waitAllActions();
|
|
897
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
898
|
-
* expect(store.state.portfolio.includes('TSLA')).toBe(true);
|
|
899
|
-
*
|
|
900
|
-
* // Dispatches two actions in PARALLEL and wait for them:
|
|
901
|
-
* let action1 = new BuyAction('IBM');
|
|
902
|
-
* let action2 = new SellAction('TSLA');
|
|
903
|
-
* dispatch(action1);
|
|
904
|
-
* dispatch(action2);
|
|
905
|
-
* await store.waitAllActions([action1, action2]);
|
|
906
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
907
|
-
* expect(store.state.portfolio.includes('TSLA')).toBe(false);
|
|
908
|
-
*
|
|
909
|
-
* // Dispatches two actions in SERIES and wait for them:
|
|
910
|
-
* await dispatchAndWait(new BuyAction('IBM'));
|
|
911
|
-
* await dispatchAndWait(new SellAction('TSLA'));
|
|
912
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
913
|
-
* expect(store.state.portfolio.includes('TSLA')).toBe(false);
|
|
914
|
-
*
|
|
915
|
-
* // Wait until some action of a given type is dispatched.
|
|
916
|
-
* dispatch(new DoALotOfStuffAction());
|
|
917
|
-
* let action = await store.waitActionType(ChangeNameAction);
|
|
918
|
-
* expect(action instanceof ChangeNameAction).toBe(true);
|
|
919
|
-
* expect(action.status.isCompletedOk).toBe(true);
|
|
920
|
-
* expect(store.state.name).toBe('Bill');
|
|
921
|
-
*
|
|
922
|
-
* // Wait until some action of the given types is dispatched.
|
|
923
|
-
* dispatch(new ProcessStocksAction());
|
|
924
|
-
* let action = await store.waitAnyActionTypeFinishes([BuyAction, SellAction]);
|
|
925
|
-
* expect(store.state.portfolio.includes('IBM')).toBe(true);
|
|
926
|
-
* ```
|
|
927
|
-
*
|
|
928
|
-
* See also:
|
|
929
|
-
* `waitCondition` - Waits until the state is in a given condition.
|
|
930
|
-
* `waitActionCondition` - Waits until the actions in progress meet a given condition.
|
|
931
|
-
* `waitAllActions` - Waits until the given actions are NOT in progress, or no actions are in progress.
|
|
932
|
-
* `waitActionType` - Waits until an action of a given type is NOT in progress.
|
|
933
|
-
* `waitAllActionTypes` - Waits until all actions of the given type are NOT in progress.
|
|
934
|
-
* `waitAnyActionTypeFinishes` - Waits until ANY action of the given types finish dispatching.
|
|
935
|
-
*
|
|
936
|
-
* You should only use this method in tests.
|
|
937
|
-
*/
|
|
938
|
-
async waitAnyActionTypeFinishes(actionTypes, { timeoutMillis = null } = {}) {
|
|
939
|
-
return this.store.waitAnyActionTypeFinishes(actionTypes, {
|
|
940
|
-
timeoutMillis: timeoutMillis
|
|
941
|
-
});
|
|
942
|
-
}
|
|
943
|
-
}
|
|
944
|
-
class RefState {
|
|
945
|
-
selectorAndValueAndSetValue;
|
|
946
|
-
constructor(selectorAndValueAndSetValue) {
|
|
947
|
-
this.selectorAndValueAndSetValue = selectorAndValueAndSetValue;
|
|
948
|
-
}
|
|
949
|
-
}
|
|
950
|
-
class RefStore {
|
|
951
|
-
selectorAndValueAndSetValue;
|
|
952
|
-
constructor(selectorAndValueAndSetValue) {
|
|
953
|
-
this.selectorAndValueAndSetValue = selectorAndValueAndSetValue;
|
|
954
|
-
}
|
|
955
|
-
}
|