state-decorator 7.0.0-beta.0 → 7.0.0-beta.10-dev

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/README.md +384 -192
  2. package/es/helpers.d.ts +66 -0
  3. package/es/helpers.js +163 -0
  4. package/{dist/v5_test.js.map → es/helpers.js.map} +1 -1
  5. package/es/impl.d.ts +19 -5
  6. package/es/impl.js +963 -1
  7. package/es/impl.js.map +1 -1
  8. package/es/index.d.ts +34 -36
  9. package/es/index.js +1013 -1
  10. package/es/index.js.map +1 -1
  11. package/es/middlewares.js +519 -1
  12. package/es/middlewares.js.map +1 -1
  13. package/es/test.d.ts +6 -4
  14. package/es/test.js +349 -1
  15. package/es/test.js.map +1 -1
  16. package/es/types.d.ts +32 -3
  17. package/es/v5.js +301 -1
  18. package/es/v5.js.map +1 -1
  19. package/es/v5_test.js +392 -1
  20. package/es/v5_test.js.map +1 -1
  21. package/es/v6.d.ts +18 -0
  22. package/es/v6.js +98 -0
  23. package/es/v6.js.map +1 -0
  24. package/helpers.d.ts +66 -0
  25. package/helpers.js +175 -0
  26. package/{dist/test.js.map → helpers.js.map} +1 -1
  27. package/impl.d.ts +19 -5
  28. package/impl.js +996 -1
  29. package/impl.js.map +1 -1
  30. package/index.d.ts +34 -36
  31. package/index.js +1044 -1
  32. package/index.js.map +1 -1
  33. package/middlewares.js +526 -1
  34. package/middlewares.js.map +1 -1
  35. package/package.json +5 -4
  36. package/test.d.ts +6 -4
  37. package/test.js +358 -1
  38. package/test.js.map +1 -1
  39. package/types.d.ts +32 -3
  40. package/v5.js +322 -1
  41. package/v5.js.map +1 -1
  42. package/v5_test.js +416 -1
  43. package/v5_test.js.map +1 -1
  44. package/v6.d.ts +18 -0
  45. package/v6.js +123 -0
  46. package/v6.js.map +1 -0
  47. package/dist/impl.d.ts +0 -195
  48. package/dist/impl.js +0 -2
  49. package/dist/impl.js.map +0 -1
  50. package/dist/index.d.ts +0 -168
  51. package/dist/index.js +0 -2
  52. package/dist/index.js.map +0 -1
  53. package/dist/middlewares.d.ts +0 -26
  54. package/dist/middlewares.js +0 -2
  55. package/dist/middlewares.js.map +0 -1
  56. package/dist/shallow.d.ts +0 -15
  57. package/dist/test.d.ts +0 -134
  58. package/dist/test.js +0 -2
  59. package/dist/types.d.ts +0 -352
  60. package/dist/v5.d.ts +0 -58
  61. package/dist/v5.js +0 -2
  62. package/dist/v5.js.map +0 -1
  63. package/dist/v5_test.d.ts +0 -70
  64. package/dist/v5_test.js +0 -2
  65. package/dist/v5_types.d.ts +0 -227
package/dist/v5_test.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var r=require("./index"),e=require("./impl"),n=function(){return(n=Object.assign||function(r){for(var e,n=1,t=arguments.length;n<t;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(r[o]=e[o]);return r}).apply(this,arguments)};function t(r,e,n){if(n||2===arguments.length)for(var t,o=0,s=e.length;o<s;o++)!t&&o in e||(t||(t=Array.prototype.slice.call(e,0,o)),t[o]=e[o]);return r.concat(t||Array.prototype.slice.call(e))}function o(r){return!(r instanceof Function)&&(r.hasOwnProperty("promise")||r.hasOwnProperty("promiseGet"))}function s(r){return r instanceof Function}function i(r){return!(r instanceof Function||r.hasOwnProperty("promise")||r.hasOwnProperty("promiseGet"))}Object.defineProperty(exports,"ConflictPolicy",{enumerable:!0,get:function(){return r.ConflictPolicy}}),Object.defineProperty(exports,"setGlobalConfig",{enumerable:!0,get:function(){return r.setGlobalConfig}}),exports.isAdvancedSyncAction=i,exports.isAsyncAction=o,exports.isAsyncPromiseGetAction=function(r){return!(r instanceof Function||r.hasOwnProperty("promise")&&r.hasOwnProperty("promiseGet"))},exports.isSyncAction=s,exports.testAdvancedSyncAction=function(r,e){return i(r)?e(r):Promise.reject(new Error("This action is not a synchronous advanced action"))},exports.testAsyncAction=function(e,t){return o(e)?t(function(e){return"promiseGet"in e?n(n({},e),{promise:e.promiseGet,retryCount:3,conflictPolicy:r.ConflictPolicy.REUSE}):e}(e)):Promise.reject(new Error("This action is not an asynchronous action"))},exports.testSyncAction=function(r,e){return s(r)?e(r):Promise.reject(new Error("This action is not a synchronous action"))},exports.testV6AdvancedSyncAction=function(r,n){return e.isSyncAction(r)?(n({action:function(e,n,t){return r.effects({s:e,args:n,p:t,ds:null,derived:null,state:e,props:t,res:null,result:null,promiseId:null})},onActionDone:function(e,n,t,o,s){r.sideEffects({s:e,args:n,p:t,a:o,notifyWarning:s,ds:null,derived:null,state:e,props:t,actions:o,res:null,result:null,promiseId:null})}}),Promise.resolve()):Promise.reject(new Error("This action is not a synchronous action"))},exports.testV6AsyncAction=function(r,n){return e.isAsyncAction(r)?n({abortable:r.abortable,rejectPromiseOnError:r.rejectPromiseOnError,retryCount:r.retryCount,retryDelaySeed:r.retryDelaySeed,isTriggerRetryError:r.isTriggerRetryError,conflictPolicy:r.conflictPolicy,getPromiseId:r.getPromiseId,promise:function(n,t,o,s,i){return e.isAsyncGetPromiseAction(r)?r.getPromise({s:t,args:n,p:o,a:s,abortSignal:i,ds:null,derived:null,state:t,props:o,actions:s,promiseId:null}):r.getGetPromise({s:t,args:n,p:o,a:s,abortSignal:i,ds:null,derived:null,state:t,props:o,actions:s,promiseId:null})},promiseGet:function(n,t,o,s,i){return e.isAsyncGetPromiseGetAction(r)?r.getGetPromise({s:t,args:n,p:o,a:s,abortSignal:i,ds:null,derived:null,state:t,props:o,actions:s,promiseId:null}):null},reducer:function(e,n,t,o){return r.effects?r.effects({s:e,args:t,p:o,res:n,ds:null,derived:null,state:e,props:o,result:n,promiseId:null}):null},preReducer:function(e,n,t){return r.preEffects?r.preEffects({s:e,args:n,p:t,ds:null,derived:null,state:e,props:t,promiseId:null}):null},optimisticReducer:function(e,n,o){var s;return r.optimisticEffects?r.optimisticEffects({s:e,args:n,p:o,ds:null,derived:null,state:e,props:o,promiseId:null===(s=r.getPromiseId)||void 0===s?void 0:s.call.apply(s,t([r],n))}):null},errorReducer:function(e,n,o,s){var i;return r.errorEffects?r.errorEffects({s:e,args:o,p:s,err:n,ds:null,derived:null,state:e,props:s,error:n,promiseId:null===(i=r.getPromiseId)||void 0===i?void 0:i.call.apply(i,t([r],o))}):null},onDone:function(e,n,o,s,i,l){var c,u;null===(c=r.sideEffects)||void 0===c||c.call(r,{s:e,args:o,p:s,a:i,res:n,notifyWarning:l,ds:null,derived:null,state:e,props:s,actions:i,result:n,promiseId:null===(u=r.getPromiseId)||void 0===u?void 0:u.call.apply(u,t([r],o))})},onFail:function(e,n,o,s,i,l){var c,u;null===(c=r.errorSideEffects)||void 0===c||c.call(r,{s:e,args:o,p:s,a:i,err:n,notifyWarning:l,ds:null,derived:null,state:e,props:s,actions:i,error:n,promiseId:null===(u=r.getPromiseId)||void 0===u?void 0:u.call.apply(u,t([r],o))})},getErrorMessage:function(e,n,o){var s,i;return null===(s=r.getErrorMessage)||void 0===s?void 0:s.call(r,{err:e,p:o,args:n,ds:null,derived:null,state:null,s:null,error:e,props:o,promiseId:null===(i=r.getPromiseId)||void 0===i?void 0:i.call.apply(i,t([r],n))})},getSuccessMessage:function(e,n,o){var s,i;return null===(s=r.getSuccessMessage)||void 0===s?void 0:s.call(r,{res:e,args:n,p:o,ds:null,derived:null,state:null,s:null,result:e,props:o,promiseId:null===(i=r.getPromiseId)||void 0===i?void 0:i.call.apply(i,t([r],n))})}}):Promise.reject(new Error("This action is not a synchronous action"))},exports.testV6SyncAction=function(r,n){if(e.isSimpleSyncAction(r)){return n((function(e,n,t){return r({s:e,args:n,p:t,ds:null,derived:null,state:e,props:t,promiseId:null})})),Promise.resolve()}return Promise.reject(new Error("This action is not a synchronous action"))};
2
- //# sourceMappingURL=v5_test.js.map
@@ -1,227 +0,0 @@
1
- /*! *****************************************************************************
2
- Copyright (c) GlobalSport SAS.
3
-
4
- Permission to use, copy, modify, and/or distribute this software for any
5
- purpose with or without fee is hereby granted.
6
-
7
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
- PERFORMANCE OF THIS SOFTWARE.
14
- ***************************************************************************** */
15
- import { ConflictPolicy, DecoratedActions, NotifyFunc, PromiseResult } from './types';
16
- export declare type PromiseProvider<S, F extends (...args: any[]) => any, A, P> = (args: Parameters<F>, state: S, props: P, actions: A, abortSignal: AbortSignal) => ReturnType<F> | null;
17
- /**
18
- * Simple form of a synchronous action.
19
- */
20
- export declare type SynchAction<S, F extends (...args: any[]) => any, P> = (state: S, args: Parameters<F>, props: P) => S | null;
21
- /**
22
- * Advanced synchronous action.
23
- * Allows to set advanced properties to a synchronous action.
24
- */
25
- export declare type AdvancedSynchAction<S, F extends (...args: any[]) => any, A, P> = {
26
- /**
27
- * The action to execute.
28
- */
29
- action: (state: S, args: Parameters<F>, props: P) => S | null;
30
- /**
31
- * Debounces the side effects if this parameter is defined.
32
- */
33
- debounceSideEffectsTimeout?: number;
34
- /**
35
- * Debounces the action if this parameter is defined.
36
- */
37
- debounceTimeout?: number;
38
- /**
39
- * Action to call when the action is done. Used to trigger other actions (even asynchronous),
40
- */
41
- onActionDone?: (state: S, args: Parameters<F>, props: P, actions: A, notifyWarning: NotifyFunc) => void;
42
- };
43
- export declare type PromiseIdMap = {
44
- [promiseId: string]: boolean;
45
- };
46
- export declare type InternalLoadingMap<A> = {
47
- [name: string]: undefined | boolean | PromiseIdMap;
48
- };
49
- export declare type LoadingMap<A> = {
50
- [P in keyof A]?: undefined | boolean;
51
- };
52
- export declare type LoadingMapParallelActions<A> = {
53
- [P in keyof A]?: PromiseIdMap;
54
- };
55
- export declare type LoadingProps<A> = {
56
- loading: boolean;
57
- loadingMap: LoadingMap<A>;
58
- loadingParallelMap: LoadingMapParallelActions<A>;
59
- };
60
- /**
61
- * Try to abort an action, if the action is <code>abortable</code>.
62
- * @param actionName The action name
63
- * @param promiseId If the action conflict type is <code>ConflictType.PARALLEL</code>, the identifier of the promise.
64
- * @returns <code>true</code> if the action is abortable and an action is ongoing, <code>false</code> otherwise.
65
- */
66
- export declare type AbortActionCallback<A> = (actionName: keyof A, promiseId?: string) => boolean;
67
- export declare type GetErrorMessage<F extends (...args: any[]) => any, P> = (e: any, args: Parameters<F>, props: P) => string;
68
- export interface AsynchActionBase<S, F extends (...args: any[]) => any, A, P> {
69
- /**
70
- * This action can be aborted. An abortSignal will be injected to the <code>promise</code> / <code>promiseGet</code>.
71
- */
72
- abortable?: boolean;
73
- /**
74
- * The success message to pass to the notifySuccess function passed as property to the StateDecorator.
75
- */
76
- successMessage?: string;
77
- /**
78
- * A function that provides the success message to pass to the notifySuccess function passed as property to the StateDecorator.
79
- */
80
- getSuccessMessage?: (result: PromiseResult<ReturnType<F>>, args: Parameters<F>, props: P) => string;
81
- /**
82
- * The error message to pass to the notifyError function passed as property to the StateDecorator.
83
- */
84
- errorMessage?: string;
85
- /**
86
- * A function that provides the error message to pass to the notifyError function passed as property to the StateDecorator.
87
- */
88
- getErrorMessage?: GetErrorMessage<F, P>;
89
- /**
90
- * If set, called with the result of the promise to update the current state.
91
- */
92
- reducer?: (state: S, result: PromiseResult<ReturnType<F>>, args: Parameters<F>, props: P) => S | null;
93
- /**
94
- * If set, called with the error of the promise to update the current state.
95
- */
96
- errorReducer?: (state: S, error: any, args: Parameters<F>, props: P) => S | null;
97
- /**
98
- * Whether reject the promise instead of handling it.
99
- */
100
- rejectPromiseOnError?: boolean;
101
- /**
102
- * Handle side effects when the request succeeded.
103
- * @param result The result of the request
104
- * @param newData The data after the reducer is applied
105
- * @param args The argument during the call of the request function
106
- * @param props The props passed to the state decorator
107
- * @param actions The actions available
108
- * @param notifyWarning If specified, a notify function to indicate that the action is a semi success (use successMessage / getSuccessMessage otherwise).
109
- */
110
- onDone?: (state: S, result: PromiseResult<ReturnType<F>>, args: Parameters<F>, props: P, actions: A, notifyWarning: NotifyFunc) => void;
111
- /**
112
- * Handle side effects when the request failed.
113
- * @param error The error of the request
114
- * @param args The argument during the call of the request function
115
- * @param props The props passed to the state decorator
116
- * @param actions The actions available
117
- * @param notifyWarning If specified, a notify function to indicate that the action is a semi failure (use errorMessage / getErrorMessage otherwise).
118
- */
119
- onFail?: (state: S, error: Error, args: Parameters<F>, props: P, actions: A, notifyWarning: NotifyFunc) => void;
120
- /**
121
- * Retrieve the state to set as current data before the promise is resolved.
122
- * If the there's no reducer, this data will be used after the promise is done.
123
- */
124
- preReducer?: (state: S, args: Parameters<F>, props: P) => S | null;
125
- /**
126
- * Retrieve the state to set as current data before the promise is resolved.
127
- * If the there's no reducer, this data will be used after the promise is done.
128
- */
129
- optimisticReducer?: (state: S, args: Parameters<F>, props: P) => S | null;
130
- /**
131
- * Policy to apply when a call to an asynchronous action is done but a previous call is still not resolved.
132
- */
133
- conflictPolicy?: ConflictPolicy;
134
- /**
135
- * Get an identifier for an action call. Used only when conflictPolicy is ConflictPolicy.PARALLEL.
136
- * This information will be available in loadingMap. loadingMap[actionName] will be an array of promise identifiers.
137
- */
138
- getPromiseId?: (...args: Parameters<F>) => string;
139
- /**
140
- * Number of retries in case of error (failed to fetch).
141
- * Do not use this if you are creating objects...
142
- * Default value is 0 (no retry).
143
- */
144
- retryCount?: number;
145
- /**
146
- * Seed of delay between each retry in milliseconds.
147
- * The applied delay is retryDelaySeed * retry count.
148
- * Default value is 1000 (1 second).
149
- */
150
- retryDelaySeed?: number;
151
- /**
152
- * Function to test if the error will trigger an action retry or will fail directly.
153
- */
154
- isTriggerRetryError?: (e: Error) => boolean;
155
- }
156
- export interface AsynchActionPromise<S, F extends (...args: any[]) => any, A, P> extends AsynchActionBase<S, F, A, P> {
157
- /**
158
- * The request, returns a promise
159
- */
160
- promise: PromiseProvider<S, F, A, P>;
161
- }
162
- export interface AsynchActionPromiseGet<S, F extends (...args: any[]) => any, A, P> extends AsynchActionBase<S, F, A, P> {
163
- /**
164
- * The request, returns a promise that is a GET request.
165
- * A shortcut to set:
166
- * - retryCount: 3
167
- * - conflictPolicy: ConflictPolicy.REUSE
168
- */
169
- promiseGet: PromiseProvider<S, F, A, P>;
170
- }
171
- export declare type AsynchAction<S, F extends (...args: any[]) => any, A, P> = AsynchActionPromise<S, F, A, P> | AsynchActionPromiseGet<S, F, A, P>;
172
- export declare type StateDecoratorAction<S, F extends (...args: any[]) => any, A, P> = AsynchAction<S, F, A, P> | SynchAction<S, F, P> | AdvancedSynchAction<S, F, A, P>;
173
- /**
174
- * S: The type of the state
175
- * A: The type of the actions to pass to the children (used to check keys only).
176
- */
177
- export declare type StateDecoratorActions<S, A extends DecoratedActions, P = {}> = {
178
- [Prop in keyof A]: StateDecoratorAction<S, A[Prop], A, P>;
179
- };
180
- declare type OnPropsChangeReducer<S, P> = (s: S, newProps: P, updatedIndices: number[]) => S;
181
- export declare type StateDecoratorOptions<S, A, P = {}> = {
182
- /**
183
- * The state decorator name. Use in debug traces to identify the useStateDecorator instance.
184
- */
185
- name?: string;
186
- /**
187
- * Show logs in the console in development mode.
188
- */
189
- logEnabled?: boolean;
190
- /**
191
- * List of action names that are marked as loading at initial time.
192
- * As a render is done before first actions can be trigerred, some actions can be marked as loading at
193
- * initial time.
194
- */
195
- initialActionsMarkedLoading?: (keyof A)[];
196
- /**
197
- * Get a list of values that will be use as reference values.
198
- * If they are different (shallow compare), onPropsChangeReducer then onPropsChange will be called.
199
- */
200
- getPropsRefValues?: (props: P) => any[];
201
- /**
202
- * Triggered when values of reference from props have changed. Allow to call actions after a prop change.
203
- */
204
- onPropsChange?: (s: S, newProps: any, actions: A, updatedIndices: number[]) => void;
205
- /**
206
- * Triggered when values of reference from props have changed. Allow to update state after a prop change.
207
- * <b>null</b> means no change.
208
- */
209
- onPropsChangeReducer?: OnPropsChangeReducer<S, P>;
210
- /**
211
- * The callback function called if an asynchronous function succeeded and a success messsage is defined.
212
- */
213
- notifySuccess?: NotifyFunc;
214
- /**
215
- * The callback function called if an asynchronous function fails and an error messsage is defined.
216
- */
217
- notifyError?: NotifyFunc;
218
- /**
219
- * The callback function injected in onDone and onFail of asynchronous functions to notify a semi success / failed request.
220
- */
221
- notifyWarning?: NotifyFunc;
222
- /**
223
- * Initial actions. They are executed outside of a side effect to trigger asynchronous actions.
224
- */
225
- onMount?: (actions: A, props: P, state: S) => void;
226
- };
227
- export {};