react-global-state-hooks 1.0.8 → 1.0.10

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.
@@ -0,0 +1,123 @@
1
+
2
+ ## Creating hooks with reusable actions
3
+
4
+ Let's say you want to have a STATE with a specific set of actions that you could be reused. With this library is pretty easy to accomplish. Let's create **increase** and **decrease** actions to our count-store. **useCountGlobal.ts**:
5
+
6
+ ```JSX
7
+ import {
8
+ IActionCollectionConfig,
9
+ IActionCollectionResult,
10
+ StateSetter,
11
+ } from 'react-global-state-hooks/lib/GlobalStoreTypes';
12
+
13
+ /**
14
+ * When using a custom api, the getHook and getHookDecoupled will not longer return directly the setter,
15
+ * intead they will return and api with the specific actions and mutations defined for the store
16
+ * Creating a configuration object for our api
17
+ */
18
+ const countActionsApi: IActionCollectionConfig<number> = {
19
+ /* Decrease the value of the count */
20
+ decrease(decrease: number) {
21
+ /**
22
+ * We need to return the async function that is gonna take care of the state mutation or actions
23
+ */
24
+ return async (setter: StateSetter<number>, state: number) => {
25
+ /**
26
+ * Next, we perfom whatever modification we want on top of the store
27
+ */
28
+ return setter(state - decrease);
29
+ };
30
+ },
31
+
32
+ //* Lets add a new action to increase the value of the count */
33
+ increase(increase: number) {
34
+
35
+ return async (setter: StateSetter<number>, state: number) => {
36
+ return setter(state + increase);
37
+ };
38
+ },
39
+ };
40
+
41
+ /**
42
+ * Now our getHook and getHookDecoupled are gonna return our custom api instead of the StateSetter,
43
+ * This will allow us to have more control over our store since the mutations of the same are gonna be limitated
44
+ */
45
+ const countStore = new GlobalStore(0, countActionsApi);
46
+ ```
47
+
48
+ If we remove all the explanatory comments the code will look like this:
49
+
50
+ ```TS
51
+ const countStore = new GlobalStore(0, {
52
+ decrease(decrease: number) {
53
+ return (setter: StateSetter<number>, state: number) =>
54
+ setter(state - decrease);
55
+ },
56
+
57
+ increase(increase: number) {
58
+ return (setter: StateSetter<number>, state: number) =>
59
+ setter(state + increase);
60
+ },
61
+ } as IActionCollectionConfig<number>);
62
+ ```
63
+
64
+ Now lets get our new global hook with specific API
65
+
66
+ ```TS
67
+ export interface ICountActions
68
+ extends IActionCollectionResult<number, IActionCollectionConfig<number>> {
69
+ decrease: (decrease: number) => Promise<number>;
70
+ increase: (increase: number) => Promise<number>;
71
+ }
72
+
73
+ /**
74
+ * The ICountActions interface is optional but it allow you yo get more accurate results for the typescript autocompletes and validations, ignore this if you are not using TS
75
+ */
76
+ export const useCountGlobal = countStore.getHook<ICountActions>();
77
+ ```
78
+
79
+ And that's it! the result of our useCountGlobal will return our actions instead of a simple setter... Let's see how that will look:
80
+
81
+ ```JSX
82
+ import { useCountGlobal } from './useCountGlobal'
83
+
84
+ const MyComponent: Reac.FC = () => {
85
+ const [count, countActions] = useCountGlobal();
86
+
87
+ // this functions are strongly typed
88
+ const increaseClick = () => countActions.increase(1);
89
+ const decreaseClick = () => countActions.decrease(1);
90
+
91
+ return (<>
92
+ <label>{count}<label/><br/>
93
+ <button onPress={increaseClick}>increase<button/>
94
+ <button onPress={decreaseClick}>decrease<button/>
95
+ </>);
96
+ }
97
+
98
+ ```
99
+
100
+ ## Customize persist storage
101
+
102
+ Let suppose you don't like **async-storage** or you also want to implement some kind of encrypt-process. You could easily extend the **GlobalStore** Class, and customize your persist store implementation.
103
+
104
+ ```JSX
105
+ import GlobalState from 'react-global-state-hooks';
106
+ import { IActionCollection } from 'react-global-state-hooks/lib/GlobalStoreTypes';
107
+
108
+ export class SecureGlobalState<
109
+ IState,
110
+ IPersist extends string | null = null,
111
+ IsPersist extends boolean = IPersist extends null ? false : true,
112
+ IActions extends IActionCollection<IState> | null = null
113
+ > extends GlobalState<IState, IPersist, IsPersist, IActions> {
114
+
115
+ protected getStoreItem = () => secureStorage.getItem(this.persistStoreAs as string);
116
+
117
+ /** value is a json string*/
118
+ protected setStoreItem = (value: string) => secureStorage.setItem(this.persistStoreAs as string, value);
119
+
120
+ }
121
+
122
+ export default SecureGlobalState;
123
+ ```
package/README.md CHANGED
@@ -1,172 +1,134 @@
1
1
  # react-global-state-hooks
2
- This is a package to easily handling global-state across your react components No-redux, No-context.
2
+ This is a package to easily handling global-state across your react-components **No-redux**, **No-context**
3
3
 
4
- This utility follows the same style as the default useState hook, this in order to be an intuitive tool to help you to quickly migrate from complex options as redux to the new react-hooks.
4
+ This utility follows the same style as the default **useState** hook, with a subscription pattern and **HOFs** to create a more intuitive, atomic and easy way of sharing state between components
5
5
 
6
6
  ## Creating a global store, an a simple hook
7
7
 
8
8
  We are gonna create a global count example **count.ts**:
9
9
 
10
- ```
10
+ ```JSX
11
+ // Import the store costructor
11
12
  import GlobalStore from 'react-global-state-hooks';
12
13
 
14
+ // initialize your store with the default value of the same.
13
15
  const countStore = new GlobalStore(0);
14
16
 
15
- export const useCount = countStore.getHook();
17
+ // you'll use this function the same way you'll use the **useState**
18
+ export const useCountGlobal = countStore.getHook();
19
+
20
+ // That's it, that's a global store... Strongly typed, with a global-hook that we could reuse cross all our react-components.
16
21
  ```
17
22
 
18
- That's it, that's a global store... Strongly typed, with a global-hook that we could reuse cross all our react-components.
23
+ ## Implementing your global hook into your components
24
+ Let's say we have two components **MyFirstComponent**, **MySecondComponent**, in order to use our global hook they will look just like:
19
25
 
20
- ## Consuming global hook
21
- Let's say we have two components stage1, stage2, in order to use our global hook they will look just like:
22
- ```
23
- import { useCount } from './count'
26
+ ```JSX
27
+ import { useCountGlobal } from './count'
24
28
 
25
- function Stage1() {
26
- const [count, setter] = useCount();
29
+ const MyFirstComponent: React.FC = () => {
30
+ const [count, setter] = useCountGlobal();
27
31
  const onClick = useCallback(() => setter(currentState => currentState + 1), []);
28
- return (<button onPress={onClick}>count: {count}<button/>);
29
- }
30
32
 
31
- function Stage2() {
32
- const [count, setter] = useCount();
33
- const onClick = useCallback(() => setter(currentState => currentState + 1));
34
- return (<button onPress={onClick}>count: {count}<button/>);
33
+ return (<button onclick={onClick}>count: {count}<button/>);
35
34
  }
36
- ```
37
- Just like that, you are using a global state. Note that the only difference between this and the default useState hook is that you are not adding the initial value, cause you already did that when you created the store.
38
-
39
- ## Persisted store
40
-
41
- You could persist the state in the local-storage by just adding a name to the constructor of your global-store let's see.
42
- ```
43
- const countStore = new GlobalStore(0, null, 'GLOBAL_COUNT');
44
- ```
45
-
46
- ## Consuming Persisted Store
47
- ```
48
- const [state, setter] = useCount();
49
- ```
50
-
51
- ## Customize persist storage
52
-
53
- Let suppose you don't like localStorage, or you also want to implement some kind of decode/encode. You could easily extends the GlobalStore Class, and customize your persist store implementation.
54
-
55
- ```
56
- import { decode, encode } from 'wherever';
57
- import GlobalState from 'react-global-state-hooks';
58
- import { IActionCollection } from 'react-global-state-hooks/lib/GlobalStoreTypes';
59
35
 
60
- export class SecureGlobalState<IState> extends GlobalState<IState> {
61
-
62
- protected localStorageGetItem = () => decode(localStorage.getItem(this.persistStoreAs as string));
36
+ const MySecondComponent: React.FC = () => {
37
+ const [count, setter] = useCountGlobal();
38
+ const onClick = useCallback(() => setter(currentState => currentState + 1));
63
39
 
64
- protected localStorageSetItem = (value: string) => localStorage.setItem(this.persistStoreAs as string, encode(value));
65
-
40
+ return (<button onclick={onClick}>count: {count}<button/>);
66
41
  }
67
42
 
68
- export default SecureGlobalState;
43
+ // Just like that! You are now using a global state!!
69
44
  ```
70
45
 
71
- ## Creating hooks with reusable actions
72
-
73
- Let's say you want to have a STATE with a specific set of actions that you could reuse. With this library is pretty easy to accomplish. Let's create **plus** and **decrease** actions to our COUNT-store. **count.ts**:
46
+ Note that the only difference between this and the default **useState** hook is that you are not adding the initial value, cause you already did that when you created the store:
74
47
 
48
+ ```JSX
49
+ const countStore = new GlobalStore(0);
75
50
  ```
76
- import * as IGlobalState from 'react-global-state-hooks/lib/GlobalStoreTypes';
77
- import GlobalStore from 'react-global-state-hooks';
78
51
 
79
- const countStore = new GlobalStore(0, {
80
- plus: (increase: number) => async (setter: IGlobalStore.StateSetter<number>, currentState: number) => {
81
- // perfom whatever login you want
82
- setter(currentState + increase);
83
- },
84
- decrease: (decrease: number) => async (setter: IGlobalStore.StateSetter<number>, currentState: number) => {
85
- // perfom whatever login you want
86
- setter(currentState - decrease);
87
- },
88
- }, 'GLOBAL_COUNT');
52
+ ## Persisted store
89
53
 
90
- export const useCount = countStore.getHook();
54
+ You could persist the state with **local-storage** by just adding the **storage-key** to the constructor of your global-store, for example:
91
55
 
56
+ ```JSX
57
+ // The FIRST parameter is the initial value of the state
58
+ // The Second parameter is an API to restrict access to the state, will talk about that later on [README]:./README.advance.md
59
+ // The Third parameter is the key that will be used on the local-storage
60
+ const countStore = new GlobalStore(0, null, 'GLOBAL_COUNT');
92
61
  ```
93
62
 
94
- Now the result of our useCount will return our actions instead of a simple setter... Let's see:
95
-
96
- ```
97
- import { useCount } from './count'
63
+ ## Consuming Persisted Store
98
64
 
99
- function Stage1() {
100
- const [count, actions] = useCount();
101
- const increaseClick = useCallback(() => actions.plus(1), []);
102
- const decreaseClick = useCallback(() => actions.decrease(1), []);
65
+ ```JSX
66
+ const MyComponent: React.FC = () => {
67
+ // connect the component to the global persisted storage
68
+ const [count, setCount] = useCountGlobal();
69
+ const onClickAddOne = () => setCount(count + 1);
103
70
 
104
- return (<>
105
- <label>{count}<label/><br/>
106
- <button onPress={increaseClick}>increase<button/>
107
- <button onPress={decreaseClick}>decrease<button/>
108
- </>);
71
+ return (<button onclick={onClickAddOne}>count: {count}<button/>);
109
72
  }
110
-
111
73
  ```
112
74
 
113
- You could also type the API contract, in order to take more advantage of typescript, just by passing an interface to getHook method:
114
-
115
- ```
116
- export interface ICountActions {
117
- plus: (increase: number) => Promise<void>,
118
- decrease: (decrease: number) => Promise<void>,
119
- }
75
+ ## Decoupled hook
120
76
 
121
- export const useCount = countStore.getHook<ICountActions>();
122
- ```
123
- The above step is necessary to get the correct typing of the parameters of your actions. The above step is necessary to get the correct typing of the parameters of your actions, otherwise, you'll get the name of the actions but the parameters would be all TYPE-ANY
77
+ If you want to access the global state outside a component or outside a hook, or without subscribing the component to the state changes...
124
78
 
125
- ## Decoupled hook
79
+ This is especially useful when you want to create components that have edition access to a certain store, but they actually don't need to be reactive to the state changes, like a search component that just need to get the current state every time that is going to search the data; but actually don't need to be subscribed to the changes over the collection he is going to be filtering.
126
80
 
127
- Finally, if you want to access the global state outside a component, or without subscribing the component to the state changes...
128
81
 
129
- This is especially useful when you want to reuse an action into another one, or when you wrote components that have edition access to a certain store, but they actually don't need to be reactive to the state changes, like a search component that just need to get the current state every time is gonna search the data, but actually don't need to hear all changes over the collection he is gonna be filtering.
82
+ ```JSX
83
+ import GlobalStore from 'react-global-state-hooks';
130
84
 
131
- ```
132
85
  const countStore = new GlobalStore(0);
133
86
 
134
- export const [getCountValue, countActions] = countStore.getHookDecoupled<ICountActions>()();
87
+ // remember this should be used as the **useState** hook.
88
+ export const useCountGlobal = countStore.getHook();
89
+
90
+ // this functions are not hooks, and they can be used in whatever place into your code, ClassComponents, OtherHooks, Services etc.
91
+ export const [getCountGlobalValue, setCountGlobalValue] = countStore.getHookDecoupled();
92
+
135
93
  ```
136
94
 
137
95
  Let's see a trivial example:
138
- ```
139
- import { useCount, useCountDecoupled } from './count'
140
96
 
141
- function Stage1() {
142
- const [count] = useCount();
97
+ ```JSX
98
+ import { useCountGlobal, setCountGlobalValue } from './useCountGlobal'
99
+
100
+ const CountDisplayerComponent: React.FC = () => {
101
+ const [count] = useCountGlobal();
143
102
 
144
- return (<label>{count}<label/><br/>);
103
+ return (<label>{count}<label/>);
145
104
  }
146
105
 
147
- function Stage2() {
148
- const [, actions] = useCountDecoupled();
149
- const increaseClick = useCallback(() => actions.plus(1), []);
150
- const decreaseClick = useCallback(() => actions.decrease(1), []);
106
+ // Stage2 does not need to be updated once the global count changes
107
+ const CountManagerComponent: React.FC = () => {
108
+ const increaseClick = () => setCountGlobalValue(count => count + 1);
109
+ const decreaseClick = () => setCountGlobalValue(count => count - 1);
151
110
 
152
111
  return (<>
153
- <button onPress={increaseClick}>increase<button/>
154
- <button onPress={decreaseClick}>decrease<button/>
112
+ <button onclick={increaseClick}>increase<button/>
113
+ <button onclick={decreaseClick}>decrease<button/>
155
114
  </>);
156
115
  }
157
116
  ```
158
117
 
159
- Now our stage 1, is just listening to the changes of the state, while stage 2 is just acting as an orchestrator, but stage 2 is not actively listening to state changes, so, actually is not gonna be re-render while stage 1 does.
160
-
161
- # Important notes:
162
- Are concern about performance? this library is for you, instead of handling huge complex stores with options like redux, or by passing the setState to a context Provider (because of the limitations that the context has)... If does, You should just use this library, we are using the more atomic and 'native' way that REACT gives to handle the state, and that is the hook **useState**...
163
-
164
- This utility is just including the implementation of the use state into a subscriber pattern, to enable you to create hooks that will be subscribed to specific store changes, does how we'll be creating a global state hook.
118
+ ## Advance Config
119
+ Here you can see more information how to create more complex services for your global stores.
120
+ [README]:./README.advance.md
165
121
 
166
122
  ## Advantages:
167
123
  1. Using REACT's simplest and default way to deal with the state.
168
124
  2. Adding partial state designations (This is not on useState default functionality)
169
125
  3. Added availability to create actions and decoupled access to the states, no more connects, and dispatches, just call your actions as a normal service of whatever other libraries.
170
126
  4. This library is already taking care of avoiding re-renders if the new state does not have changes
171
- 5. This library also is taking care of batching multiple stores updates by using **React unstable_batchedUpdates**; this is a problem that the **useState** have when you call multiple **setStates** into async flows as setTimeout
172
- 6. This tool also take care for you to avoid localStorage data to lose the data types that you stored. For example when you are using datetimes
127
+ 5. This tool also take care for you to avoid **localStorage** data to lose the data types that you stored. For example when you are using datetimes
128
+ 6. This library also is taking care of batching multiple stores updates by using **React unstable_batchedUpdates**; this is a problem that the **useState** have when you call multiple **setStates** into async flows as setTimeout
129
+
130
+ # Finallly notes:
131
+ Are concern about performance? this library is for you, instead of handling huge complex stores with options like redux, or by passing the setState to a context Provider (because of the limitations that the context has)... You should just use this library, we are using the more atomic and 'native' way that REACT gives to handle the state, and that is the hook **useState**...
132
+
133
+ This utility is just including the implementation of the use state into a subscriber pattern, to enable you to create hooks that will be subscribed to specific store changes, does how we'll be creating a global state hook.
134
+
@@ -10,7 +10,7 @@ export declare const isPrimitive: <T>(value: T) => boolean;
10
10
  * this will disable the default return of the state-setter of the hook, and instead will return the API
11
11
  * @param {string} persistStoreAs - A name if you want to persist the state of the store in localstorage
12
12
  * */
13
- export declare class GlobalStore<IState, IActions extends IGlobalStore.IActionCollection<IState> | null = null> implements IGlobalStore.IGlobalState<IState, IActions> {
13
+ export declare class GlobalStore<IState, IActions extends IGlobalStore.IActionCollectionConfig<IState> | null = null> implements IGlobalStore.IGlobalState<IState, IActions> {
14
14
  protected state: IState;
15
15
  protected actions: IActions;
16
16
  protected persistStoreAs: string | null;
@@ -30,15 +30,15 @@ export declare class GlobalStore<IState, IActions extends IGlobalStore.IActionCo
30
30
  * Returns a global hook that will share information across components by subscribing them to a specific store.
31
31
  * @return [currentState, GlobalState.IHookResult<IState, IActions, IApi>]
32
32
  */
33
- getHook: <IApi extends IGlobalStore.ActionCollectionResult<IActions> | null = IActions extends null ? null : IGlobalStore.ActionCollectionResult<IActions>>() => () => [IState, IGlobalStore.IHookResult<IState, IActions, IApi>];
33
+ getHook: <IApi extends IGlobalStore.IActionCollectionResult<IState, IActions> | null = IActions extends null ? null : IGlobalStore.IActionCollectionResult<IState, IActions>>() => () => [IState, IGlobalStore.IHookResult<IState, IActions, IApi>];
34
34
  /**
35
35
  * This is an access to the subscribers queue and to the current state of a specific store...
36
36
  * THIS IS NOT A REACT-HOOK, so you could use it everywhere example other hooks, and services.
37
37
  * @return [currentState, GlobalState.IHookResult<IState, IActions, IApi>]
38
38
  */
39
- getHookDecoupled: <IApi extends IGlobalStore.ActionCollectionResult<IActions> | null = IActions extends null ? null : IGlobalStore.ActionCollectionResult<IActions>>() => [() => IState, IGlobalStore.IHookResult<IState, IActions, IApi>];
39
+ getHookDecoupled: <IApi extends IGlobalStore.IActionCollectionResult<IState, IActions> | null = IActions extends null ? null : IGlobalStore.IActionCollectionResult<IState, IActions>>() => [() => IState, IGlobalStore.IHookResult<IState, IActions, IApi>];
40
40
  private _stateOrchestrator;
41
- protected get stateOrchestrator(): IGlobalStore.StateSetter<IState> | IGlobalStore.ActionCollectionResult<IActions>;
41
+ protected get stateOrchestrator(): IGlobalStore.StateSetter<IState> | IGlobalStore.IActionCollectionResult<IState, IActions>;
42
42
  /**
43
43
  ** [subscriber-update-callback, hook, newState]
44
44
  */
@@ -48,7 +48,7 @@ export declare class GlobalStore<IState, IActions extends IGlobalStore.IActionCo
48
48
  protected globalSetterToPersistStoreAsync: (setter: Partial<IState> | ((state: IState) => Partial<IState>)) => Promise<void>;
49
49
  static ExecutePendingBatchesCallbacks: (() => void)[];
50
50
  static ExecutePendingBatches: import("lodash").DebouncedFunc<() => void>;
51
- protected getActions: <IApi extends IGlobalStore.ActionCollectionResult<IGlobalStore.IActionCollection<IState>>>() => IApi;
51
+ protected getActions: <IApi extends IGlobalStore.IActionCollectionResult<IState, IGlobalStore.IActionCollectionConfig<IState>>>() => IApi;
52
52
  }
53
53
  export default GlobalStore;
54
54
  //# sourceMappingURL=GlobalStore.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"GlobalStore.d.ts","sourceRoot":"","sources":["../src/GlobalStore.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,YAAY,MAAM,oBAAoB,CAAC;AAOnD,eAAO,MAAM,WAAW,0BAAuH,CAAC;AAEhJ;;;;;;;;IAQI;AACJ,qBAAa,WAAW,CACtB,MAAM,EACN,QAAQ,SAAS,YAAY,CAAC,iBAAiB,CAAC,MAAM,CAAC,GAAG,IAAI,GAAG,IAAI,CACrE,YAAW,YAAY,CAAC,YAAY,CAAC,MAAM,EAAE,QAAQ,CAAC;IAQ1C,SAAS,CAAC,KAAK,EAAE,MAAM;IAAE,SAAS,CAAC,OAAO,EAAE,QAAQ;IAAqB,SAAS,CAAC,cAAc,EAAE,MAAM,GAAG,IAAI;IAN5H,SAAS,CAAC,WAAW,EAAE,YAAY,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAM;IAE/D,IAAW,cAAc,YAExB;gBAEqB,KAAK,EAAE,MAAM,EAAY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AM,CAAyC;IAE5E;;;OAGG;IACI,OAAO,6OAqBZ;IAEF;;;;OAIG;IACI,gBAAgB,mLAGf,MAAM,oDASZ;IAEF,OAAO,CAAC,kBAAkB,CAA0G;IAEpI,SAAS,KAAK,iBAAiB,IAAI,YAAY,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,YAAY,CAAC,uBAAuB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAY3H;IAED;;OAEG;IACH,SAAS,CAAC,MAAM,CAAC,cAAc,EAAE,CAAC,MAAM,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,CAAM;IAErE,SAAS,CAAC,YAAY,sCAAuC,MAAM,KAAK,QAAQ,MAAM,CAAC,aAAa,MAAM,IAAI,UAyB5G;IAEF,SAAS,CAAC,iBAAiB,sCACiB,MAAM,KAAK,QAAQ,MAAM,CAAC,MACpE,QAAQ,IAAI,CAAC,CAAyE;IAExF,SAAS,CAAC,+BAA+B,sCAA6C,MAAM,KAAK,QAAQ,MAAM,CAAC,MAAI,QAAQ,IAAI,CAAC,CAG/H;IAEF,MAAM,CAAC,8BAA8B,EAAE,CAAC,MAAM,IAAI,CAAC,EAAE,CAAM;IAG3D,MAAM,CAAC,qBAAqB,6CAWtB;IAEN,SAAS,CAAC,UAAU,sHAwBlB;CAEH;AAED,eAAe,WAAW,CAAC"}
@@ -5,7 +5,7 @@ const tslib_1 = require("tslib");
5
5
  const react_dom_1 = tslib_1.__importDefault(require("react-dom"));
6
6
  const lodash_1 = require("lodash");
7
7
  const react_1 = require("react");
8
- const isPrimitive = (value) => lodash_1.isNil(value) || lodash_1.isNumber(value) || lodash_1.isBoolean(value) || lodash_1.isString(value) || typeof value === 'symbol';
8
+ const isPrimitive = (value) => (0, lodash_1.isNil)(value) || (0, lodash_1.isNumber)(value) || (0, lodash_1.isBoolean)(value) || (0, lodash_1.isString)(value) || typeof value === 'symbol';
9
9
  exports.isPrimitive = isPrimitive;
10
10
  /**
11
11
  * This is a class to create global-store objects
@@ -17,6 +17,9 @@ exports.isPrimitive = isPrimitive;
17
17
  * @param {string} persistStoreAs - A name if you want to persist the state of the store in localstorage
18
18
  * */
19
19
  class GlobalStore {
20
+ get isPersistStore() {
21
+ return !!this.persistStoreAs;
22
+ }
20
23
  constructor(state, actions = null, persistStoreAs = null) {
21
24
  this.state = state;
22
25
  this.actions = actions;
@@ -24,15 +27,15 @@ class GlobalStore {
24
27
  this.subscribers = [];
25
28
  this.storedStateItem = undefined;
26
29
  this.getPersistStoreValue = () => this.getStoreItem();
27
- this.getStateCopy = () => Object.freeze(lodash_1.cloneDeep(this.state));
30
+ this.getStateCopy = () => Object.freeze((0, lodash_1.cloneDeep)(this.state));
28
31
  /**
29
32
  * Returns a global hook that will share information across components by subscribing them to a specific store.
30
33
  * @return [currentState, GlobalState.IHookResult<IState, IActions, IApi>]
31
34
  */
32
35
  this.getHook = () => () => {
33
- const [value, setter] = react_1.useState(this.state);
36
+ const [value, setter] = (0, react_1.useState)(this.state);
34
37
  const valueWrapper = this.isPersistStore ? this.getPersistStoreValue() : value;
35
- react_1.useEffect(() => {
38
+ (0, react_1.useEffect)(() => {
36
39
  this.subscribers.push(setter);
37
40
  return () => {
38
41
  this.subscribers = this.subscribers.filter((hook) => setter !== hook);
@@ -58,7 +61,7 @@ class GlobalStore {
58
61
  this._stateOrchestrator = null;
59
62
  this.globalSetter = (setter, callback) => {
60
63
  const partialState = typeof setter === 'function' ? setter(this.getStateCopy()) : setter;
61
- let newState = exports.isPrimitive(partialState) || Array.isArray(partialState)
64
+ let newState = (0, exports.isPrimitive)(partialState) || Array.isArray(partialState)
62
65
  ? partialState : Object.assign(Object.assign({}, this.state), partialState);
63
66
  // avoid perform multiple update batches by accumulating state changes of the same hook
64
67
  GlobalStore.batchedUpdates = GlobalStore.batchedUpdates.filter(([, hook, previousState]) => {
@@ -66,7 +69,7 @@ class GlobalStore {
66
69
  if (isSameHook) {
67
70
  // eslint-disable-next-line no-console
68
71
  console.warn('You should try avoid call the same state-setter multiple times at one execution line');
69
- newState = exports.isPrimitive(newState) || Array.isArray(partialState)
72
+ newState = (0, exports.isPrimitive)(newState) || Array.isArray(partialState)
70
73
  ? newState : Object.assign(Object.assign({}, previousState), newState);
71
74
  }
72
75
  return !isSameHook;
@@ -99,9 +102,6 @@ class GlobalStore {
99
102
  }) })), {});
100
103
  };
101
104
  }
102
- get isPersistStore() {
103
- return !!this.persistStoreAs;
104
- }
105
105
  formatItemFromStore(obj) {
106
106
  const isArray = Array.isArray(obj);
107
107
  if (isArray) {
@@ -114,7 +114,7 @@ class GlobalStore {
114
114
  if (isDateType) {
115
115
  return Object.assign(Object.assign({}, acumulator), { [key]: new Date(unformatedValue) });
116
116
  }
117
- return Object.assign(Object.assign({}, acumulator), { [key]: exports.isPrimitive(unformatedValue) ? unformatedValue : this.formatItemFromStore(unformatedValue) });
117
+ return Object.assign(Object.assign({}, acumulator), { [key]: (0, exports.isPrimitive)(unformatedValue) ? unformatedValue : this.formatItemFromStore(unformatedValue) });
118
118
  }, {});
119
119
  }
120
120
  localStorageGetItem() {
@@ -126,7 +126,7 @@ class GlobalStore {
126
126
  const item = this.localStorageGetItem();
127
127
  if (item) {
128
128
  const value = JSON.parse(item);
129
- const primitive = exports.isPrimitive(value);
129
+ const primitive = (0, exports.isPrimitive)(value);
130
130
  const newState = primitive ? value : this.formatItemFromStore(value);
131
131
  this.state = primitive || Array.isArray(value) ? newState : Object.assign(Object.assign({}, this.state), newState);
132
132
  }
@@ -140,7 +140,7 @@ class GlobalStore {
140
140
  return Object.keys(obj).reduce((acumulator, key) => {
141
141
  const value = obj[key];
142
142
  const isDatetime = value instanceof Date;
143
- return (Object.assign(Object.assign({}, acumulator), { [key]: exports.isPrimitive(value) || isDatetime ? value : this.formatToStore(value), [`${key}_type`]: isDatetime ? 'date' : typeof value }));
143
+ return (Object.assign(Object.assign({}, acumulator), { [key]: (0, exports.isPrimitive)(value) || isDatetime ? value : this.formatToStore(value), [`${key}_type`]: isDatetime ? 'date' : typeof value }));
144
144
  }, {});
145
145
  }
146
146
  localStorageSetItem(valueToStore) {
@@ -150,7 +150,7 @@ class GlobalStore {
150
150
  if (this.storedStateItem === this.state)
151
151
  return;
152
152
  this.storedStateItem = this.state;
153
- const valueToStore = exports.isPrimitive(this.state) ? this.state : this.formatToStore(lodash_1.cloneDeep(this.state));
153
+ const valueToStore = (0, exports.isPrimitive)(this.state) ? this.state : this.formatToStore((0, lodash_1.cloneDeep)(this.state));
154
154
  this.localStorageSetItem(JSON.stringify(valueToStore));
155
155
  }
156
156
  get stateOrchestrator() {
@@ -175,7 +175,7 @@ exports.GlobalStore = GlobalStore;
175
175
  GlobalStore.batchedUpdates = [];
176
176
  GlobalStore.ExecutePendingBatchesCallbacks = [];
177
177
  // avoid multiples calls to batchedUpdates
178
- GlobalStore.ExecutePendingBatches = lodash_1.debounce(() => {
178
+ GlobalStore.ExecutePendingBatches = (0, lodash_1.debounce)(() => {
179
179
  const reactBatchedUpdates = react_dom_1.default.unstable_batchedUpdates || ((mock) => mock());
180
180
  reactBatchedUpdates(() => {
181
181
  GlobalStore.batchedUpdates.forEach(([execute]) => {
@@ -2,27 +2,27 @@
2
2
  * @param {StateSetter<IState>} setter - add a new value to the state
3
3
  * @returns {Promise<void>} result - resolves when update_batches finished
4
4
  */
5
- export declare type StateSetter<IState> = (setter: Partial<IState> | ((state: IState) => Partial<IState>)) => Promise<void>;
5
+ export type StateSetter<IState> = (setter: Partial<IState> | ((state: IState) => Partial<IState>)) => Promise<void>;
6
6
  /**
7
7
  * This is the structure required by the API actions in order to be able to capture action parameters and inject state setter into actions.
8
8
  */
9
- export declare type IAction<IState> = <IResult>(...params: any[]) => (setter: StateSetter<IState>, currentState: IState) => Promise<unknown> | IResult;
9
+ export type IAction<IState> = <IResult>(...params: any[]) => (setter: StateSetter<IState>, currentState: IState) => Promise<unknown> | IResult;
10
10
  /**
11
11
  * Configuration of you API
12
12
  */
13
- export interface IActionCollection<IState> {
13
+ export interface IActionCollectionConfig<IState> {
14
14
  [key: string]: IAction<IState>;
15
15
  }
16
16
  /**
17
17
  * This is the API result of the hook (if you passed an API as a parameter)
18
18
  */
19
- export declare type ActionCollectionResult<IActions> = {
20
- [key in keyof IActions]: <IResult>(...params: any[]) => any | IResult;
19
+ export type IActionCollectionResult<IState, IActions extends IActionCollectionConfig<IState> | null> = {
20
+ [key in keyof IActions]: (...params: any[]) => unknown;
21
21
  };
22
22
  /**
23
23
  * Hook result, if you passed an API as a parameter it will be returned in the second position of the hook invoke.
24
24
  */
25
- export declare type IHookResult<IState, IActions extends IActionCollection<IState> | null = null, IApi extends ActionCollectionResult<IActions> | null = IActions extends null ? null : ActionCollectionResult<IActions>> = IApi extends null ? StateSetter<IState> : IActions extends IActionCollection<IState> ? IApi extends ActionCollectionResult<IActions> ? IApi : StateSetter<IState> : StateSetter<IState>;
25
+ export type IHookResult<IState, IActions extends IActionCollectionConfig<IState> | null = null, IApi extends IActionCollectionResult<IState, IActions> | null = IActions extends null ? null : IActionCollectionResult<IState, IActions>> = IApi extends null ? StateSetter<IState> : IActions extends IActionCollectionConfig<IState> ? IApi extends IActionCollectionResult<IState, IActions> ? IApi : StateSetter<IState> : StateSetter<IState>;
26
26
  /**
27
27
  * This is a class to create global-store objects
28
28
  * @template IState
@@ -32,12 +32,12 @@ export declare type IHookResult<IState, IActions extends IActionCollection<IStat
32
32
  * this will disable the default return of the state-setter of the hook, and instead will return the API
33
33
  * @param {string} persistStoreAs - A name if you want to persist the state of the store in localstorage
34
34
  * */
35
- export interface IGlobalState<IState, IActions extends IActionCollection<IState> | null = null> {
35
+ export interface IGlobalState<IState, IActions extends IActionCollectionConfig<IState> | null = null> {
36
36
  /**
37
37
  * Returns a global hook that will share information across components by subscribing them to a specific store.
38
38
  * @return [currentState, GlobalState.IHookResult<IState, IActions, IApi>]
39
39
  */
40
- getHook: <IApi extends IActions extends ActionCollectionResult<IActions> ? ActionCollectionResult<IActions> : null>() => () => [
40
+ getHook: <IApi extends IActions extends IActionCollectionResult<IState, IActions> ? IActionCollectionResult<IState, IActions> : null>() => () => [
41
41
  IState,
42
42
  IHookResult<IState, IActions, IApi>
43
43
  ];
@@ -46,7 +46,7 @@ export interface IGlobalState<IState, IActions extends IActionCollection<IState>
46
46
  * THIS IS NOT A REACT-HOOK, so you could use it everywhere example other hooks, and services.
47
47
  * @return [currentState, GlobalState.IHookResult<IState, IActions, IApi>]
48
48
  */
49
- getHookDecoupled: <IApi extends IActions extends ActionCollectionResult<IActions> ? ActionCollectionResult<IActions> : null>() => [
49
+ getHookDecoupled: <IApi extends IActions extends IActionCollectionResult<IState, IActions> ? IActionCollectionResult<IState, IActions> : null>() => [
50
50
  () => IState,
51
51
  IHookResult<IState, IActions, IApi>
52
52
  ];
@@ -1 +1 @@
1
- {"version":3,"file":"GlobalStoreTypes.d.ts","sourceRoot":"","sources":["../src/GlobalStoreTypes.ts"],"names":[],"mappings":"AAAA;;;EAGE;AACF,oBAAY,WAAW,CAAC,MAAM,IAAI,CAChC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC,KAC3D,OAAO,CAAC,IAAI,CAAC,CAAC;AAEnB;;EAEE;AAEF,oBAAY,OAAO,CAAC,MAAM,IAAI,CAAC,OAAO,EAAE,GAAG,MAAM,EAAE,GAAG,EAAE,KAAK,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,EAAE,YAAY,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC;AAE/I;;EAEE;AACF,MAAM,WAAW,iBAAiB,CAAC,MAAM;IACvC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CAChC;AAED;;EAEE;AACF,oBAAY,sBAAsB,CAAC,QAAQ,IAAI;KAE5C,GAAG,IAAI,MAAM,QAAQ,GAAG,CAAC,OAAO,EAAE,GAAG,MAAM,EAAE,GAAG,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C;AAExB;;;;;;;;IAQI;AACJ,MAAM,WAAW,YAAY,CAC3B,MAAM,EAAE,QAAQ,SAAS,uBAAuB,CAAC,MAAM,CAAC,GAAG,IAAI,GAAG,IAAI;IAEtE;;;MAGE;IACF,OAAO,EAAE,CAAC,IAAI,SAAS,QAAQ,SAAS,uBAAuB,CAAC,MAAM,EAAE,QAAQ,CAAC,GAAG,uBAAuB,CAAC,MAAM,EAAE,QAAQ,CAAC,GAAG,IAAI,OAAO,MAAM;QAC/I,MAAM;QACN,WAAW,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,CAAC;KACpC,CAAC;IAEF;;;;MAIE;IACF,gBAAgB,EAAE,CAAC,IAAI,SAAS,QAAQ,SAAS,uBAAuB,CAAC,MAAM,EAAE,QAAQ,CAAC,GAAG,uBAAuB,CAAC,MAAM,EAAE,QAAQ,CAAC,GAAG,IAAI,OAAQ;QACnJ,MAAM,MAAM;QACZ,WAAW,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,CAAC;KACpC,CAAC;CACH"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-global-state-hooks",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "This is a package to easily handling global-state across your react components No-redux, No-context.",
5
5
  "main": "lib/GlobalStore.js",
6
6
  "files": [