state-decorator 7.0.0-beta.9-dev → 7.0.0-rc1

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 (56) hide show
  1. package/README.md +42 -11
  2. package/development.d.ts +24 -0
  3. package/development.js +2 -0
  4. package/development.js.map +1 -0
  5. package/es/development.d.ts +24 -0
  6. package/es/development.js +2 -0
  7. package/es/development.js.map +1 -0
  8. package/es/graph.d.ts +8 -0
  9. package/es/helpers.js +1 -162
  10. package/es/helpers.js.map +1 -1
  11. package/es/impl.d.ts +20 -18
  12. package/es/impl.js +1 -963
  13. package/es/impl.js.map +1 -1
  14. package/es/index.d.ts +6 -9
  15. package/es/index.js +1 -1013
  16. package/es/index.js.map +1 -1
  17. package/es/middlewares.d.ts +0 -9
  18. package/es/middlewares.js +1 -519
  19. package/es/middlewares.js.map +1 -1
  20. package/es/test.d.ts +7 -7
  21. package/es/test.js +1 -349
  22. package/es/test.js.map +1 -1
  23. package/es/types.d.ts +70 -58
  24. package/es/v5.js +1 -301
  25. package/es/v5.js.map +1 -1
  26. package/es/v5_test.js +1 -392
  27. package/es/v5_test.js.map +1 -1
  28. package/es/v5_types.d.ts +15 -15
  29. package/es/v6.d.ts +5 -2
  30. package/es/v6.js +1 -96
  31. package/es/v6.js.map +1 -1
  32. package/graph.d.ts +8 -0
  33. package/helpers.js +1 -174
  34. package/helpers.js.map +1 -1
  35. package/impl.d.ts +20 -18
  36. package/impl.js +1 -996
  37. package/impl.js.map +1 -1
  38. package/index.d.ts +6 -9
  39. package/index.js +1 -1044
  40. package/index.js.map +1 -1
  41. package/middlewares.d.ts +0 -9
  42. package/middlewares.js +1 -526
  43. package/middlewares.js.map +1 -1
  44. package/package.json +1 -1
  45. package/test.d.ts +7 -7
  46. package/test.js +1 -358
  47. package/test.js.map +1 -1
  48. package/types.d.ts +70 -58
  49. package/v5.js +1 -322
  50. package/v5.js.map +1 -1
  51. package/v5_test.js +1 -416
  52. package/v5_test.js.map +1 -1
  53. package/v5_types.d.ts +15 -15
  54. package/v6.d.ts +5 -2
  55. package/v6.js +1 -120
  56. package/v6.js.map +1 -1
package/es/types.d.ts CHANGED
@@ -12,34 +12,34 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
12
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
13
  PERFORMANCE OF THIS SOFTWARE.
14
14
  ***************************************************************************** */
15
- export declare type PromiseResult<Type> = Type extends Promise<infer X> ? X : null;
16
- export declare type NotifyFunc = (msg: string) => void;
17
- export declare type PromiseIdMap = {
15
+ export type PromiseResult<Type> = Type extends Promise<infer X> ? X : null;
16
+ export type NotifyFunc = (msg: string) => void;
17
+ export type PromiseIdMap = {
18
18
  [promiseId: string]: boolean;
19
19
  };
20
- export declare type LoadingMap<A> = {
20
+ export type LoadingMap<A> = {
21
21
  [pId in keyof A]?: boolean;
22
22
  };
23
- export declare type LoadingParallelMap<A> = {
23
+ export type LoadingParallelMap<A> = {
24
24
  [P in keyof A]?: PromiseIdMap;
25
25
  };
26
- export declare type InternalLoadingMap<A> = LoadingParallelMap<A>;
27
- export declare type LoadingProps<A> = {
26
+ export type InternalLoadingMap<A> = LoadingParallelMap<A>;
27
+ export type LoadingProps<A> = {
28
28
  loading: boolean;
29
29
  loadingMap: LoadingMap<A>;
30
30
  loadingParallelMap: LoadingParallelMap<A>;
31
31
  };
32
- export declare type LoadingMapProps<A> = Pick<LoadingProps<A>, 'loadingMap'>;
33
- export declare type PromiseIdErrorMap = {
32
+ export type LoadingMapProps<A> = Pick<LoadingProps<A>, 'loadingMap'>;
33
+ export type PromiseIdErrorMap = {
34
34
  [promiseId: string]: Error;
35
35
  };
36
- export declare type ErrorMap<A> = {
36
+ export type ErrorMap<A> = {
37
37
  [pId in keyof A]?: Error;
38
38
  };
39
- export declare type ErrorParallelMap<A> = {
39
+ export type ErrorParallelMap<A> = {
40
40
  [P in keyof A]?: PromiseIdErrorMap;
41
41
  };
42
- export declare type ErrorProps<A> = {
42
+ export type ErrorProps<A> = {
43
43
  errorMap: ErrorMap<A>;
44
44
  };
45
45
  export interface DecoratedActions {
@@ -76,12 +76,12 @@ export declare enum ConflictPolicy {
76
76
  */
77
77
  REUSE = "reuse"
78
78
  }
79
- export declare type PromiseProvider<S, DS, F extends (...args: any[]) => any, A, P> = (ctx: GetPromiseInvocationContext<S, DS, F, P, A>) => ReturnType<F> | null;
79
+ export type PromiseProvider<S, DS, F extends (...args: any[]) => any, A, P> = (ctx: GetPromiseInvocationContext<S, DS, F, P, A>) => ReturnType<F> | null;
80
80
  /**
81
81
  * Simple form of a synchronous action.
82
82
  */
83
- export declare type SimpleSyncAction<S, F extends (...args: any[]) => any, P, DS, FxRes = Partial<S>> = (ctx: InvocationContext<S, DS, F, P>) => FxRes | null;
84
- export declare type SyncAction<S, F extends (...args: any[]) => any, A, P, DS, FxRes = Partial<S>> = {
83
+ export type SimpleSyncAction<S, F extends (...args: any[]) => any, P, DS, FxRes = Partial<S>> = (ctx: InvocationContext<S, DS, F, P>) => FxRes | null;
84
+ export type SyncAction<S, F extends (...args: any[]) => any, A, P, DS, FxRes = Partial<S>> = {
85
85
  /**
86
86
  * Function that update the state when the action is called.
87
87
  */
@@ -105,75 +105,75 @@ export declare type SyncAction<S, F extends (...args: any[]) => any, A, P, DS, F
105
105
  * @param promiseId If the action conflict type is <code>ConflictType.PARALLEL</code>, the identifier of the promise.
106
106
  * @returns <code>true</code> if the action is abortable and an action is ongoing, <code>false</code> otherwise.
107
107
  */
108
- export declare type AbortActionCallback<A> = (actionName: keyof A, promiseId?: string) => boolean;
109
- export declare type GetErrorMessage<S, DS, F extends (...args: any[]) => any, P> = (ctx: ErrorMessageInvocationContext<S, DS, F, P>) => string | null;
110
- export declare type ContextState<S> = {
108
+ export type AbortActionCallback<A> = (actionName: keyof A, promiseId?: string) => boolean;
109
+ export type GetErrorMessage<S, DS, F extends (...args: any[]) => any, P> = (ctx: ErrorMessageInvocationContext<S, DS, F, P>) => string | null;
110
+ export type ContextState<S> = {
111
111
  state: S;
112
112
  s: S;
113
113
  };
114
- export declare type ContextDerivedStateState<DS> = {
114
+ export type ContextDerivedStateState<DS> = {
115
115
  derived: DS;
116
116
  ds: DS;
117
117
  };
118
- export declare type ContextBase<S, P> = ContextState<S> & {
118
+ export type ContextBase<S, P> = ContextState<S> & {
119
119
  props: P;
120
120
  p: P;
121
121
  };
122
- export declare type ContextWithDerived<S, DS, P> = ContextBase<S, P> & ContextDerivedStateState<DS>;
123
- export declare type InvocationContextActions<A> = {
122
+ export type ContextWithDerived<S, DS, P> = ContextBase<S, P> & ContextDerivedStateState<DS>;
123
+ export type InvocationContextActions<A> = {
124
124
  actions: A;
125
125
  a: A;
126
126
  };
127
- export declare type WarningNotifyFunc = {
127
+ export type WarningNotifyFunc = {
128
128
  notifyWarning?: NotifyFunc;
129
129
  };
130
- export declare type InvocationContext<S, DS, F extends (...args: any[]) => any, P> = ContextWithDerived<S, DS, P> & {
130
+ export type InvocationContext<S, DS, F extends (...args: any[]) => any, P> = ContextWithDerived<S, DS, P> & {
131
131
  args: Parameters<F>;
132
132
  promiseId: string;
133
133
  };
134
- export declare type EffectsInvocationContext<S, DS, F extends (...args: any[]) => any, P> = InvocationContext<S, DS, F, P> & {
134
+ export type EffectsInvocationContext<S, DS, F extends (...args: any[]) => any, P> = InvocationContext<S, DS, F, P> & {
135
135
  result: PromiseResult<ReturnType<F>>;
136
136
  res: PromiseResult<ReturnType<F>>;
137
137
  };
138
- export declare type GetPromiseInvocationContext<S, DS, F extends (...args: any[]) => any, P, A> = InvocationContextActions<A> & InvocationContext<S, DS, F, P> & {
138
+ export type GetPromiseInvocationContext<S, DS, F extends (...args: any[]) => any, P, A> = InvocationContextActions<A> & InvocationContext<S, DS, F, P> & {
139
139
  abortSignal: AbortSignal;
140
140
  };
141
- export declare type ErrorEffectsInvocationContext<S, DS, F extends (...args: any[]) => any, P> = InvocationContext<S, DS, F, P> & {
141
+ export type ErrorEffectsInvocationContext<S, DS, F extends (...args: any[]) => any, P> = InvocationContext<S, DS, F, P> & {
142
142
  error: Error;
143
143
  err: Error;
144
144
  };
145
- export declare type SuccessMessageInvocationContext<S, DS, F extends (...args: any[]) => any, P> = EffectsInvocationContext<S, DS, F, P>;
146
- export declare type ErrorMessageInvocationContext<S, DS, F extends (...args: any[]) => any, P> = ErrorEffectsInvocationContext<S, DS, F, P>;
147
- export declare type ClearErrorFunc<A> = (actionName: keyof A, promiseId?: string) => void;
148
- export declare type ClearError<A> = {
145
+ export type SuccessMessageInvocationContext<S, DS, F extends (...args: any[]) => any, P> = EffectsInvocationContext<S, DS, F, P>;
146
+ export type ErrorMessageInvocationContext<S, DS, F extends (...args: any[]) => any, P> = ErrorEffectsInvocationContext<S, DS, F, P>;
147
+ export type ClearErrorFunc<A> = (actionName: keyof A, promiseId?: string) => void;
148
+ export type ClearError<A> = {
149
149
  clearError: ClearErrorFunc<A>;
150
150
  };
151
- export declare type SideEffectsInvocationContext<S, DS, F extends (...args: any[]) => any, P, A> = EffectsInvocationContext<S, DS, F, P> & InvocationContextActions<A> & WarningNotifyFunc & ClearError<A>;
152
- export declare type ErrorSideEffectsInvocationContext<S, DS, F extends (...args: any[]) => any, P, A> = ErrorEffectsInvocationContext<S, DS, F, P> & InvocationContextActions<A> & WarningNotifyFunc;
153
- export declare type OnMountInvocationContext<S, A, P> = ContextBase<S, P> & InvocationContextActions<A>;
154
- export declare type AbortedActions<A> = Partial<Record<keyof A, string[]>>;
155
- export declare type OnUnMountInvocationContext<S, A, P> = ContextBase<S, P> & {
151
+ export type SideEffectsInvocationContext<S, DS, F extends (...args: any[]) => any, P, A> = EffectsInvocationContext<S, DS, F, P> & InvocationContextActions<A> & WarningNotifyFunc & ClearError<A>;
152
+ export type ErrorSideEffectsInvocationContext<S, DS, F extends (...args: any[]) => any, P, A> = ErrorEffectsInvocationContext<S, DS, F, P> & InvocationContextActions<A> & WarningNotifyFunc;
153
+ export type OnMountInvocationContext<S, A, P> = ContextBase<S, P> & InvocationContextActions<A>;
154
+ export type AbortedActions<A> = Partial<Record<keyof A, string[]>>;
155
+ export type OnUnMountInvocationContext<S, A, P> = ContextBase<S, P> & {
156
156
  abortedActions: AbortedActions<A>;
157
157
  };
158
- export declare type EffectsType = 'preEffects' | 'effects' | 'errorEffects' | 'optimisticEffects' | null;
159
- export declare type MiddlewareActionContext<S, A, P> = {
158
+ export type EffectsType = 'preEffects' | 'effects' | 'errorEffects' | 'optimisticEffects' | null;
159
+ export type MiddlewareActionContext<S, A, P> = {
160
160
  name: keyof A;
161
161
  type: EffectsType;
162
162
  isAsync: boolean;
163
163
  context: InvocationContext<S, any, any, P>;
164
164
  };
165
- export declare type MiddlewareStoreContext<S, A extends DecoratedActions, P> = {
165
+ export type MiddlewareStoreContext<S, A extends DecoratedActions, P> = {
166
166
  actions: StoreActions<S, A, P, any, any>;
167
167
  state: S;
168
168
  setState: (s: S) => void;
169
169
  options: StoreOptions<S, A, P, any>;
170
170
  };
171
- export declare type MiddlewareFactory<S, A extends DecoratedActions, P> = () => Middleware<S, A, P>;
171
+ export type MiddlewareFactory<S, A extends DecoratedActions, P> = () => Middleware<S, A, P>;
172
172
  /**
173
173
  * A state change middleware.
174
174
  * Allow to listen and change a state change on the fly.
175
175
  */
176
- export declare type Middleware<S, A extends DecoratedActions, P> = {
176
+ export type Middleware<S, A extends DecoratedActions, P> = {
177
177
  /**
178
178
  * Initializes the middleware. Called when the store is initializing.
179
179
  */
@@ -190,7 +190,7 @@ export declare type Middleware<S, A extends DecoratedActions, P> = {
190
190
  */
191
191
  destroy: () => void;
192
192
  };
193
- export declare type AsyncActionBase<S, F extends (...args: any[]) => any, A, P, DS, FxRes = Partial<S>> = {
193
+ export type AsyncActionBase<S, F extends (...args: any[]) => any, A, P, DS, FxRes = Partial<S>> = {
194
194
  /**
195
195
  * This action can be aborted. An abortSignal will be injected to the <code>promise</code> / <code>promiseGet</code>.
196
196
  */
@@ -276,13 +276,13 @@ export declare type AsyncActionBase<S, F extends (...args: any[]) => any, A, P,
276
276
  */
277
277
  isTriggerRetryError?: (e: Error) => boolean;
278
278
  };
279
- export declare type AsyncActionPromise<S, F extends (...args: any[]) => any, A, P, DS = {}, FxRes = Partial<S>> = AsyncActionBase<S, F, A, P, DS, FxRes> & {
279
+ export type AsyncActionPromise<S, F extends (...args: any[]) => any, A, P, DS = {}, FxRes = Partial<S>> = AsyncActionBase<S, F, A, P, DS, FxRes> & {
280
280
  /**
281
281
  * The request, returns a promise
282
282
  */
283
283
  getPromise: PromiseProvider<S, DS, F, A, P>;
284
284
  };
285
- export declare type AsyncActionPromiseGet<S, F extends (...args: any[]) => any, A, P, DS, FxRes = Partial<S>> = AsyncActionBase<S, F, A, P, DS, FxRes> & {
285
+ export type AsyncActionPromiseGet<S, F extends (...args: any[]) => any, A, P, DS, FxRes = Partial<S>> = AsyncActionBase<S, F, A, P, DS, FxRes> & {
286
286
  /**
287
287
  * The request, returns a promise that is a GET request.
288
288
  * A shortcut to set:
@@ -291,43 +291,48 @@ export declare type AsyncActionPromiseGet<S, F extends (...args: any[]) => any,
291
291
  */
292
292
  getGetPromise: PromiseProvider<S, DS, F, A, P>;
293
293
  };
294
- export declare type AsyncAction<S, F extends (...args: any[]) => any, A, P, DS = {}, FxRes = Partial<S>> = AsyncActionPromise<S, F, A, P, DS, FxRes> | AsyncActionPromiseGet<S, F, A, P, DS, FxRes>;
295
- export declare type StoreAction<S, F extends (...args: any[]) => any, A, P, DS = {}, FxRes = Partial<S>> = AsyncAction<S, F, A, P, DS, FxRes> | SimpleSyncAction<S, F, P, DS, FxRes> | SyncAction<S, F, A, P, DS, FxRes>;
294
+ export type AsyncAction<S, F extends (...args: any[]) => any, A, P, DS = {}, FxRes = Partial<S>> = AsyncActionPromise<S, F, A, P, DS, FxRes> | AsyncActionPromiseGet<S, F, A, P, DS, FxRes>;
295
+ export type StoreAction<S, F extends (...args: any[]) => any, A, P, DS = {}, FxRes = Partial<S>> = AsyncAction<S, F, A, P, DS, FxRes> | SimpleSyncAction<S, F, P, DS, FxRes> | SyncAction<S, F, A, P, DS, FxRes>;
296
296
  /**
297
297
  * S: The type of the state
298
298
  * A: The type of the actions to pass to the children (used to check keys only).
299
299
  */
300
- export declare type StoreActions<S, A extends DecoratedActions, P = {}, DS = {}, FxRes = Partial<S>> = {
300
+ export type StoreActions<S, A extends DecoratedActions, P = {}, DS = {}, FxRes = Partial<S>> = {
301
301
  [Prop in keyof A]: StoreAction<S, A[Prop], A, P, DS, FxRes>;
302
302
  };
303
- export declare type OnPropsChangeEffectsContext<S, DS, P> = ContextWithDerived<S, DS, P> & {
303
+ export type OnPropsChangeEffectsContext<S, DS, P> = ContextWithDerived<S, DS, P> & {
304
304
  indices: number[];
305
305
  index: number;
306
306
  isInit: boolean;
307
307
  getInitialState: (p: P) => S;
308
308
  };
309
- export declare type OnPropsChangeSideEffectsContext<S, P, A, DS = {}> = OnPropsChangeEffectsContext<S, DS, P> & {
309
+ export type OnPropsChangeSideEffectsContext<S, P, A, DS = {}> = OnPropsChangeEffectsContext<S, DS, P> & {
310
310
  actions: A;
311
311
  a: A;
312
312
  };
313
- declare type DerivedStateOption<S, P, DS> = {
313
+ type DerivedStateOption<S, P, DS> = {
314
314
  [k in keyof DS]: {
315
- getDeps: (ctx: ContextBase<S, P>) => any[];
316
- get: (ctx: ContextBase<S, P>) => DS[k];
315
+ getDeps?: (ctx: ContextBase<S, P>) => any[];
316
+ get: (ctx: ContextBase<S, P> & ContextDerivedStateState<DS>) => DS[k];
317
+ derivedDeps?: (keyof DS)[];
317
318
  };
318
319
  };
319
- export declare type OnPropsChangeOptions<S, DS, A, P> = {
320
+ export type OnPropsChangeOptions<S, DS, A, P, FxRes = Partial<S>> = {
320
321
  onMount?: boolean;
321
322
  onMountDeferred?: boolean;
322
323
  getDeps: (p: P) => any[];
323
- effects?: (ctx: OnPropsChangeEffectsContext<S, DS, P>) => Partial<S>;
324
+ effects?: (ctx: OnPropsChangeEffectsContext<S, DS, P>) => FxRes;
324
325
  sideEffects?: (ctx: OnPropsChangeSideEffectsContext<S, P, A>) => void;
325
326
  };
326
- export declare type StoreOptions<S, A, P = {}, DS = {}> = {
327
+ export type StoreOptions<S, A, P = {}, DS = {}, FxRes = Partial<S>> = {
327
328
  /**
328
329
  * The state decorator name. Use in debug traces to identify the useStateDecorator instance.
329
330
  */
330
331
  name?: string;
332
+ /**
333
+ * In development only (NODE_ENV="development"), activates state changes traces on the console.
334
+ */
335
+ logEnabled?: boolean;
331
336
  /**
332
337
  * Indicates that all asynchronous actions errors are managed externally.
333
338
  * Same as setting isErrorManaged to true for all actions.
@@ -342,7 +347,7 @@ export declare type StoreOptions<S, A, P = {}, DS = {}> = {
342
347
  /**
343
348
  * One or several configurations of inbound properties change managements.
344
349
  */
345
- onPropsChange?: OnPropsChangeOptions<S, DS, A, P> | OnPropsChangeOptions<S, DS, A, P>[];
350
+ onPropsChange?: OnPropsChangeOptions<S, DS, A, P, FxRes> | OnPropsChangeOptions<S, DS, A, P, FxRes>[];
346
351
  /**
347
352
  * The callback function called if an asynchronous function succeeded and a success messsage is defined.
348
353
  */
@@ -372,10 +377,17 @@ export declare type StoreOptions<S, A, P = {}, DS = {}> = {
372
377
  * Compute derived state
373
378
  */
374
379
  derivedState?: DerivedStateOption<S, P, DS>;
380
+ fullStateEffects?: boolean;
375
381
  };
376
- export declare type StoreConfig<S, A extends DecoratedActions, P = {}, DS = {}> = {
377
- getInitialState: S | ((p: P) => S);
382
+ type StoreConfigBase<S, A extends DecoratedActions, P = {}, DS = {}> = {
378
383
  actions: StoreActions<S, A, P, DS>;
379
384
  middlewares?: MiddlewareFactory<S, A, P>[];
380
385
  } & StoreOptions<S, A, P, DS>;
386
+ type StoreConfigObj<S, A extends DecoratedActions, P = {}, DS = {}> = StoreConfigBase<S, A, P, DS> & {
387
+ initialState: S;
388
+ };
389
+ type StoreConfigFunc<S, A extends DecoratedActions, P = {}, DS = {}> = StoreConfigBase<S, A, P, DS> & {
390
+ getInitialState: (p: P) => S;
391
+ };
392
+ export type StoreConfig<S, A extends DecoratedActions, P = {}, DS = {}> = StoreConfigObj<S, A, P, DS> | StoreConfigFunc<S, A, P, DS>;
381
393
  export {};
package/es/v5.js CHANGED
@@ -1,302 +1,2 @@
1
- import { ConflictPolicy, createStore } from './index';
2
- export { ConflictPolicy, setGlobalConfig } from './index';
3
- import { useReducer, useRef, useLayoutEffect, useEffect } from 'react';
4
- import { logDetailedEffects } from './middlewares';
5
-
6
- /*! *****************************************************************************
7
- Copyright (c) Microsoft Corporation.
8
-
9
- Permission to use, copy, modify, and/or distribute this software for any
10
- purpose with or without fee is hereby granted.
11
-
12
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
13
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
14
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
15
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
16
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
17
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18
- PERFORMANCE OF THIS SOFTWARE.
19
- ***************************************************************************** */
20
-
21
- var __assign = function() {
22
- __assign = Object.assign || function __assign(t) {
23
- for (var s, i = 1, n = arguments.length; i < n; i++) {
24
- s = arguments[i];
25
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
26
- }
27
- return t;
28
- };
29
- return __assign.apply(this, arguments);
30
- };
31
-
32
- /*! *****************************************************************************
33
- Copyright (c) GlobalSport SAS.
34
-
35
- Permission to use, copy, modify, and/or distribute this software for any
36
- purpose with or without fee is hereby granted.
37
-
38
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
39
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
40
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
41
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
42
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
43
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
44
- PERFORMANCE OF THIS SOFTWARE.
45
- ***************************************************************************** */
46
- function isAsyncActionPromise(action) {
47
- return !(action instanceof Function) && action.hasOwnProperty('promise');
48
- }
49
- function isAsyncActionPromiseGet(action) {
50
- return !(action instanceof Function) && action.hasOwnProperty('promiseGet');
51
- }
52
- /**
53
- * Type guard function to test if an action is a asynchronous action.
54
- */
55
- function isAsyncAction(action) {
56
- return !(action instanceof Function) && (action.hasOwnProperty('promise') || action.hasOwnProperty('promiseGet'));
57
- }
58
- /**
59
- * Type guard function to test if an action is a asynchronous action.
60
- */
61
- function isAsyncPromiseGetAction(action) {
62
- return !(action instanceof Function) && !(action.hasOwnProperty('promise') && action.hasOwnProperty('promiseGet'));
63
- }
64
- /**
65
- * Type guard function to test if an action is a synchronous action.
66
- */
67
- function isSyncAction(action) {
68
- return action instanceof Function;
69
- }
70
- /**
71
- * Type guard function to test if an action is a synchronous action.
72
- */
73
- function isAdvancedSyncAction(action) {
74
- return !(action instanceof Function) && !action.hasOwnProperty('promise') && !action.hasOwnProperty('promiseGet');
75
- }
76
- function convertV5Actions(sourceActions) {
77
- var actionNames = Object.keys(sourceActions);
78
- return actionNames.reduce(function (acc, actionName) {
79
- var sourceAction = sourceActions[actionName];
80
- if (isSyncAction(sourceAction)) {
81
- acc[actionName] = function (ctx) {
82
- return sourceAction(ctx.s, ctx.args, ctx.p);
83
- };
84
- }
85
- else if (isAdvancedSyncAction(sourceAction)) {
86
- var action = {
87
- effects: function (ctx) {
88
- return sourceAction.action(ctx.s, ctx.args, ctx.p);
89
- },
90
- };
91
- if (sourceAction.onActionDone) {
92
- action.sideEffects = function (_a) {
93
- var s = _a.s, p = _a.p, args = _a.args, a = _a.a, notifyWarning = _a.notifyWarning;
94
- sourceAction.onActionDone(s, args, p, a, notifyWarning);
95
- };
96
- }
97
- acc[actionName] = action;
98
- }
99
- else if (isAsyncActionPromise(sourceAction) || isAsyncActionPromiseGet(sourceAction)) {
100
- var asyncAction_1;
101
- if (isAsyncActionPromiseGet(sourceAction)) {
102
- asyncAction_1 = {
103
- getPromise: function (_a) {
104
- var s = _a.s, p = _a.p, a = _a.a, args = _a.args, abortSignal = _a.abortSignal;
105
- return sourceAction.promiseGet(args, s, p, a, abortSignal);
106
- },
107
- retryCount: 3,
108
- conflictPolicy: ConflictPolicy.REUSE,
109
- };
110
- }
111
- else {
112
- asyncAction_1 = {
113
- getPromise: function (_a) {
114
- var s = _a.s, p = _a.p, a = _a.a, args = _a.args, abortSignal = _a.abortSignal;
115
- return sourceAction.promise(args, s, p, a, abortSignal);
116
- },
117
- };
118
- }
119
- [
120
- 'abortable',
121
- 'conflictPolicy',
122
- 'getPromiseId',
123
- 'isTriggerRetryError',
124
- 'rejectPromiseOnError',
125
- 'retryCount',
126
- 'debounceTimeout',
127
- 'debounceSideEffectsTimeout',
128
- 'retryDelaySeed',
129
- ].forEach(function (prop) {
130
- if (sourceAction[prop] != null) {
131
- asyncAction_1[prop] = sourceAction[prop];
132
- }
133
- });
134
- if (sourceAction.successMessage) {
135
- asyncAction_1.getSuccessMessage = function () { return sourceAction.successMessage; };
136
- }
137
- if (sourceAction.errorMessage) {
138
- asyncAction_1.getSuccessMessage = function () { return sourceAction.errorMessage; };
139
- }
140
- if (sourceAction.getSuccessMessage) {
141
- asyncAction_1.getSuccessMessage = function (_a) {
142
- var res = _a.res, args = _a.args, p = _a.p;
143
- return sourceAction.getSuccessMessage(res, args, p);
144
- };
145
- }
146
- if (sourceAction.getErrorMessage) {
147
- asyncAction_1.getErrorMessage = function (_a) {
148
- var err = _a.err, args = _a.args, p = _a.p;
149
- return sourceAction.getErrorMessage(err, args, p);
150
- };
151
- }
152
- if (sourceAction.preReducer) {
153
- asyncAction_1.preEffects = function (_a) {
154
- var s = _a.s, p = _a.p, args = _a.args;
155
- return sourceAction.preReducer(s, args, p);
156
- };
157
- }
158
- if (sourceAction.optimisticReducer) {
159
- asyncAction_1.optimisticEffects = function (_a) {
160
- var s = _a.s, p = _a.p, args = _a.args;
161
- return sourceAction.optimisticReducer(s, args, p);
162
- };
163
- }
164
- if (sourceAction.reducer) {
165
- asyncAction_1.effects = function (_a) {
166
- var s = _a.s, res = _a.res, p = _a.p, args = _a.args;
167
- return sourceAction.reducer(s, res, args, p);
168
- };
169
- }
170
- if (sourceAction.errorReducer) {
171
- asyncAction_1.errorEffects = function (_a) {
172
- var s = _a.s, err = _a.err, p = _a.p, args = _a.args;
173
- return sourceAction.errorReducer(s, err, args, p);
174
- };
175
- }
176
- if (sourceAction.onDone) {
177
- asyncAction_1.sideEffects = function (_a) {
178
- var s = _a.s, res = _a.res, p = _a.p, args = _a.args, a = _a.a, notifyWarning = _a.notifyWarning;
179
- return sourceAction.onDone(s, res, args, p, a, notifyWarning);
180
- };
181
- }
182
- if (sourceAction.onFail) {
183
- asyncAction_1.errorSideEffects = function (_a) {
184
- var s = _a.s, err = _a.err, p = _a.p, args = _a.args, a = _a.a, notifyWarning = _a.notifyWarning;
185
- return sourceAction.onFail(s, err, args, p, a, notifyWarning);
186
- };
187
- }
188
- acc[actionName] = asyncAction_1;
189
- }
190
- return acc;
191
- }, {});
192
- }
193
- function convertV5Options(sourceOptions) {
194
- var opts = {};
195
- var keys = Object.keys(sourceOptions);
196
- var doNotCopyProps = [
197
- 'onPropsChange',
198
- 'onPropsChangeReducer',
199
- 'getPropsRefValues',
200
- 'onMount',
201
- 'initialActionsMarkedLoading',
202
- ];
203
- keys.forEach(function (prop) {
204
- if (!doNotCopyProps.includes(prop)) {
205
- opts[prop] = sourceOptions[prop];
206
- }
207
- });
208
- if (sourceOptions.onMount) {
209
- opts.onMount = function (_a) {
210
- var s = _a.s, p = _a.p, a = _a.a;
211
- return sourceOptions.onMount(a, p, s);
212
- };
213
- }
214
- if (sourceOptions.getPropsRefValues) {
215
- opts.onPropsChange = {
216
- getDeps: sourceOptions.getPropsRefValues,
217
- effects: function (_a) {
218
- var s = _a.s, p = _a.p, indices = _a.indices;
219
- return sourceOptions.onPropsChangeReducer ? sourceOptions.onPropsChangeReducer(s, p, indices) : null;
220
- },
221
- sideEffects: function (_a) {
222
- var s = _a.s, p = _a.p, a = _a.a, indices = _a.indices;
223
- return sourceOptions.onPropsChange == null ? null : sourceOptions.onPropsChange(s, p, a, indices);
224
- },
225
- };
226
- }
227
- if (sourceOptions.initialActionsMarkedLoading) {
228
- opts.initialActionsMarkedLoading = sourceOptions.initialActionsMarkedLoading;
229
- }
230
- return opts;
231
- }
232
- function useStateDecorator(getInitialState, actions, props, options, middlewares) {
233
- if (options === void 0) { options = {}; }
234
- var _a = useReducer(function (s) { return (s > 100 ? 0 : s + 1); }, 0), forceRefresh = _a[1];
235
- var actionsRef = useRef();
236
- var optionsRef = useRef();
237
- if (actionsRef.current == null) {
238
- actionsRef.current = convertV5Actions(actions);
239
- }
240
- if (optionsRef.current == null) {
241
- optionsRef.current = convertV5Options(options);
242
- }
243
- var storeRef = useRef();
244
- if (storeRef.current == null) {
245
- var middlewareFactories = middlewares;
246
- if (options.logEnabled) {
247
- middlewareFactories.push(logDetailedEffects());
248
- }
249
- storeRef.current = createStore(__assign(__assign({ getInitialState: getInitialState, actions: actionsRef.current }, optionsRef.current), { middlewares: middlewareFactories }));
250
- }
251
- storeRef.current.setProps(props);
252
- useLayoutEffect(function () {
253
- return storeRef.current.addStateListener(function () {
254
- forceRefresh();
255
- });
256
- }, []);
257
- var store = storeRef.current;
258
- return {
259
- state: store.state,
260
- actions: store.actions,
261
- loading: store.loading,
262
- loadingMap: store.loadingMap,
263
- loadingParallelMap: store.loadingParallelMap,
264
- abortAction: store.abortAction,
265
- };
266
- }
267
- /**
268
- * Hook to execute a function when hook is mounted.
269
- * @param onMount function to execute on the hook nount
270
- */
271
- function useOnMount(onMount) {
272
- useEffect(function () {
273
- var removeListener = onMount();
274
- return typeof removeListener === 'function' ? removeListener : undefined;
275
- }, []);
276
- }
277
- /**
278
- * Hook to execute a function when hook is unmounted.
279
- * @param onUnMount function to execute on the hook nount
280
- */
281
- function useOnUnmount(onUnmount, propList) {
282
- if (propList === void 0) { propList = []; }
283
- useEffect(function () {
284
- return onUnmount;
285
- }, propList);
286
- }
287
- /**
288
- * Hook to execute a function when hook is unmounted.
289
- * @param onUnMount function to execute on the hook nount
290
- */
291
- function useOnUnload(onUnload) {
292
- useEffect(function () {
293
- document.addEventListener('beforeunload', onUnload);
294
- return function () {
295
- document.removeEventListener('beforeunload', onUnload);
296
- };
297
- }, []);
298
- }
299
-
300
- export default useStateDecorator;
301
- export { isAdvancedSyncAction, isAsyncAction, isAsyncPromiseGetAction, isSyncAction, useOnMount, useOnUnload, useOnUnmount };
1
+ import{ConflictPolicy as e,createStore as n}from"./index";export{ConflictPolicy,setGlobalConfig}from"./index";import{useReducer as r,useRef as t,useLayoutEffect as o,useEffect as i}from"react";var c=function(){return(c=Object.assign||function(e){for(var n,r=1,t=arguments.length;r<t;r++)for(var o in n=arguments[r])Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o]);return e}).apply(this,arguments)},s=function e(n,r){if(n===r)return!0;if(n&&r&&"object"==typeof n&&"object"==typeof r){if(n.constructor!==r.constructor)return!1;var t,o,i;if(Array.isArray(n)){if((t=n.length)!=r.length)return!1;for(o=t;0!=o--;)if(!e(n[o],r[o]))return!1;return!0}if(n.constructor===RegExp)return n.source===r.source&&n.flags===r.flags;if(n.valueOf!==Object.prototype.valueOf)return n.valueOf()===r.valueOf();if(n.toString!==Object.prototype.toString)return n.toString()===r.toString();if((t=(i=Object.keys(n)).length)!==Object.keys(r).length)return!1;for(o=t;0!=o--;)if(!Object.prototype.hasOwnProperty.call(r,i[o]))return!1;for(o=t;0!=o--;){var c=i[o];if(!e(n[c],r[c]))return!1}return!0}return n!=n&&r!=r},u=null;function a(e){return!(e instanceof Function)&&e.hasOwnProperty("promiseGet")}function f(e){return!(e instanceof Function)&&(e.hasOwnProperty("promise")||e.hasOwnProperty("promiseGet"))}function l(e){return!(e instanceof Function||e.hasOwnProperty("promise")&&e.hasOwnProperty("promiseGet"))}function g(e){return e instanceof Function}function p(e){return!(e instanceof Function||e.hasOwnProperty("promise")||e.hasOwnProperty("promiseGet"))}function d(n){return Object.keys(n).reduce((function(r,t){var o=n[t];if(g(o))r[t]=function(e){return o(e.s,e.args,e.p)};else if(p(o)){var i={effects:function(e){return o.action(e.s,e.args,e.p)}};o.onActionDone&&(i.sideEffects=function(e){var n=e.s,r=e.p,t=e.args,i=e.a,c=e.notifyWarning;o.onActionDone(n,t,r,i,c)}),r[t]=i}else if(function(e){return!(e instanceof Function)&&e.hasOwnProperty("promise")}(o)||a(o)){var c;c=a(o)?{getPromise:function(e){var n=e.s,r=e.p,t=e.a,i=e.args,c=e.abortSignal;return o.promiseGet(i,n,r,t,c)},retryCount:3,conflictPolicy:e.REUSE}:{getPromise:function(e){var n=e.s,r=e.p,t=e.a,i=e.args,c=e.abortSignal;return o.promise(i,n,r,t,c)}},["abortable","conflictPolicy","getPromiseId","isTriggerRetryError","rejectPromiseOnError","retryCount","debounceTimeout","debounceSideEffectsTimeout","retryDelaySeed"].forEach((function(e){null!=o[e]&&(c[e]=o[e])})),o.successMessage&&(c.getSuccessMessage=function(){return o.successMessage}),o.errorMessage&&(c.getSuccessMessage=function(){return o.errorMessage}),o.getSuccessMessage&&(c.getSuccessMessage=function(e){var n=e.res,r=e.args,t=e.p;return o.getSuccessMessage(n,r,t)}),o.getErrorMessage&&(c.getErrorMessage=function(e){var n=e.err,r=e.args,t=e.p;return o.getErrorMessage(n,r,t)}),o.preReducer&&(c.preEffects=function(e){var n=e.s,r=e.p,t=e.args;return o.preReducer(n,t,r)}),o.optimisticReducer&&(c.optimisticEffects=function(e){var n=e.s,r=e.p,t=e.args;return o.optimisticReducer(n,t,r)}),o.reducer&&(c.effects=function(e){var n=e.s,r=e.res,t=e.p,i=e.args;return o.reducer(n,r,i,t)}),o.errorReducer&&(c.errorEffects=function(e){var n=e.s,r=e.err,t=e.p,i=e.args;return o.errorReducer(n,r,i,t)}),o.onDone&&(c.sideEffects=function(e){var n=e.s,r=e.res,t=e.p,i=e.args,c=e.a,s=e.notifyWarning;return o.onDone(n,r,i,t,c,s)}),o.onFail&&(c.errorSideEffects=function(e){var n=e.s,r=e.err,t=e.p,i=e.args,c=e.a,s=e.notifyWarning;return o.onFail(n,r,i,t,c,s)}),r[t]=c}return r}),{})}function v(e,i,s,a,f){void 0===a&&(a={});var l,g,p,v,y=r((function(e){return e>100?0:e+1}),0)[1],h=t(),m=t();null==h.current&&(h.current=d(i)),null==m.current&&(m.current=(l=a,g={},p=Object.keys(l),v=["onPropsChange","onPropsChangeReducer","getPropsRefValues","onMount","initialActionsMarkedLoading"],p.forEach((function(e){v.includes(e)||(g[e]=l[e])})),l.onMount&&(g.onMount=function(e){var n=e.s,r=e.p,t=e.a;return l.onMount(t,r,n)}),l.getPropsRefValues&&(g.onPropsChange={getDeps:l.getPropsRefValues,effects:function(e){var n=e.s,r=e.p,t=e.indices;return l.onPropsChangeReducer?l.onPropsChangeReducer(n,r,t):null},sideEffects:function(e){var n=e.s,r=e.p,t=e.a,o=e.indices;return null==l.onPropsChange?null:l.onPropsChange(n,r,t,o)}}),l.initialActionsMarkedLoading&&(g.initialActionsMarkedLoading=l.initialActionsMarkedLoading),g));var E=t();if(null==E.current){var O=f;a.logEnabled&&O.push(u()),E.current=n(c(c({getInitialState:e,actions:h.current},m.current),{middlewares:O}))}E.current.setProps(s),o((function(){return E.current.addStateListener((function(){y()}))}),[]);var b=E.current;return{state:b.state,actions:b.actions,loading:b.loading,loadingMap:b.loadingMap,loadingParallelMap:b.loadingParallelMap,abortAction:b.abortAction}}function y(e){i((function(){var n=e();return"function"==typeof n?n:void 0}),[])}function h(e,n){void 0===n&&(n=[]),i((function(){return e}),n)}function m(e){i((function(){return document.addEventListener("beforeunload",e),function(){document.removeEventListener("beforeunload",e)}}),[])}"development"!==process.env.NODE_ENV&&"test"!==process.env.NODE_ENV||(u=function(e){return void 0===e&&(e=console),function(){var n="",r=e,t=function(e,n){var r;if(n!==e){var t=null!=e?typeof e:typeof n;if("number"===t||"string"===t||"boolean"===t)r="".concat(e," => ").concat(""===n?'""':n);else if(e&&e.length||n&&n.length)if(null==e)r="was ".concat(e,", now contains ").concat(n.length," element(s)");else if(null==n)r="contained ".concat(e.length," element(s), now is ").concat(n);else if(0===e.length)r="was empty, now contains ".concat(n.length," elements");else if(0===n.length)r="contained ".concat(e.length," elements, now is empty");else{var o=n.filter((function(n){return!e.find((function(e){return s(n,e)}))})),i=e.filter((function(e){return!n.find((function(n){return s(e,n)}))}));o.length>10&&(o="".concat(o.length," element(s) added")),i.length>10&&(i="".concat(i.length," element(s) removed")),r={added:o,removed:i}}else r=n}return r},o=function(e){return"string"==typeof e?'"'.concat(e,'"'):e};return{init:function(t){var i,c;n=(null===(i=t.options)||void 0===i?void 0:i.name)?"[".concat(null===(c=t.options)||void 0===c?void 0:c.name,"]"):"";var s=t.state;e.group(n,"initialState"),Object.keys(s).forEach((function(e){r.log(e,":",o(s[e]))})),e.groupEnd()},destroy:null,effects:function(e,i,c,s){var u,a=null===(u=e.context)||void 0===u?void 0:u.args,f=[n,e.name.toString()];if(e.isAsync&&("preEffects"===e.type?f.push(s?"START":"CANCELLED"):"effects"===e.type?f.push("DONE"):f.push("FAILED")),null!=c||a&&0!==Object.keys(a).length?r.group.apply(r,f):r.log.apply(r,f),a&&Object.keys(a).length>0&&(r.group("Arguments"),a.forEach((function(e){r.log(e,":",o(e))})),r.groupEnd()),null!=c){if(r.groupCollapsed("Before"),null==i)r.log("was null");else Object.keys(i).forEach((function(e){r.log(e,":",o(i[e]))}));if(r.groupEnd(),r.groupCollapsed("After"),null==c)r.log("was null");else Object.keys(c).forEach((function(e){r.log(e,":",o(c[e]))}));if(r.groupEnd(),null!=c){r.group("Diff");var l=function(e,n){var r={};return e&&Object.keys(e).forEach((function(o){if(n.hasOwnProperty(o)){var i=e[o],c=n[o],s=t(i,c);s&&(r[o]=s)}else r[o]="was deleted"})),Object.keys(n).forEach((function(o){if(null==e||!e.hasOwnProperty(o)){var i=n[o];r[o]=t(void 0,i)}})),r}(i,c);Object.keys(l).forEach((function(e){return r.log(e,":",l[e])})),r.groupEnd()}}return r.groupEnd(),null}}}});export default v;export{p as isAdvancedSyncAction,f as isAsyncAction,l as isAsyncPromiseGetAction,g as isSyncAction,y as useOnMount,m as useOnUnload,h as useOnUnmount};
302
2
  //# sourceMappingURL=v5.js.map