state-decorator 7.3.0 → 7.3.1-destroy.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/v5.d.ts CHANGED
@@ -1,45 +1,45 @@
1
- import type { DecoratedActions, MiddlewareFactory } from './types';
2
- import { ConflictPolicy } from './types';
3
- import { setGlobalConfig } from './index';
4
- import type { AdvancedSynchAction, AsynchActionPromiseGet, StateDecoratorAction, StateDecoratorActions, StateDecoratorOptions, AsynchActionPromise, SynchAction, LoadingMap, LoadingProps, LoadingMapParallelActions, AsynchAction } from './v5_types';
5
- export type { AdvancedSynchAction, AsynchActionPromiseGet, StateDecoratorAction, StateDecoratorActions, StateDecoratorOptions, AsynchActionPromise, SynchAction, LoadingMap, LoadingMapParallelActions, LoadingProps, ConflictPolicy, setGlobalConfig, };
6
- /**
7
- * Type guard function to test if an action is a asynchronous action.
8
- */
9
- export declare function isAsyncAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>): action is AsynchAction<S, F, A, P>;
10
- /**
11
- * Type guard function to test if an action is a asynchronous action.
12
- */
13
- export declare function isAsyncPromiseGetAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>): action is AsynchActionPromiseGet<S, F, A, P>;
14
- /**
15
- * Type guard function to test if an action is a synchronous action.
16
- */
17
- export declare function isSyncAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>): action is SynchAction<S, F, P>;
18
- /**
19
- * Type guard function to test if an action is a synchronous action.
20
- */
21
- export declare function isAdvancedSyncAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>): action is AdvancedSynchAction<S, F, A, P>;
22
- declare function useStateDecorator<S, A extends DecoratedActions, P>(getInitialState: (props: P) => S, actions: StateDecoratorActions<S, A, P>, props: P, options?: StateDecoratorOptions<S, A, P>, middlewares?: MiddlewareFactory<S, A, P>[]): {
23
- state: S & {};
24
- actions: A;
25
- loading: boolean;
26
- loadingMap: import("./types").LoadingMap<A>;
27
- loadingParallelMap: import("./types").LoadingParallelMap<A>;
28
- abortAction: (actionName: keyof A, promiseId?: string) => boolean;
29
- };
30
- export default useStateDecorator;
31
- /**
32
- * Hook to execute a function when hook is mounted.
33
- * @param onMount function to execute on the hook nount
34
- */
35
- export declare function useOnMount(onMount: () => any): void;
36
- /**
37
- * Hook to execute a function when hook is unmounted.
38
- * @param onUnMount function to execute on the hook nount
39
- */
40
- export declare function useOnUnmount(onUnmount: () => void, propList?: any[]): void;
41
- /**
42
- * Hook to execute a function when hook is unmounted.
43
- * @param onUnMount function to execute on the hook nount
44
- */
45
- export declare function useOnUnload(onUnload: () => void): void;
1
+ import type { DecoratedActions, MiddlewareFactory } from './types';
2
+ import { ConflictPolicy } from './types';
3
+ import { setGlobalConfig } from './index';
4
+ import type { AdvancedSynchAction, AsynchActionPromiseGet, StateDecoratorAction, StateDecoratorActions, StateDecoratorOptions, AsynchActionPromise, SynchAction, LoadingMap, LoadingProps, LoadingMapParallelActions, AsynchAction } from './v5_types';
5
+ export type { AdvancedSynchAction, AsynchActionPromiseGet, StateDecoratorAction, StateDecoratorActions, StateDecoratorOptions, AsynchActionPromise, SynchAction, LoadingMap, LoadingMapParallelActions, LoadingProps, ConflictPolicy, setGlobalConfig, };
6
+ /**
7
+ * Type guard function to test if an action is a asynchronous action.
8
+ */
9
+ export declare function isAsyncAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>): action is AsynchAction<S, F, A, P>;
10
+ /**
11
+ * Type guard function to test if an action is a asynchronous action.
12
+ */
13
+ export declare function isAsyncPromiseGetAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>): action is AsynchActionPromiseGet<S, F, A, P>;
14
+ /**
15
+ * Type guard function to test if an action is a synchronous action.
16
+ */
17
+ export declare function isSyncAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>): action is SynchAction<S, F, P>;
18
+ /**
19
+ * Type guard function to test if an action is a synchronous action.
20
+ */
21
+ export declare function isAdvancedSyncAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>): action is AdvancedSynchAction<S, F, A, P>;
22
+ declare function useStateDecorator<S, A extends DecoratedActions, P>(getInitialState: (props: P) => S, actions: StateDecoratorActions<S, A, P>, props: P, options?: StateDecoratorOptions<S, A, P>, middlewares?: MiddlewareFactory<S, A, P>[]): {
23
+ state: S & {};
24
+ actions: A;
25
+ loading: boolean;
26
+ loadingMap: import("./types").LoadingMap<A>;
27
+ loadingParallelMap: import("./types").LoadingParallelMap<A>;
28
+ abortAction: (actionName: keyof A, promiseId?: string) => boolean;
29
+ };
30
+ export default useStateDecorator;
31
+ /**
32
+ * Hook to execute a function when hook is mounted.
33
+ * @param onMount function to execute on the hook nount
34
+ */
35
+ export declare function useOnMount(onMount: () => any): void;
36
+ /**
37
+ * Hook to execute a function when hook is unmounted.
38
+ * @param onUnMount function to execute on the hook nount
39
+ */
40
+ export declare function useOnUnmount(onUnmount: () => void, propList?: any[]): void;
41
+ /**
42
+ * Hook to execute a function when hook is unmounted.
43
+ * @param onUnMount function to execute on the hook nount
44
+ */
45
+ export declare function useOnUnload(onUnload: () => void): void;
package/v5_test.d.ts CHANGED
@@ -1,57 +1,57 @@
1
- import type { StoreAction } from './types';
2
- import { ConflictPolicy } from './types';
3
- import type { AdvancedSynchAction, AsynchActionPromiseGet, StateDecoratorAction, StateDecoratorActions, StateDecoratorOptions, AsynchActionPromise, SynchAction, LoadingMap, LoadingProps, LoadingMapParallelActions, AsynchAction } from './v5_types';
4
- import { setGlobalConfig } from './impl';
5
- export type { AdvancedSynchAction, AsynchActionPromiseGet, StateDecoratorAction, StateDecoratorActions, StateDecoratorOptions, AsynchActionPromise, SynchAction, LoadingMap, LoadingMapParallelActions, LoadingProps, ConflictPolicy, setGlobalConfig, };
6
- /**
7
- * Type guard function to test if an action is a asynchronous action.
8
- */
9
- export declare function isAsyncAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>): action is AsynchAction<S, F, A, P>;
10
- /**
11
- * Type guard function to test if an action is a asynchronous action.
12
- */
13
- export declare function isAsyncPromiseGetAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>): action is AsynchActionPromiseGet<S, F, A, P>;
14
- /**
15
- * Type guard function to test if an action is a synchronous action.
16
- */
17
- export declare function isSyncAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>): action is SynchAction<S, F, P>;
18
- /**
19
- * Type guard function to test if an action is a synchronous action.
20
- */
21
- export declare function isAdvancedSyncAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>): action is AdvancedSynchAction<S, F, A, P>;
22
- /**
23
- * Utility to test an asynchronous action.
24
- * @param action The action to test
25
- * @param test The test function that takes the discrimined action type and cam return a promise
26
- */
27
- export declare function testAsyncAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>, test: (action: AsynchActionPromise<S, F, A, P>) => any | Promise<any>): any;
28
- /**
29
- * Utility to test an synchronous action.
30
- * @param action The action to test
31
- * @param test The test function that takes the discrimined action type and cam return a promise
32
- */
33
- export declare function testSyncAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>, test: (action: SynchAction<S, F, P>) => any | Promise<any>): any;
34
- /**
35
- * Utility to test an advanced synchronous action.
36
- * @param action The action to test
37
- * @param test The test function that takes the discrimined action type and cam return a promise
38
- */
39
- export declare function testAdvancedSyncAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>, test: (action: AdvancedSynchAction<S, F, A, P>) => any | Promise<any>): any;
40
- /**
41
- * Utility to test a V6 synchronous action using v5 test.
42
- * @param action The action to test
43
- * @param test The test function that takes the discrimined action type and cam return a promise
44
- */
45
- export declare function testV6SyncAction<S, F extends (...args: any[]) => any, A, P>(action: StoreAction<S, F, A, P>, test: (action: SynchAction<S, F, P>) => any | Promise<any>): Promise<void>;
46
- /**
47
- * Utility to test a V6 synchronous action using v5 test.
48
- * @param actionIn The action to test
49
- * @param test The test function that takes the discrimined action type and cam return a promise
50
- */
51
- export declare function testV6AdvancedSyncAction<S, F extends (...args: any[]) => any, A, P>(actionIn: StoreAction<S, F, A, P>, test: (action: AdvancedSynchAction<S, F, A, P>) => any | Promise<any>): Promise<void>;
52
- /**
53
- * Utility to test a V6 synchronous action using v5 test.
54
- * @param actionIn The action to test
55
- * @param test The test function that takes the discrimined action type and cam return a promise
56
- */
57
- export declare function testV6AsyncAction<S, F extends (...args: any[]) => any, A, P>(actionIn: StoreAction<S, F, A, P>, test: (action: AsynchAction<S, F, A, P>) => any | Promise<any>): any;
1
+ import type { StoreAction } from './types';
2
+ import { ConflictPolicy } from './types';
3
+ import type { AdvancedSynchAction, AsynchActionPromiseGet, StateDecoratorAction, StateDecoratorActions, StateDecoratorOptions, AsynchActionPromise, SynchAction, LoadingMap, LoadingProps, LoadingMapParallelActions, AsynchAction } from './v5_types';
4
+ import { setGlobalConfig } from './impl';
5
+ export type { AdvancedSynchAction, AsynchActionPromiseGet, StateDecoratorAction, StateDecoratorActions, StateDecoratorOptions, AsynchActionPromise, SynchAction, LoadingMap, LoadingMapParallelActions, LoadingProps, ConflictPolicy, setGlobalConfig, };
6
+ /**
7
+ * Type guard function to test if an action is a asynchronous action.
8
+ */
9
+ export declare function isAsyncAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>): action is AsynchAction<S, F, A, P>;
10
+ /**
11
+ * Type guard function to test if an action is a asynchronous action.
12
+ */
13
+ export declare function isAsyncPromiseGetAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>): action is AsynchActionPromiseGet<S, F, A, P>;
14
+ /**
15
+ * Type guard function to test if an action is a synchronous action.
16
+ */
17
+ export declare function isSyncAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>): action is SynchAction<S, F, P>;
18
+ /**
19
+ * Type guard function to test if an action is a synchronous action.
20
+ */
21
+ export declare function isAdvancedSyncAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>): action is AdvancedSynchAction<S, F, A, P>;
22
+ /**
23
+ * Utility to test an asynchronous action.
24
+ * @param action The action to test
25
+ * @param test The test function that takes the discrimined action type and cam return a promise
26
+ */
27
+ export declare function testAsyncAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>, test: (action: AsynchActionPromise<S, F, A, P>) => any | Promise<any>): any;
28
+ /**
29
+ * Utility to test an synchronous action.
30
+ * @param action The action to test
31
+ * @param test The test function that takes the discrimined action type and cam return a promise
32
+ */
33
+ export declare function testSyncAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>, test: (action: SynchAction<S, F, P>) => any | Promise<any>): any;
34
+ /**
35
+ * Utility to test an advanced synchronous action.
36
+ * @param action The action to test
37
+ * @param test The test function that takes the discrimined action type and cam return a promise
38
+ */
39
+ export declare function testAdvancedSyncAction<S, F extends (...args: any[]) => any, A, P>(action: StateDecoratorAction<S, F, A, P>, test: (action: AdvancedSynchAction<S, F, A, P>) => any | Promise<any>): any;
40
+ /**
41
+ * Utility to test a V6 synchronous action using v5 test.
42
+ * @param action The action to test
43
+ * @param test The test function that takes the discrimined action type and cam return a promise
44
+ */
45
+ export declare function testV6SyncAction<S, F extends (...args: any[]) => any, A, P>(action: StoreAction<S, F, A, P>, test: (action: SynchAction<S, F, P>) => any | Promise<any>): Promise<void>;
46
+ /**
47
+ * Utility to test a V6 synchronous action using v5 test.
48
+ * @param actionIn The action to test
49
+ * @param test The test function that takes the discrimined action type and cam return a promise
50
+ */
51
+ export declare function testV6AdvancedSyncAction<S, F extends (...args: any[]) => any, A, P>(actionIn: StoreAction<S, F, A, P>, test: (action: AdvancedSynchAction<S, F, A, P>) => any | Promise<any>): Promise<void>;
52
+ /**
53
+ * Utility to test a V6 synchronous action using v5 test.
54
+ * @param actionIn The action to test
55
+ * @param test The test function that takes the discrimined action type and cam return a promise
56
+ */
57
+ export declare function testV6AsyncAction<S, F extends (...args: any[]) => any, A, P>(actionIn: StoreAction<S, F, A, P>, test: (action: AsynchAction<S, F, A, P>) => any | Promise<any>): any;
package/v5_types.d.ts CHANGED
@@ -1,213 +1,213 @@
1
- import { ConflictPolicy, DecoratedActions, NotifyFunc, PromiseResult } from './types';
2
- export type PromiseProvider<S, F extends (...args: any[]) => any, A, P> = (args: Parameters<F>, state: S, props: P, actions: A, abortSignal: AbortSignal) => ReturnType<F> | null;
3
- /**
4
- * Simple form of a synchronous action.
5
- */
6
- export type SynchAction<S, F extends (...args: any[]) => any, P> = (state: S, args: Parameters<F>, props: P) => S | null;
7
- /**
8
- * Advanced synchronous action.
9
- * Allows to set advanced properties to a synchronous action.
10
- */
11
- export type AdvancedSynchAction<S, F extends (...args: any[]) => any, A, P> = {
12
- /**
13
- * The action to execute.
14
- */
15
- action: (state: S, args: Parameters<F>, props: P) => S | null;
16
- /**
17
- * Debounces the side effects if this parameter is defined.
18
- */
19
- debounceSideEffectsTimeout?: number;
20
- /**
21
- * Debounces the action if this parameter is defined.
22
- */
23
- debounceTimeout?: number;
24
- /**
25
- * Action to call when the action is done. Used to trigger other actions (even asynchronous),
26
- */
27
- onActionDone?: (state: S, args: Parameters<F>, props: P, actions: A, notifyWarning: NotifyFunc) => void;
28
- };
29
- export type PromiseIdMap = {
30
- [promiseId: string]: boolean;
31
- };
32
- export type InternalLoadingMap<A> = {
33
- [name: string]: undefined | boolean | PromiseIdMap;
34
- };
35
- export type LoadingMap<A> = {
36
- [P in keyof A]?: undefined | boolean;
37
- };
38
- export type LoadingMapParallelActions<A> = {
39
- [P in keyof A]?: PromiseIdMap;
40
- };
41
- export type LoadingProps<A> = {
42
- loading: boolean;
43
- loadingMap: LoadingMap<A>;
44
- loadingParallelMap: LoadingMapParallelActions<A>;
45
- };
46
- /**
47
- * Try to abort an action, if the action is <code>abortable</code>.
48
- * @param actionName The action name
49
- * @param promiseId If the action conflict type is <code>ConflictType.PARALLEL</code>, the identifier of the promise.
50
- * @returns <code>true</code> if the action is abortable and an action is ongoing, <code>false</code> otherwise.
51
- */
52
- export type AbortActionCallback<A> = (actionName: keyof A, promiseId?: string) => boolean;
53
- export type GetErrorMessage<F extends (...args: any[]) => any, P> = (e: any, args: Parameters<F>, props: P) => string;
54
- export interface AsynchActionBase<S, F extends (...args: any[]) => any, A, P> {
55
- /**
56
- * This action can be aborted. An abortSignal will be injected to the <code>promise</code> / <code>promiseGet</code>.
57
- */
58
- abortable?: boolean;
59
- /**
60
- * The success message to pass to the notifySuccess function passed as property to the StateDecorator.
61
- */
62
- successMessage?: string;
63
- /**
64
- * A function that provides the success message to pass to the notifySuccess function passed as property to the StateDecorator.
65
- */
66
- getSuccessMessage?: (result: PromiseResult<ReturnType<F>>, args: Parameters<F>, props: P) => string;
67
- /**
68
- * The error message to pass to the notifyError function passed as property to the StateDecorator.
69
- */
70
- errorMessage?: string;
71
- /**
72
- * A function that provides the error message to pass to the notifyError function passed as property to the StateDecorator.
73
- */
74
- getErrorMessage?: GetErrorMessage<F, P>;
75
- /**
76
- * If set, called with the result of the promise to update the current state.
77
- */
78
- reducer?: (state: S, result: PromiseResult<ReturnType<F>>, args: Parameters<F>, props: P) => S | null;
79
- /**
80
- * If set, called with the error of the promise to update the current state.
81
- */
82
- errorReducer?: (state: S, error: any, args: Parameters<F>, props: P) => S | null;
83
- /**
84
- * Whether reject the promise instead of handling it.
85
- */
86
- rejectPromiseOnError?: boolean;
87
- /**
88
- * Handle side effects when the request succeeded.
89
- * @param result The result of the request
90
- * @param newData The data after the reducer is applied
91
- * @param args The argument during the call of the request function
92
- * @param props The props passed to the state decorator
93
- * @param actions The actions available
94
- * @param notifyWarning If specified, a notify function to indicate that the action is a semi success (use successMessage / getSuccessMessage otherwise).
95
- */
96
- onDone?: (state: S, result: PromiseResult<ReturnType<F>>, args: Parameters<F>, props: P, actions: A, notifyWarning: NotifyFunc) => void;
97
- /**
98
- * Handle side effects when the request failed.
99
- * @param error The error of the request
100
- * @param args The argument during the call of the request function
101
- * @param props The props passed to the state decorator
102
- * @param actions The actions available
103
- * @param notifyWarning If specified, a notify function to indicate that the action is a semi failure (use errorMessage / getErrorMessage otherwise).
104
- */
105
- onFail?: (state: S, error: Error, args: Parameters<F>, props: P, actions: A, notifyWarning: NotifyFunc) => void;
106
- /**
107
- * Retrieve the state to set as current data before the promise is resolved.
108
- * If the there's no reducer, this data will be used after the promise is done.
109
- */
110
- preReducer?: (state: S, args: Parameters<F>, props: P) => S | null;
111
- /**
112
- * Retrieve the state to set as current data before the promise is resolved.
113
- * If the there's no reducer, this data will be used after the promise is done.
114
- */
115
- optimisticReducer?: (state: S, args: Parameters<F>, props: P) => S | null;
116
- /**
117
- * Policy to apply when a call to an asynchronous action is done but a previous call is still not resolved.
118
- */
119
- conflictPolicy?: ConflictPolicy;
120
- /**
121
- * Get an identifier for an action call. Used only when conflictPolicy is ConflictPolicy.PARALLEL.
122
- * This information will be available in loadingMap. loadingMap[actionName] will be an array of promise identifiers.
123
- */
124
- getPromiseId?: (...args: Parameters<F>) => string;
125
- /**
126
- * Number of retries in case of error (failed to fetch).
127
- * Do not use this if you are creating objects...
128
- * Default value is 0 (no retry).
129
- */
130
- retryCount?: number;
131
- /**
132
- * Seed of delay between each retry in milliseconds.
133
- * The applied delay is retryDelaySeed * retry count.
134
- * Default value is 1000 (1 second).
135
- */
136
- retryDelaySeed?: number;
137
- /**
138
- * Function to test if the error will trigger an action retry or will fail directly.
139
- */
140
- isTriggerRetryError?: (e: Error) => boolean;
141
- }
142
- export interface AsynchActionPromise<S, F extends (...args: any[]) => any, A, P> extends AsynchActionBase<S, F, A, P> {
143
- /**
144
- * The request, returns a promise
145
- */
146
- promise: PromiseProvider<S, F, A, P>;
147
- }
148
- export interface AsynchActionPromiseGet<S, F extends (...args: any[]) => any, A, P> extends AsynchActionBase<S, F, A, P> {
149
- /**
150
- * The request, returns a promise that is a GET request.
151
- * A shortcut to set:
152
- * - retryCount: 3
153
- * - conflictPolicy: ConflictPolicy.REUSE
154
- */
155
- promiseGet: PromiseProvider<S, F, A, P>;
156
- }
157
- export type AsynchAction<S, F extends (...args: any[]) => any, A, P> = AsynchActionPromise<S, F, A, P> | AsynchActionPromiseGet<S, F, A, P>;
158
- export type StateDecoratorAction<S, F extends (...args: any[]) => any, A, P> = AsynchAction<S, F, A, P> | SynchAction<S, F, P> | AdvancedSynchAction<S, F, A, P>;
159
- /**
160
- * S: The type of the state
161
- * A: The type of the actions to pass to the children (used to check keys only).
162
- */
163
- export type StateDecoratorActions<S, A extends DecoratedActions, P = {}> = {
164
- [Prop in keyof A]: StateDecoratorAction<S, A[Prop], A, P>;
165
- };
166
- type OnPropsChangeReducer<S, P> = (s: S, newProps: P, updatedIndices: number[]) => S;
167
- export type StateDecoratorOptions<S, A, P = {}> = {
168
- /**
169
- * The state decorator name. Use in debug traces to identify the useStateDecorator instance.
170
- */
171
- name?: string;
172
- /**
173
- * Show logs in the console in development mode.
174
- */
175
- logEnabled?: boolean;
176
- /**
177
- * List of action names that are marked as loading at initial time.
178
- * As a render is done before first actions can be trigerred, some actions can be marked as loading at
179
- * initial time.
180
- */
181
- initialActionsMarkedLoading?: (keyof A)[];
182
- /**
183
- * Get a list of values that will be use as reference values.
184
- * If they are different (shallow compare), onPropsChangeReducer then onPropsChange will be called.
185
- */
186
- getPropsRefValues?: (props: P) => any[];
187
- /**
188
- * Triggered when values of reference from props have changed. Allow to call actions after a prop change.
189
- */
190
- onPropsChange?: (s: S, newProps: any, actions: A, updatedIndices: number[]) => void;
191
- /**
192
- * Triggered when values of reference from props have changed. Allow to update state after a prop change.
193
- * <b>null</b> means no change.
194
- */
195
- onPropsChangeReducer?: OnPropsChangeReducer<S, P>;
196
- /**
197
- * The callback function called if an asynchronous function succeeded and a success messsage is defined.
198
- */
199
- notifySuccess?: NotifyFunc;
200
- /**
201
- * The callback function called if an asynchronous function fails and an error messsage is defined.
202
- */
203
- notifyError?: NotifyFunc;
204
- /**
205
- * The callback function injected in onDone and onFail of asynchronous functions to notify a semi success / failed request.
206
- */
207
- notifyWarning?: NotifyFunc;
208
- /**
209
- * Initial actions. They are executed outside of a side effect to trigger asynchronous actions.
210
- */
211
- onMount?: (actions: A, props: P, state: S) => void;
212
- };
213
- export {};
1
+ import { ConflictPolicy, DecoratedActions, NotifyFunc, PromiseResult } from './types';
2
+ export type PromiseProvider<S, F extends (...args: any[]) => any, A, P> = (args: Parameters<F>, state: S, props: P, actions: A, abortSignal: AbortSignal) => ReturnType<F> | null;
3
+ /**
4
+ * Simple form of a synchronous action.
5
+ */
6
+ export type SynchAction<S, F extends (...args: any[]) => any, P> = (state: S, args: Parameters<F>, props: P) => S | null;
7
+ /**
8
+ * Advanced synchronous action.
9
+ * Allows to set advanced properties to a synchronous action.
10
+ */
11
+ export type AdvancedSynchAction<S, F extends (...args: any[]) => any, A, P> = {
12
+ /**
13
+ * The action to execute.
14
+ */
15
+ action: (state: S, args: Parameters<F>, props: P) => S | null;
16
+ /**
17
+ * Debounces the side effects if this parameter is defined.
18
+ */
19
+ debounceSideEffectsTimeout?: number;
20
+ /**
21
+ * Debounces the action if this parameter is defined.
22
+ */
23
+ debounceTimeout?: number;
24
+ /**
25
+ * Action to call when the action is done. Used to trigger other actions (even asynchronous),
26
+ */
27
+ onActionDone?: (state: S, args: Parameters<F>, props: P, actions: A, notifyWarning: NotifyFunc) => void;
28
+ };
29
+ export type PromiseIdMap = {
30
+ [promiseId: string]: boolean;
31
+ };
32
+ export type InternalLoadingMap<A> = {
33
+ [name: string]: undefined | boolean | PromiseIdMap;
34
+ };
35
+ export type LoadingMap<A> = {
36
+ [P in keyof A]?: undefined | boolean;
37
+ };
38
+ export type LoadingMapParallelActions<A> = {
39
+ [P in keyof A]?: PromiseIdMap;
40
+ };
41
+ export type LoadingProps<A> = {
42
+ loading: boolean;
43
+ loadingMap: LoadingMap<A>;
44
+ loadingParallelMap: LoadingMapParallelActions<A>;
45
+ };
46
+ /**
47
+ * Try to abort an action, if the action is <code>abortable</code>.
48
+ * @param actionName The action name
49
+ * @param promiseId If the action conflict type is <code>ConflictType.PARALLEL</code>, the identifier of the promise.
50
+ * @returns <code>true</code> if the action is abortable and an action is ongoing, <code>false</code> otherwise.
51
+ */
52
+ export type AbortActionCallback<A> = (actionName: keyof A, promiseId?: string) => boolean;
53
+ export type GetErrorMessage<F extends (...args: any[]) => any, P> = (e: any, args: Parameters<F>, props: P) => string;
54
+ export interface AsynchActionBase<S, F extends (...args: any[]) => any, A, P> {
55
+ /**
56
+ * This action can be aborted. An abortSignal will be injected to the <code>promise</code> / <code>promiseGet</code>.
57
+ */
58
+ abortable?: boolean;
59
+ /**
60
+ * The success message to pass to the notifySuccess function passed as property to the StateDecorator.
61
+ */
62
+ successMessage?: string;
63
+ /**
64
+ * A function that provides the success message to pass to the notifySuccess function passed as property to the StateDecorator.
65
+ */
66
+ getSuccessMessage?: (result: PromiseResult<ReturnType<F>>, args: Parameters<F>, props: P) => string;
67
+ /**
68
+ * The error message to pass to the notifyError function passed as property to the StateDecorator.
69
+ */
70
+ errorMessage?: string;
71
+ /**
72
+ * A function that provides the error message to pass to the notifyError function passed as property to the StateDecorator.
73
+ */
74
+ getErrorMessage?: GetErrorMessage<F, P>;
75
+ /**
76
+ * If set, called with the result of the promise to update the current state.
77
+ */
78
+ reducer?: (state: S, result: PromiseResult<ReturnType<F>>, args: Parameters<F>, props: P) => S | null;
79
+ /**
80
+ * If set, called with the error of the promise to update the current state.
81
+ */
82
+ errorReducer?: (state: S, error: any, args: Parameters<F>, props: P) => S | null;
83
+ /**
84
+ * Whether reject the promise instead of handling it.
85
+ */
86
+ rejectPromiseOnError?: boolean;
87
+ /**
88
+ * Handle side effects when the request succeeded.
89
+ * @param result The result of the request
90
+ * @param newData The data after the reducer is applied
91
+ * @param args The argument during the call of the request function
92
+ * @param props The props passed to the state decorator
93
+ * @param actions The actions available
94
+ * @param notifyWarning If specified, a notify function to indicate that the action is a semi success (use successMessage / getSuccessMessage otherwise).
95
+ */
96
+ onDone?: (state: S, result: PromiseResult<ReturnType<F>>, args: Parameters<F>, props: P, actions: A, notifyWarning: NotifyFunc) => void;
97
+ /**
98
+ * Handle side effects when the request failed.
99
+ * @param error The error of the request
100
+ * @param args The argument during the call of the request function
101
+ * @param props The props passed to the state decorator
102
+ * @param actions The actions available
103
+ * @param notifyWarning If specified, a notify function to indicate that the action is a semi failure (use errorMessage / getErrorMessage otherwise).
104
+ */
105
+ onFail?: (state: S, error: Error, args: Parameters<F>, props: P, actions: A, notifyWarning: NotifyFunc) => void;
106
+ /**
107
+ * Retrieve the state to set as current data before the promise is resolved.
108
+ * If the there's no reducer, this data will be used after the promise is done.
109
+ */
110
+ preReducer?: (state: S, args: Parameters<F>, props: P) => S | null;
111
+ /**
112
+ * Retrieve the state to set as current data before the promise is resolved.
113
+ * If the there's no reducer, this data will be used after the promise is done.
114
+ */
115
+ optimisticReducer?: (state: S, args: Parameters<F>, props: P) => S | null;
116
+ /**
117
+ * Policy to apply when a call to an asynchronous action is done but a previous call is still not resolved.
118
+ */
119
+ conflictPolicy?: ConflictPolicy;
120
+ /**
121
+ * Get an identifier for an action call. Used only when conflictPolicy is ConflictPolicy.PARALLEL.
122
+ * This information will be available in loadingMap. loadingMap[actionName] will be an array of promise identifiers.
123
+ */
124
+ getPromiseId?: (...args: Parameters<F>) => string;
125
+ /**
126
+ * Number of retries in case of error (failed to fetch).
127
+ * Do not use this if you are creating objects...
128
+ * Default value is 0 (no retry).
129
+ */
130
+ retryCount?: number;
131
+ /**
132
+ * Seed of delay between each retry in milliseconds.
133
+ * The applied delay is retryDelaySeed * retry count.
134
+ * Default value is 1000 (1 second).
135
+ */
136
+ retryDelaySeed?: number;
137
+ /**
138
+ * Function to test if the error will trigger an action retry or will fail directly.
139
+ */
140
+ isTriggerRetryError?: (e: Error) => boolean;
141
+ }
142
+ export interface AsynchActionPromise<S, F extends (...args: any[]) => any, A, P> extends AsynchActionBase<S, F, A, P> {
143
+ /**
144
+ * The request, returns a promise
145
+ */
146
+ promise: PromiseProvider<S, F, A, P>;
147
+ }
148
+ export interface AsynchActionPromiseGet<S, F extends (...args: any[]) => any, A, P> extends AsynchActionBase<S, F, A, P> {
149
+ /**
150
+ * The request, returns a promise that is a GET request.
151
+ * A shortcut to set:
152
+ * - retryCount: 3
153
+ * - conflictPolicy: ConflictPolicy.REUSE
154
+ */
155
+ promiseGet: PromiseProvider<S, F, A, P>;
156
+ }
157
+ export type AsynchAction<S, F extends (...args: any[]) => any, A, P> = AsynchActionPromise<S, F, A, P> | AsynchActionPromiseGet<S, F, A, P>;
158
+ export type StateDecoratorAction<S, F extends (...args: any[]) => any, A, P> = AsynchAction<S, F, A, P> | SynchAction<S, F, P> | AdvancedSynchAction<S, F, A, P>;
159
+ /**
160
+ * S: The type of the state
161
+ * A: The type of the actions to pass to the children (used to check keys only).
162
+ */
163
+ export type StateDecoratorActions<S, A extends DecoratedActions, P = {}> = {
164
+ [Prop in keyof A]: StateDecoratorAction<S, A[Prop], A, P>;
165
+ };
166
+ type OnPropsChangeReducer<S, P> = (s: S, newProps: P, updatedIndices: number[]) => S;
167
+ export type StateDecoratorOptions<S, A, P = {}> = {
168
+ /**
169
+ * The state decorator name. Use in debug traces to identify the useStateDecorator instance.
170
+ */
171
+ name?: string;
172
+ /**
173
+ * Show logs in the console in development mode.
174
+ */
175
+ logEnabled?: boolean;
176
+ /**
177
+ * List of action names that are marked as loading at initial time.
178
+ * As a render is done before first actions can be trigerred, some actions can be marked as loading at
179
+ * initial time.
180
+ */
181
+ initialActionsMarkedLoading?: (keyof A)[];
182
+ /**
183
+ * Get a list of values that will be use as reference values.
184
+ * If they are different (shallow compare), onPropsChangeReducer then onPropsChange will be called.
185
+ */
186
+ getPropsRefValues?: (props: P) => any[];
187
+ /**
188
+ * Triggered when values of reference from props have changed. Allow to call actions after a prop change.
189
+ */
190
+ onPropsChange?: (s: S, newProps: any, actions: A, updatedIndices: number[]) => void;
191
+ /**
192
+ * Triggered when values of reference from props have changed. Allow to update state after a prop change.
193
+ * <b>null</b> means no change.
194
+ */
195
+ onPropsChangeReducer?: OnPropsChangeReducer<S, P>;
196
+ /**
197
+ * The callback function called if an asynchronous function succeeded and a success messsage is defined.
198
+ */
199
+ notifySuccess?: NotifyFunc;
200
+ /**
201
+ * The callback function called if an asynchronous function fails and an error messsage is defined.
202
+ */
203
+ notifyError?: NotifyFunc;
204
+ /**
205
+ * The callback function injected in onDone and onFail of asynchronous functions to notify a semi success / failed request.
206
+ */
207
+ notifyWarning?: NotifyFunc;
208
+ /**
209
+ * Initial actions. They are executed outside of a side effect to trigger asynchronous actions.
210
+ */
211
+ onMount?: (actions: A, props: P, state: S) => void;
212
+ };
213
+ export {};