react-native-global-state-hooks 2.0.1 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.advance.md +77 -32
- package/README.md +59 -31
- package/lib/GlobalStore.d.ts +9 -6
- package/lib/GlobalStore.d.ts.map +1 -1
- package/lib/GlobalStore.js +10 -12
- package/lib/GlobalStoreTypes.d.ts +5 -11
- package/lib/GlobalStoreTypes.d.ts.map +1 -1
- package/package.json +2 -3
package/README.advance.md
CHANGED
|
@@ -1,61 +1,105 @@
|
|
|
1
|
-
|
|
2
|
-
## Customize persist storage
|
|
3
|
-
|
|
4
|
-
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.
|
|
5
|
-
|
|
6
1
|
## Creating hooks with reusable actions
|
|
7
2
|
|
|
8
|
-
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
|
|
3
|
+
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**:
|
|
9
4
|
|
|
10
5
|
```JSX
|
|
11
|
-
import
|
|
12
|
-
|
|
6
|
+
import {
|
|
7
|
+
IActionCollectionConfig,
|
|
8
|
+
IActionCollectionResult,
|
|
9
|
+
StateSetter,
|
|
10
|
+
} from 'react-native-global-state-hooks/lib/GlobalStoreTypes';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* When using a custom api, the getHook and getHookDecoupled will not longer return directly the setter,
|
|
14
|
+
* intead they will return and api with the specific actions and mutations defined for the store
|
|
15
|
+
* Creating a configuration object for our api
|
|
16
|
+
*/
|
|
17
|
+
const countActionsApi: IActionCollectionConfig<number> = {
|
|
18
|
+
/* Decrease the value of the count */
|
|
19
|
+
decrease(decrease: number) {
|
|
20
|
+
/**
|
|
21
|
+
* We need to return the async function that is gonna take care of the state mutation or actions
|
|
22
|
+
*/
|
|
23
|
+
return async (setter: StateSetter<number>, state: number) => {
|
|
24
|
+
/**
|
|
25
|
+
* Next, we perfom whatever modification we want on top of the store
|
|
26
|
+
*/
|
|
27
|
+
return setter(state - decrease);
|
|
28
|
+
};
|
|
29
|
+
},
|
|
13
30
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
increase: (increase: number) => (setter: IGlobalState.StateSetter<number>, state: number) => Promise<void>,
|
|
17
|
-
}
|
|
31
|
+
//* Lets add a new action to increase the value of the count */
|
|
32
|
+
increase(increase: number) {
|
|
18
33
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
34
|
+
return async (setter: StateSetter<number>, state: number) => {
|
|
35
|
+
return setter(state + increase);
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Now our getHook and getHookDecoupled are gonna return our custom api instead of the StateSetter,
|
|
42
|
+
* This will allow us to have more control over our store since the mutations of the same are gonna be limitated
|
|
43
|
+
*/
|
|
44
|
+
const countStore = new GlobalStore(0, countActionsApi);
|
|
45
|
+
```
|
|
23
46
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
47
|
+
If we remove all the explanatory comments the code will look like this:
|
|
48
|
+
|
|
49
|
+
```TS
|
|
50
|
+
const countStore = new GlobalStore(0, {
|
|
51
|
+
decrease(decrease: number) {
|
|
52
|
+
return (setter: StateSetter<number>, state: number) =>
|
|
53
|
+
setter(state - decrease);
|
|
27
54
|
},
|
|
28
|
-
|
|
29
|
-
|
|
55
|
+
|
|
56
|
+
increase(increase: number) {
|
|
57
|
+
return (setter: StateSetter<number>, state: number) =>
|
|
58
|
+
setter(state + increase);
|
|
30
59
|
},
|
|
31
|
-
};
|
|
60
|
+
} as IActionCollectionConfig<number>);
|
|
61
|
+
```
|
|
32
62
|
|
|
33
|
-
|
|
63
|
+
Now lets get our new global hook with specific API
|
|
34
64
|
|
|
35
|
-
|
|
65
|
+
```TS
|
|
66
|
+
export interface ICountActions
|
|
67
|
+
extends IActionCollectionResult<number, IActionCollectionConfig<number>> {
|
|
68
|
+
decrease: (decrease: number) => Promise<number>;
|
|
69
|
+
increase: (increase: number) => Promise<number>;
|
|
70
|
+
}
|
|
36
71
|
|
|
72
|
+
/**
|
|
73
|
+
* 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
|
|
74
|
+
*/
|
|
75
|
+
export const useCountGlobal = countStore.getHook<ICountActions>();
|
|
37
76
|
```
|
|
38
77
|
|
|
39
|
-
|
|
78
|
+
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:
|
|
40
79
|
|
|
41
80
|
```JSX
|
|
42
81
|
import { useCountGlobal } from './useCountGlobal'
|
|
43
82
|
|
|
44
|
-
|
|
45
|
-
const [count,
|
|
83
|
+
const MyComponent: Reac.FC = () => {
|
|
84
|
+
const [count, countActions] = useCountGlobal();
|
|
46
85
|
|
|
47
|
-
|
|
48
|
-
const
|
|
86
|
+
// this functions are strongly typed
|
|
87
|
+
const increaseClick = () => countActions.increase(1);
|
|
88
|
+
const decreaseClick = () => countActions.decrease(1);
|
|
49
89
|
|
|
50
90
|
return (<>
|
|
51
|
-
<
|
|
52
|
-
<
|
|
53
|
-
<
|
|
91
|
+
<Text>{count}<Text/>
|
|
92
|
+
<Button onPress={increaseClick} title={'increase'} />
|
|
93
|
+
<Button onPress={decreaseClick} title={'decrease'} />
|
|
54
94
|
</>);
|
|
55
95
|
}
|
|
56
96
|
|
|
57
97
|
```
|
|
58
98
|
|
|
99
|
+
## Customize persist storage
|
|
100
|
+
|
|
101
|
+
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.
|
|
102
|
+
|
|
59
103
|
```JSX
|
|
60
104
|
import GlobalState from 'react-native-global-state-hooks';
|
|
61
105
|
import secureStorage from 'react-native-secure-storage';
|
|
@@ -70,6 +114,7 @@ export class SecureGlobalState<
|
|
|
70
114
|
|
|
71
115
|
protected asyncStorageGetItem = () => secureStorage.getItem(this.persistStoreAs as string, config);
|
|
72
116
|
|
|
117
|
+
/** value is a json string*/
|
|
73
118
|
protected asyncStorageSetItem = (value: string) => secureStorage.setItem(this.persistStoreAs as string, value, config);
|
|
74
119
|
|
|
75
120
|
}
|
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# react-native-global-state-hooks
|
|
2
|
-
This is a package to easily handling global-state across your react-native-components **No-redux
|
|
2
|
+
This is a package to easily handling global-state across your react-native-components **No-redux**, **No-context**
|
|
3
3
|
|
|
4
|
-
This utility follows the same style as the default **useState** hook,
|
|
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
|
**after version 1.0.4, we migrated to @react-native-async-storage/async-storage, because @react-native-async-storage/async-storage has been deprecated!!**
|
|
7
7
|
|
|
@@ -10,104 +10,132 @@ This utility follows the same style as the default **useState** hook, this in or
|
|
|
10
10
|
We are gonna create a global count example **useCountGlobal.ts**:
|
|
11
11
|
|
|
12
12
|
```JSX
|
|
13
|
+
// Import the store costructor
|
|
13
14
|
import GlobalStore from 'react-native-global-state-hooks';
|
|
14
15
|
|
|
16
|
+
// initialize your store with the default value of the same.
|
|
15
17
|
const countStore = new GlobalStore(0);
|
|
16
18
|
|
|
19
|
+
// you'll use this function the same way you'll use the **useState**
|
|
17
20
|
export const useCountGlobal = countStore.getHook();
|
|
21
|
+
|
|
22
|
+
// That's it, that's a global store... Strongly typed, with a global-hook that we could reuse cross all our react-components.
|
|
18
23
|
```
|
|
19
24
|
|
|
20
|
-
|
|
25
|
+
## Implementing your global hook into your components
|
|
26
|
+
Let's say we have two components **MyFirstComponent**, **MySecondComponent**, in order to use our global hook they will look just like:
|
|
21
27
|
|
|
22
|
-
## Consuming global hook
|
|
23
|
-
Let's say we have two components **Stage1**, **Stage2**, in order to use our global hook they will look just like:
|
|
24
28
|
```JSX
|
|
25
29
|
import { useCountGlobal } from './useCountGlobal'
|
|
26
30
|
|
|
27
|
-
const
|
|
31
|
+
const MyFirstComponent: React.FC = () => {
|
|
28
32
|
const [count, setter] = useCountGlobal();
|
|
29
33
|
const onClickAddOne = () => setter(count + 1);
|
|
30
34
|
|
|
31
|
-
return (<
|
|
35
|
+
return (<Button title={`count: ${count}`} onPress={onClickAddOne} />);
|
|
32
36
|
}
|
|
33
37
|
|
|
34
|
-
const
|
|
38
|
+
const MySecondComponent: React.FC = () => {
|
|
35
39
|
const [count, setter] = useCountGlobal();
|
|
36
40
|
const onClickAddTwo = () => setter(count + 2);
|
|
37
41
|
|
|
38
|
-
return (<
|
|
42
|
+
return (<Button title={`count: ${count}`} onPress={onClickAddOne} />);
|
|
39
43
|
}
|
|
40
|
-
```
|
|
41
44
|
|
|
42
|
-
Just like that! You now
|
|
45
|
+
// Just like that! You are now using a global state!!
|
|
46
|
+
```
|
|
43
47
|
|
|
44
48
|
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:
|
|
49
|
+
|
|
45
50
|
```JSX
|
|
46
51
|
const countStore = new GlobalStore(0);
|
|
47
52
|
```
|
|
48
53
|
|
|
49
54
|
## Persisted store
|
|
50
55
|
|
|
51
|
-
You could persist the state
|
|
56
|
+
You could persist the state with **@react-native-async-storage** by just adding the **storage-key** to the constructor of your global-store, for example:
|
|
57
|
+
|
|
52
58
|
```JSX
|
|
53
59
|
// The FIRST parameter is the initial value of the state
|
|
54
|
-
// The Second parameter is an API to restrict access to the state, will talk about that later.
|
|
55
|
-
// The Third parameter is the key that will
|
|
60
|
+
// The Second parameter is an API to restrict access to the state, will talk about that later on [README]:./README.advance.md
|
|
61
|
+
// The Third parameter is the key that will be used on the async-storage
|
|
56
62
|
const countStore = new GlobalStore(0, null, 'GLOBAL_COUNT');
|
|
57
63
|
```
|
|
58
64
|
|
|
59
65
|
## Consuming Persisted Store
|
|
60
66
|
|
|
61
67
|
```JSX
|
|
62
|
-
const
|
|
68
|
+
const MyComponent: React.FC = () => {
|
|
69
|
+
// connect the component to the global persisted storage
|
|
70
|
+
const [count, setCount, isCountReady] = useCountGlobal();
|
|
71
|
+
const onClickAddOne = () => setCount(count + 1);
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* since the async storage is ASYNC, the first time the hook is called into a component we may get the default value, instead of the one from the storage.
|
|
75
|
+
*
|
|
76
|
+
* We are able to validate this with the third value returned in the hook array **isCountReady**, which is a boolean which let us now if the async storage already was reached out.
|
|
77
|
+
*/
|
|
78
|
+
const countLabel = isCountReady ? `count: ${count}` : 'Loading async storage...';
|
|
79
|
+
|
|
80
|
+
return (<Button title={countLabel} onPress={onClickAddOne} />);
|
|
81
|
+
}
|
|
63
82
|
```
|
|
64
83
|
|
|
65
|
-
**isCountUpdated**: With the persist storage, the first time the hooks is called the same is gonna return the default value declared in the **new GlobalStore**, at the same time will perform an **async** request to the **async-storage** in order to get the stored value, to validate if that call was already performed, youl'll get an extra boolean value.
|
|
66
|
-
|
|
67
84
|
## Decoupled hook
|
|
68
85
|
|
|
86
|
+
If you want to access the global state outside a component or outside a hook, or without subscribing the component to the state changes...
|
|
87
|
+
|
|
88
|
+
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.
|
|
89
|
+
|
|
90
|
+
|
|
69
91
|
```JSX
|
|
70
92
|
import GlobalStore from 'react-native-global-state-hooks';
|
|
71
93
|
|
|
72
94
|
const countStore = new GlobalStore(0);
|
|
73
95
|
|
|
96
|
+
// remember this should be used as the **useState** hook.
|
|
74
97
|
export const useCountGlobal = countStore.getHook();
|
|
75
98
|
|
|
99
|
+
// this functions are not hooks, and they can be used in whatever place into your code, ClassComponents, OtherHooks, Services etc.
|
|
76
100
|
export const [getCountGlobalValue, setCountGlobalValue] = countStore.getHookDecoupled();
|
|
77
101
|
|
|
78
102
|
```
|
|
79
103
|
|
|
80
|
-
If you want to access the global state outside a component or outside a hook, or without subscribing the component to the state changes...
|
|
81
|
-
|
|
82
|
-
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.
|
|
83
|
-
|
|
84
|
-
|
|
85
104
|
Let's see a trivial example:
|
|
105
|
+
|
|
86
106
|
```JSX
|
|
87
107
|
import { useCountGlobal, setCountGlobalValue } from './useCountGlobal'
|
|
88
108
|
|
|
89
|
-
const
|
|
109
|
+
const CountDisplayerComponent: React.FC = () => {
|
|
90
110
|
const [count] = useCountGlobal();
|
|
91
111
|
|
|
92
|
-
return (<
|
|
112
|
+
return (<Text>{count}<Text/>);
|
|
93
113
|
}
|
|
94
114
|
|
|
95
115
|
// Stage2 does not need to be updated once the global count changes
|
|
96
|
-
const
|
|
116
|
+
const CountManagerComponent: React.FC = () => {
|
|
97
117
|
const increaseClick = () => setCountGlobalValue(count => count + 1);
|
|
98
118
|
const decreaseClick = () => setCountGlobalValue(count => count - 1);
|
|
99
119
|
|
|
100
120
|
return (<>
|
|
101
|
-
<
|
|
102
|
-
<
|
|
121
|
+
<Button onPress={increaseClick} title={'increase'} />
|
|
122
|
+
<Button onPress={decreaseClick} title={'decrease'}/>
|
|
103
123
|
</>);
|
|
104
124
|
}
|
|
105
125
|
```
|
|
106
126
|
|
|
127
|
+
## Advance Config
|
|
128
|
+
Here you can see more information how to create more complex services for your global stores.
|
|
129
|
+
[README]:./README.advance.md
|
|
130
|
+
|
|
107
131
|
## Advantages:
|
|
108
132
|
1. Using REACT's simplest and default way to deal with the state.
|
|
109
|
-
2.
|
|
110
|
-
3.
|
|
133
|
+
2. Adding partial state designations (This is not on useState default functionality)
|
|
134
|
+
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.
|
|
135
|
+
4. This library is already taking care of avoiding re-renders if the new state does not have changes
|
|
136
|
+
5. This tool also take care for you to avoid **async-storage*** data to lose the data types that you stored. For example when you are using datetimes
|
|
111
137
|
|
|
112
|
-
|
|
113
|
-
|
|
138
|
+
# Finallly notes:
|
|
139
|
+
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**...
|
|
140
|
+
|
|
141
|
+
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.
|
package/lib/GlobalStore.d.ts
CHANGED
|
@@ -18,20 +18,23 @@ export declare class GlobalStore<IState, IPersist extends string | null = null,
|
|
|
18
18
|
protected asyncStorageSetItem(valueToStore: string): Promise<void>;
|
|
19
19
|
protected setAsyncStoreItem(): Promise<void>;
|
|
20
20
|
protected getStateCopy: () => IState;
|
|
21
|
-
getHook: <IApi extends IGlobalStore.
|
|
22
|
-
getHookDecoupled: <IApi extends IGlobalStore.
|
|
21
|
+
getHook: <IApi extends IGlobalStore.IActionCollectionResult<IState, IActions> | null = IActions extends null ? null : IGlobalStore.IActionCollectionResult<IState, IActions>>() => () => [IState, IGlobalStore.IHookResult<IState, IActions, IApi>, IsPersist extends true ? boolean : null];
|
|
22
|
+
getHookDecoupled: <IApi extends IGlobalStore.IActionCollectionResult<IState, IActions> | null = IActions extends null ? null : IGlobalStore.IActionCollectionResult<IState, IActions>>() => [() => IPersist extends string ? Promise<IState> : IState, IGlobalStore.IHookResult<IState, IActions, IApi>];
|
|
23
23
|
private _stateOrchestrator;
|
|
24
|
-
protected get stateOrchestrator(): IGlobalStore.StateSetter<IState> | IGlobalStore.
|
|
24
|
+
protected get stateOrchestrator(): IGlobalStore.StateSetter<IState> | IGlobalStore.IActionCollectionResult<IState, IActions>;
|
|
25
25
|
/**
|
|
26
26
|
** [subscriber-update-callback, hook]
|
|
27
27
|
*/
|
|
28
28
|
protected static batchedUpdates: [() => void, object][];
|
|
29
29
|
protected globalSetter: (setter: IState | ((state: IState) => IState), callback: () => void) => void;
|
|
30
|
-
protected globalSetterAsync: (setter: IState | ((state: IState) => IState)) => Promise<
|
|
31
|
-
protected globalSetterToPersistStoreAsync: (setter: IState | ((state: IState) => IState)) => Promise<
|
|
30
|
+
protected globalSetterAsync: (setter: IState | ((state: IState) => IState)) => Promise<IState>;
|
|
31
|
+
protected globalSetterToPersistStoreAsync: (setter: IState | ((state: IState) => IState)) => Promise<IState>;
|
|
32
32
|
static ExecutePendingBatchesCallbacks: (() => void)[];
|
|
33
|
+
/**
|
|
34
|
+
* React native cannot use unstable_batchedUpdates, it does not have any effect
|
|
35
|
+
*/
|
|
33
36
|
static ExecutePendingBatches: import("lodash").DebouncedFunc<() => void>;
|
|
34
|
-
protected getActions: <IApi extends IGlobalStore.
|
|
37
|
+
protected getActions: <IApi extends IGlobalStore.IActionCollectionResult<IState, IGlobalStore.IActionCollectionConfig<IState>>>() => IApi;
|
|
35
38
|
}
|
|
36
39
|
export default GlobalStore;
|
|
37
40
|
//# sourceMappingURL=GlobalStore.d.ts.map
|
package/lib/GlobalStore.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GlobalStore.d.ts","sourceRoot":"","sources":["../src/GlobalStore.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"GlobalStore.d.ts","sourceRoot":"","sources":["../src/GlobalStore.ts"],"names":[],"mappings":";AAKA,OAAO,KAAK,YAAY,MAAM,oBAAoB,CAAC;AAEnD,eAAO,MAAM,WAAW,0BAAuH,CAAC;AAEhJ,qBAAa,WAAW,CACtB,MAAM,EACN,QAAQ,SAAS,MAAM,GAAG,IAAI,GAAG,IAAI,EACrC,SAAS,SAAS,OAAO,GAAG,QAAQ,SAAS,IAAI,GAAG,KAAK,GAAG,IAAI,EAChE,QAAQ,SAAS,YAAY,CAAC,uBAA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AAM,EAAE,QAAQ,CAAC,CAY3H;IAED;;MAEE;IACF,SAAS,CAAC,MAAM,CAAC,cAAc,EAAE,CAAC,MAAM,IAAI,EAAE,MAAM,CAAC,EAAE,CAAM;IAE7D,SAAS,CAAC,YAAY,6BAA8B,MAAM,KAAK,MAAM,aAAa,MAAM,IAAI,UAqB1F;IAEF,SAAS,CAAC,iBAAiB,6BAAoC,MAAM,KAAK,MAAM,MAC9E,QAAQ,MAAM,CAAC,CAAmF;IAEpG,SAAS,CAAC,+BAA+B,6BAAoC,MAAM,KAAK,MAAM,MAAI,QAAQ,MAAM,CAAC,CAK/G;IAEF,MAAM,CAAC,8BAA8B,EAAE,CAAC,MAAM,IAAI,CAAC,EAAE,CAAM;IAE3D;;MAEE;IACF,MAAM,CAAC,qBAAqB,6CAQtB;IAEN,SAAS,CAAC,UAAU,sHA0BlB;CAEH;AAED,eAAe,WAAW,CAAC"}
|
package/lib/GlobalStore.js
CHANGED
|
@@ -5,7 +5,6 @@ const tslib_1 = require("tslib");
|
|
|
5
5
|
const react_1 = require("react");
|
|
6
6
|
const lodash_1 = require("lodash");
|
|
7
7
|
const async_storage_1 = (0, tslib_1.__importDefault)(require("@react-native-async-storage/async-storage"));
|
|
8
|
-
const react_dom_1 = (0, tslib_1.__importDefault)(require("react-dom"));
|
|
9
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';
|
|
10
9
|
exports.isPrimitive = isPrimitive;
|
|
11
10
|
class GlobalStore {
|
|
@@ -59,10 +58,11 @@ class GlobalStore {
|
|
|
59
58
|
GlobalStore.ExecutePendingBatchesCallbacks.push(callback);
|
|
60
59
|
GlobalStore.ExecutePendingBatches();
|
|
61
60
|
};
|
|
62
|
-
this.globalSetterAsync = (setter) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { return new Promise((resolve) => this.globalSetter(setter, () => resolve())); });
|
|
61
|
+
this.globalSetterAsync = (setter) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { return new Promise((resolve) => this.globalSetter(setter, () => resolve(this.state))); });
|
|
63
62
|
this.globalSetterToPersistStoreAsync = (setter) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
|
|
64
63
|
yield this.globalSetterAsync(setter);
|
|
65
64
|
yield this.setAsyncStoreItem();
|
|
65
|
+
return this.state;
|
|
66
66
|
});
|
|
67
67
|
this.getActions = () => {
|
|
68
68
|
const actions = this.actions;
|
|
@@ -173,17 +173,15 @@ exports.GlobalStore = GlobalStore;
|
|
|
173
173
|
*/
|
|
174
174
|
GlobalStore.batchedUpdates = [];
|
|
175
175
|
GlobalStore.ExecutePendingBatchesCallbacks = [];
|
|
176
|
-
|
|
177
|
-
|
|
176
|
+
/**
|
|
177
|
+
* React native cannot use unstable_batchedUpdates, it does not have any effect
|
|
178
|
+
*/
|
|
178
179
|
GlobalStore.ExecutePendingBatches = (0, lodash_1.debounce)(() => {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
GlobalStore.batchedUpdates.forEach(([execute]) => {
|
|
182
|
-
execute();
|
|
183
|
-
});
|
|
184
|
-
GlobalStore.batchedUpdates = [];
|
|
185
|
-
GlobalStore.ExecutePendingBatchesCallbacks.forEach((callback) => callback());
|
|
186
|
-
GlobalStore.ExecutePendingBatchesCallbacks = [];
|
|
180
|
+
GlobalStore.batchedUpdates.forEach(([execute]) => {
|
|
181
|
+
execute();
|
|
187
182
|
});
|
|
183
|
+
GlobalStore.batchedUpdates = [];
|
|
184
|
+
GlobalStore.ExecutePendingBatchesCallbacks.forEach((callback) => callback());
|
|
185
|
+
GlobalStore.ExecutePendingBatchesCallbacks = [];
|
|
188
186
|
}, 0);
|
|
189
187
|
exports.default = GlobalStore;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @param {StateSetter<IState>} setter - add a new value to the state
|
|
3
3
|
* @returns {void} result - void
|
|
4
4
|
*/
|
|
5
|
-
export declare type StateSetter<IState> = (setter: IState | ((state: IState) => IState)) =>
|
|
5
|
+
export declare type StateSetter<IState> = (setter: IState | ((state: IState) => IState)) => Promise<IState>;
|
|
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
|
*/
|
|
@@ -14,21 +14,15 @@ export interface IActionCollectionConfig<IState> {
|
|
|
14
14
|
[key: string]: IActionConfig<IState>;
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
17
|
-
* This should be the API format of the hook (if you passed an API as a parameter)
|
|
18
|
-
*/
|
|
19
|
-
export interface IActionCollectionResult {
|
|
20
|
-
[key: string]: (...params: any[]) => Promise<unknown>;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
17
|
* This is the API result of the hook (if you passed an API as a parameter)
|
|
24
18
|
*/
|
|
25
|
-
export declare type
|
|
19
|
+
export declare type IActionCollectionResult<IState, IActions extends IActionCollectionConfig<IState> | null> = {
|
|
26
20
|
[key in keyof IActions]: (...params: any[]) => unknown;
|
|
27
21
|
};
|
|
28
22
|
/**
|
|
29
23
|
* Hook result, if you passed an API as a parameter it will be returned in the second position of the hook invoke.
|
|
30
24
|
*/
|
|
31
|
-
export declare type IHookResult<IState, IActions extends IActionCollectionConfig<IState> | null = null, IApi extends
|
|
25
|
+
export declare 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>;
|
|
32
26
|
/**
|
|
33
27
|
* This is a class to create global-store objects
|
|
34
28
|
* @template IState
|
|
@@ -49,7 +43,7 @@ export interface IGlobalState<IState, IPersist extends string | null = null, IsP
|
|
|
49
43
|
* Returns a global hook that will share information across components by subscribing them to a specific store.
|
|
50
44
|
* @return [currentState, GlobalState.IHookResult<IState, IActions, IApi>, initialStatePersistStorage | null, isUpdatedPersistStorage | null]
|
|
51
45
|
*/
|
|
52
|
-
getHook: <IApi extends IActions extends
|
|
46
|
+
getHook: <IApi extends IActions extends IActionCollectionResult<IState, IActions> ? IActionCollectionResult<IState, IActions> : null>() => () => [
|
|
53
47
|
IState,
|
|
54
48
|
IHookResult<IState, IActions, IApi>,
|
|
55
49
|
IsPersist extends true ? boolean : null
|
|
@@ -59,7 +53,7 @@ export interface IGlobalState<IState, IPersist extends string | null = null, IsP
|
|
|
59
53
|
* THIS IS NOT A REACT-HOOK, so you could use it everywhere example other hooks, and services.
|
|
60
54
|
* @return [currentState, GlobalState.IHookResult<IState, IActions, IApi>]
|
|
61
55
|
*/
|
|
62
|
-
getHookDecoupled: <IApi extends IActions extends
|
|
56
|
+
getHookDecoupled: <IApi extends IActions extends IActionCollectionResult<IState, IActions> ? IActionCollectionResult<IState, IActions> : null>() => [
|
|
63
57
|
() => IPersist extends string ? Promise<IState> : IState,
|
|
64
58
|
IHookResult<IState, IActions, IApi>
|
|
65
59
|
];
|
|
@@ -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,MAAM,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC,KACzC,
|
|
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,MAAM,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC,KACzC,OAAO,CAAC,MAAM,CAAC,CAAC;AAErB;;EAEE;AACF,oBAAY,aAAa,CAAC,MAAM,IAA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;IAG9D,cAAc,EAAE,QAAQ,CAAC;IAEzB,cAAc,EAAE,OAAO,CAAC;IAExB;;;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;QACnC,SAAS,SAAS,IAAI,GAAG,OAAO,GAAG,IAAI;KACxC,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,OAAO;QAClJ,MAAM,QAAQ,SAAS,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM;QACxD,WAAW,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,CAAC;KACpC,CAAC;CACH;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB,CAClC,MAAM,EACN,QAAQ,SAAS,MAAM,GAAG,IAAI,GAAG,IAAI,EACrC,SAAS,SAAS,OAAO,GAAG,QAAQ,SAAS,IAAI,GAAG,KAAK,GAAG,IAAI,EAChE,QAAQ,SAAS,uBAAuB,CAAC,MAAM,CAAC,GAAG,IAAI,GAAG,IAAI,CAC9D,SAAQ,YAAY,CAAC,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,QAAQ,CAAC;CAE5D"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-global-state-hooks",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3",
|
|
4
4
|
"description": "This is a package to easily handling global-state across your react-native-components No-redux",
|
|
5
5
|
"main": "lib/GlobalStore.js",
|
|
6
6
|
"files": [
|
|
@@ -61,7 +61,6 @@
|
|
|
61
61
|
"peerDependencies": {
|
|
62
62
|
"@react-native-async-storage/async-storage": "workspace:*",
|
|
63
63
|
"lodash": "workspace:*",
|
|
64
|
-
"react": "workspace:*"
|
|
65
|
-
"react-dom": "workspace:*"
|
|
64
|
+
"react": "workspace:*"
|
|
66
65
|
}
|
|
67
66
|
}
|