react-hooks-global-states 1.0.6 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +179 -93
- package/lib/bundle.js +1 -1
- package/lib/bundle.js.LICENSE.txt +0 -9
- package/lib/src/GlobalStore.combiners.d.ts +12 -0
- package/lib/src/GlobalStore.context.d.ts +5 -0
- package/lib/src/GlobalStore.d.ts +37 -37
- package/lib/src/GlobalStore.functionHooks.d.ts +8 -6
- package/lib/src/GlobalStore.types.d.ts +17 -17
- package/lib/src/GlobalStoreAbstract.d.ts +8 -8
- package/lib/src/index.d.ts +8 -7
- package/package.json +1 -2
package/lib/src/GlobalStore.d.ts
CHANGED
|
@@ -4,10 +4,10 @@ export declare const throwNoSubscribersWereAdded: () => never;
|
|
|
4
4
|
* The GlobalStore class is the main class of the library and it is used to create a GlobalStore instances
|
|
5
5
|
* @template {TState} TState - The type of the state object
|
|
6
6
|
* @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
|
|
7
|
-
* @template {
|
|
7
|
+
* @template {TStateMutator} TStateMutator - The type of the actionsConfig object (optional) (default: null) if a configuration is passed, the hook will return an object with the actions then all the store manipulation will be done through the actions
|
|
8
8
|
* */
|
|
9
|
-
export declare class GlobalStore<TState, TMetadata = null,
|
|
10
|
-
protected actionsConfig:
|
|
9
|
+
export declare class GlobalStore<TState, TMetadata = null, TStateMutator extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> = StateSetter<TState>> {
|
|
10
|
+
protected actionsConfig: TStateMutator | null;
|
|
11
11
|
/**
|
|
12
12
|
* list of all the subscribers setState functions
|
|
13
13
|
* @template {TState} TState - The type of the state object
|
|
@@ -16,68 +16,68 @@ export declare class GlobalStore<TState, TMetadata = null, TStateSetter extends
|
|
|
16
16
|
/**
|
|
17
17
|
* Actions of the store
|
|
18
18
|
*/
|
|
19
|
-
actions?: ActionCollectionResult<TState, TMetadata,
|
|
19
|
+
actions?: ActionCollectionResult<TState, TMetadata, TStateMutator>;
|
|
20
20
|
/**
|
|
21
21
|
* additional configuration for the store
|
|
22
22
|
* @template {TState} TState - The type of the state object
|
|
23
23
|
* @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
|
|
24
|
-
* @template {
|
|
25
|
-
* @property {GlobalStoreConfig<TState, TMetadata,
|
|
26
|
-
* @property {GlobalStoreConfig<TState, TMetadata,
|
|
27
|
-
* @property {GlobalStoreConfig<TState, TMetadata,
|
|
28
|
-
* @property {GlobalStoreConfig<TState, TMetadata,
|
|
29
|
-
* @property {GlobalStoreConfig<TState, TMetadata,
|
|
24
|
+
* @template {TStateMutator} TStateMutator - The type of the actionsConfig object (optional) (default: null) if a configuration is passed, the hook will return an object with the actions then all the store manipulation will be done through the actions
|
|
25
|
+
* @property {GlobalStoreConfig<TState, TMetadata, TStateMutator>} config.metadata - The metadata to pass to the callbacks (optional) (default: null)
|
|
26
|
+
* @property {GlobalStoreConfig<TState, TMetadata, TStateMutator>} config.onInit - The callback to execute when the store is initialized (optional) (default: null)
|
|
27
|
+
* @property {GlobalStoreConfig<TState, TMetadata, TStateMutator>} config.onStateChanged - The callback to execute when the state is changed (optional) (default: null)
|
|
28
|
+
* @property {GlobalStoreConfig<TState, TMetadata, TStateMutator>} config.onSubscribed - The callback to execute when a component is subscribed to the store (optional) (default: null)
|
|
29
|
+
* @property {GlobalStoreConfig<TState, TMetadata, TStateMutator>} config.computePreventStateChange - The callback to execute when the state is changed to compute if the state change should be prevented (optional) (default: null)
|
|
30
30
|
*/
|
|
31
|
-
protected config: GlobalStoreConfig<TState, TMetadata,
|
|
31
|
+
protected config: GlobalStoreConfig<TState, TMetadata, TStateMutator>;
|
|
32
32
|
/**
|
|
33
33
|
* execute once the store is created
|
|
34
34
|
* @template {TState} TState - The type of the state object
|
|
35
35
|
* @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
|
|
36
|
-
* @template {
|
|
37
|
-
* @param {StateConfigCallbackParam<TState, TMetadata,
|
|
36
|
+
* @template {TStateMutator} TStateMutator - The type of the actionsConfig object (optional) (default: null) if a configuration is passed, the hook will return an object with the actions then all the store manipulation will be done through the actions
|
|
37
|
+
* @param {StateConfigCallbackParam<TState, TMetadata, TStateMutator>} parameters - The parameters object brings the following properties: setState, getState, setMetadata, getMetadata
|
|
38
38
|
* @param {Dispatch<SetStateAction<TState>>} parameters.setState - The setState function to update the state
|
|
39
39
|
* @param {() => TState} parameters.getState - The getState function to get the state
|
|
40
40
|
* @param {Dispatch<SetStateAction<TMetadata>>} parameters.setMetadata - The setMetadata function to update the metadata
|
|
41
41
|
* @param {() => TMetadata} parameters.getMetadata - The getMetadata function to get the metadata
|
|
42
42
|
* */
|
|
43
|
-
protected onInit?: GlobalStoreConfig<TState, TMetadata,
|
|
43
|
+
protected onInit?: GlobalStoreConfig<TState, TMetadata, TStateMutator>['onInit'];
|
|
44
44
|
/**
|
|
45
45
|
* execute every time the state is changed
|
|
46
46
|
* @template {TState} TState - The type of the state object
|
|
47
47
|
* @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
|
|
48
|
-
* @template {
|
|
49
|
-
* @param {StateConfigCallbackParam<TState, TMetadata,
|
|
48
|
+
* @template {TStateMutator} TStateMutator - The type of the actionsConfig object (optional) (default: null) if a configuration is passed, the hook will return an object with the actions then all the store manipulation will be done through the actions
|
|
49
|
+
* @param {StateConfigCallbackParam<TState, TMetadata, TStateMutator>} parameters - The parameters object brings the following properties: setState, getState, setMetadata, getMetadata
|
|
50
50
|
* @param {Dispatch<SetStateAction<TState>>} parameters.setState - The setState function to update the state
|
|
51
51
|
* @param {() => TState} parameters.getState - The getState function to get the state
|
|
52
52
|
* @param {Dispatch<SetStateAction<TMetadata>>} parameters.setMetadata - The setMetadata function to update the metadata
|
|
53
53
|
* @param {() => TMetadata} parameters.getMetadata - The getMetadata function to get the metadata
|
|
54
54
|
* */
|
|
55
|
-
protected onStateChanged?: GlobalStoreConfig<TState, TMetadata,
|
|
55
|
+
protected onStateChanged?: GlobalStoreConfig<TState, TMetadata, TStateMutator>['onStateChanged'];
|
|
56
56
|
/**
|
|
57
57
|
* Execute each time a new component gets subscribed to the store
|
|
58
58
|
* @template {TState} TState - The type of the state object
|
|
59
59
|
* @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
|
|
60
|
-
* @template {
|
|
61
|
-
* @param {StateConfigCallbackParam<TState, TMetadata,
|
|
60
|
+
* @template {TStateMutator} TStateMutator - The type of the actionsConfig object (optional) (default: null) if a configuration is passed, the hook will return an object with the actions then all the store manipulation will be done through the actions
|
|
61
|
+
* @param {StateConfigCallbackParam<TState, TMetadata, TStateMutator>} parameters - The parameters object brings the following properties: setState, getState, setMetadata, getMetadata
|
|
62
62
|
* @param {Dispatch<SetStateAction<TState>>} parameters.setState - The setState function to update the state
|
|
63
63
|
* @param {() => TState} parameters.getState - The getState function to get the state
|
|
64
64
|
* @param {Dispatch<SetStateAction<TMetadata>>} parameters.setMetadata - The setMetadata function to update the metadata
|
|
65
65
|
* @param {() => TMetadata} parameters.getMetadata - The getMetadata function to get the metadata
|
|
66
66
|
* */
|
|
67
|
-
protected onSubscribed?: GlobalStoreConfig<TState, TMetadata,
|
|
67
|
+
protected onSubscribed?: GlobalStoreConfig<TState, TMetadata, TStateMutator>['onSubscribed'];
|
|
68
68
|
/**
|
|
69
69
|
* Execute every time a state change is triggered and before the state is updated, it allows to prevent the state change by returning true
|
|
70
70
|
* @template {TState} TState - The type of the state object
|
|
71
71
|
* @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
|
|
72
|
-
* @template {
|
|
73
|
-
* @param {StateConfigCallbackParam<TState, TMetadata,
|
|
72
|
+
* @template {TStateMutator} TStateMutator - The type of the actionsConfig object (optional) (default: null) if a configuration is passed, the hook will return an object with the actions then all the store manipulation will be done through the actions
|
|
73
|
+
* @param {StateConfigCallbackParam<TState, TMetadata, TStateMutator>} parameters - The parameters object brings the following properties: setState, getState, setMetadata, getMetadata
|
|
74
74
|
* @param {Dispatch<SetStateAction<TState>>} parameters.setState - The setState function to update the state
|
|
75
75
|
* @param {() => TState} parameters.getState - The getState function to get the state
|
|
76
76
|
* @param {Dispatch<SetStateAction<TMetadata>>} parameters.setMetadata - The setMetadata function to update the metadata
|
|
77
77
|
* @param {() => TMetadata} parameters.getMetadata - The getMetadata function to get the metadata
|
|
78
78
|
* @returns {boolean} - true to prevent the state change, false to allow the state change
|
|
79
79
|
* */
|
|
80
|
-
protected computePreventStateChange?: GlobalStoreConfig<TState, TMetadata,
|
|
80
|
+
protected computePreventStateChange?: GlobalStoreConfig<TState, TMetadata, TStateMutator>['computePreventStateChange'];
|
|
81
81
|
/**
|
|
82
82
|
* We use a wrapper in order to be able to force the state update when necessary even with primitive types
|
|
83
83
|
*/
|
|
@@ -98,9 +98,9 @@ export declare class GlobalStore<TState, TMetadata = null, TStateSetter extends
|
|
|
98
98
|
* The metadata object could be null if not needed
|
|
99
99
|
* The setter Object is used to define the actions that will be used to manipulate the state
|
|
100
100
|
* @param {TState} state - The initial state
|
|
101
|
-
* @param {
|
|
101
|
+
* @param {TStateMutator} actionsConfig - The actions configuration object (optional) (default: null) if not null the store manipulation will be done through the actions
|
|
102
102
|
* */
|
|
103
|
-
constructor(state: TState, config: GlobalStoreConfig<TState, TMetadata,
|
|
103
|
+
constructor(state: TState, config: GlobalStoreConfig<TState, TMetadata, TStateMutator>);
|
|
104
104
|
/**
|
|
105
105
|
* Create a new global store with custom action
|
|
106
106
|
* The metadata object could be null if not needed
|
|
@@ -114,15 +114,15 @@ export declare class GlobalStore<TState, TMetadata = null, TStateSetter extends
|
|
|
114
114
|
* @param {GlobalStoreConfig<TState, TMetadata>} config.onStateChanged - The callback to execute when the state is changed (optional) (default: null)
|
|
115
115
|
* @param {GlobalStoreConfig<TState, TMetadata>} config.onSubscribed - The callback to execute when a new component gets subscribed to the store (optional) (default: null)
|
|
116
116
|
* @param {GlobalStoreConfig<TState, TMetadata>} config.computePreventStateChange - The callback to execute every time a state change is triggered and before the state is updated, it allows to prevent the state change by returning true (optional) (default: null)
|
|
117
|
-
* @param {
|
|
117
|
+
* @param {TStateMutator} actionsConfig - The actions configuration object (optional) (default: null) if not null the store manipulation will be done through the actions
|
|
118
118
|
* */
|
|
119
|
-
constructor(state: TState, config: GlobalStoreConfig<TState, TMetadata,
|
|
119
|
+
constructor(state: TState, config: GlobalStoreConfig<TState, TMetadata, TStateMutator>, actionsConfig: TStateMutator);
|
|
120
120
|
protected initialize: () => Promise<void>;
|
|
121
121
|
/**
|
|
122
122
|
* set the state and update all the subscribers
|
|
123
123
|
* @param {StateSetter<TState>} setter - The setter function or the value to set
|
|
124
124
|
* */
|
|
125
|
-
protected setState: ({ state, forceUpdate
|
|
125
|
+
protected setState: ({ state, forceUpdate }: {
|
|
126
126
|
state: TState;
|
|
127
127
|
forceUpdate: boolean;
|
|
128
128
|
}) => void;
|
|
@@ -159,7 +159,7 @@ export declare class GlobalStore<TState, TMetadata = null, TStateSetter extends
|
|
|
159
159
|
* this parameter object allows to update the state, get the state, update the metadata, get the metadata
|
|
160
160
|
* @returns {StateConfigCallbackParam<TState, TMetadata>} - The parameters object
|
|
161
161
|
* */
|
|
162
|
-
protected getConfigCallbackParam: () => StateConfigCallbackParam<TState, TMetadata,
|
|
162
|
+
protected getConfigCallbackParam: () => StateConfigCallbackParam<TState, TMetadata, TStateMutator>;
|
|
163
163
|
protected updateSubscription: ({ subscriptionId, callback, selector, config, stateWrapper: { state }, }: Omit<SubscriberParameters, "currentState"> & {
|
|
164
164
|
stateWrapper: {
|
|
165
165
|
state: unknown;
|
|
@@ -168,19 +168,19 @@ export declare class GlobalStore<TState, TMetadata = null, TStateSetter extends
|
|
|
168
168
|
protected executeOnSubscribed: () => void;
|
|
169
169
|
/**
|
|
170
170
|
* Returns a custom hook that allows to handle a global state
|
|
171
|
-
* @returns {[TState,
|
|
171
|
+
* @returns {[TState, TStateMutator, TMetadata]} - The state, the state setter or the actions map, the metadata
|
|
172
172
|
* */
|
|
173
|
-
getHook: () => <State = TState>(selector?: SelectorCallback<TState, State>, config?: UseHookConfig<State>) => [state: State extends null ? TState : State, setter: keyof
|
|
173
|
+
getHook: () => <State = TState>(selector?: SelectorCallback<TState, State>, config?: UseHookConfig<State>) => [state: State extends null ? TState : State, setter: keyof TStateMutator extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TStateMutator>, metadata: TMetadata];
|
|
174
174
|
/**
|
|
175
175
|
* Returns an array with the a function to get the state, the state setter or the actions map, and a function to get the metadata
|
|
176
|
-
* @returns {[() => TState,
|
|
176
|
+
* @returns {[() => TState, TStateMutator, () => TMetadata]} - The state getter, the state setter or the actions map, the metadata getter
|
|
177
177
|
* */
|
|
178
|
-
getHookDecoupled: () => [StateGetter<TState>, keyof
|
|
178
|
+
getHookDecoupled: () => [StateGetter<TState>, keyof TStateMutator extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TStateMutator>, MetadataGetter<TMetadata>];
|
|
179
179
|
/**
|
|
180
180
|
* Returns the state setter or the actions map
|
|
181
|
-
* @returns {
|
|
181
|
+
* @returns {TStateMutator} - The state setter or the actions map
|
|
182
182
|
* */
|
|
183
|
-
protected getStateOrchestrator: () => keyof
|
|
183
|
+
protected getStateOrchestrator: () => keyof TStateMutator extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TStateMutator>;
|
|
184
184
|
/**
|
|
185
185
|
* Calculate whenever or not we should compute the callback parameters on the state change
|
|
186
186
|
* @returns {boolean} - True if we should compute the callback parameters on the state change
|
|
@@ -195,7 +195,7 @@ export declare class GlobalStore<TState, TMetadata = null, TStateSetter extends
|
|
|
195
195
|
protected setStateWrapper: StateSetter<TState>;
|
|
196
196
|
/**
|
|
197
197
|
* This creates a map of actions that can be used to modify or interact with the state
|
|
198
|
-
* @returns {ActionCollectionResult<TState, TMetadata,
|
|
198
|
+
* @returns {ActionCollectionResult<TState, TMetadata, TStateMutator>} - The actions map result of the configuration object passed to the constructor
|
|
199
199
|
* */
|
|
200
|
-
protected getStoreActionsMap: () => ActionCollectionResult<TState, TMetadata,
|
|
200
|
+
protected getStoreActionsMap: () => ActionCollectionResult<TState, TMetadata, TStateMutator>;
|
|
201
201
|
}
|
|
@@ -1,28 +1,30 @@
|
|
|
1
1
|
import { ActionCollectionConfig, StateSetter, ActionCollectionResult, UseHookConfig, AvoidNever, UnsubscribeCallback, StateHook, StateGetter, createStateConfig, CustomGlobalHookBuilderParams, CustomGlobalHookParams, SelectorCallback, SubscribeToEmitter } from './GlobalStore.types';
|
|
2
2
|
/**
|
|
3
3
|
* Creates a global state with the given state and config.
|
|
4
|
-
* @returns {} [HOOK,
|
|
4
|
+
* @returns {} [HOOK, DECOUPLED_RETRIEVER, DECOUPLED_MUTATOR] this is an array with the hook, the decoupled getState function and the decoupled setter of the state
|
|
5
5
|
*/
|
|
6
|
-
export declare const createGlobalStateWithDecoupledFuncs: <TState, TMetadata = null, TActions extends ActionCollectionConfig<TState, TMetadata> = null>(state: TState, { actions, ...config }?: createStateConfig<TState, TMetadata, TActions>) => [StateHook<TState, keyof TActions extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TActions>, TMetadata>, StateGetter<TState>, keyof TActions extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TActions>];
|
|
6
|
+
export declare const createGlobalStateWithDecoupledFuncs: <TState, TMetadata = null, TActions extends ActionCollectionConfig<TState, TMetadata> = null>(state: TState, { actions, ...config }?: createStateConfig<TState, TMetadata, TActions>) => [state: StateHook<TState, keyof TActions extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TActions>, TMetadata>, stateRetriever: StateGetter<TState>, stateMutator: keyof TActions extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TActions>];
|
|
7
7
|
/**
|
|
8
8
|
* Creates a global hook that can be used to access the state and actions across the application
|
|
9
9
|
* @returns {} - () => [TState, Setter, TMetadata] the hook that can be used to access the state and the setter of the state
|
|
10
10
|
*/
|
|
11
|
-
export declare const createGlobalState: <TState, TMetadata = null, TActions extends ActionCollectionConfig<TState, TMetadata> = null>(state: TState, config?: createStateConfig<TState, TMetadata, TActions>) => StateHook<TState, keyof TActions extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TActions>, TMetadata
|
|
11
|
+
export declare const createGlobalState: <TState, TMetadata = null, TActions extends ActionCollectionConfig<TState, TMetadata> = null>(state: TState, config?: createStateConfig<TState, TMetadata, TActions>) => StateHook<TState, keyof TActions extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TActions>, TMetadata> & {
|
|
12
|
+
stateControls: () => [stateRetriever: StateGetter<TState>, stateMutator: keyof TActions extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TActions>];
|
|
13
|
+
};
|
|
12
14
|
/**
|
|
13
15
|
* @description
|
|
14
16
|
* Use this function to create a custom global store.
|
|
15
17
|
* You can use this function to create a store with async storage.
|
|
16
18
|
*/
|
|
17
|
-
export declare const createCustomGlobalStateWithDecoupledFuncs: <TInheritMetadata = null, TCustomConfig = null>({ onInitialize, onChange, }: CustomGlobalHookBuilderParams<TInheritMetadata, TCustomConfig>) => <TState, TMetadata = null, TActions extends ActionCollectionConfig<TState, AvoidNever<TInheritMetadata> & AvoidNever<TMetadata>> = null>(state: TState, { config: customConfig, onInit, onStateChanged, ...parameters }?: CustomGlobalHookParams<TCustomConfig, TState, AvoidNever<TInheritMetadata> & AvoidNever<TMetadata>, TActions>) => [StateHook<TState, keyof TActions extends never ? StateSetter<TState> : ActionCollectionResult<TState, AvoidNever<TInheritMetadata> & AvoidNever<TMetadata>, TActions>, AvoidNever<TInheritMetadata> & AvoidNever<TMetadata>>, StateGetter<TState>, keyof TActions extends never ? StateSetter<TState> : ActionCollectionResult<TState, AvoidNever<TInheritMetadata> & AvoidNever<TMetadata>, TActions>];
|
|
19
|
+
export declare const createCustomGlobalStateWithDecoupledFuncs: <TInheritMetadata = null, TCustomConfig = null>({ onInitialize, onChange, }: CustomGlobalHookBuilderParams<TInheritMetadata, TCustomConfig>) => <TState, TMetadata = null, TActions extends ActionCollectionConfig<TState, AvoidNever<TInheritMetadata> & AvoidNever<TMetadata>> = null>(state: TState, { config: customConfig, onInit, onStateChanged, ...parameters }?: CustomGlobalHookParams<TCustomConfig, TState, AvoidNever<TInheritMetadata> & AvoidNever<TMetadata>, TActions>) => [state: StateHook<TState, keyof TActions extends never ? StateSetter<TState> : ActionCollectionResult<TState, AvoidNever<TInheritMetadata> & AvoidNever<TMetadata>, TActions>, AvoidNever<TInheritMetadata> & AvoidNever<TMetadata>>, stateRetriever: StateGetter<TState>, stateMutator: keyof TActions extends never ? StateSetter<TState> : ActionCollectionResult<TState, AvoidNever<TInheritMetadata> & AvoidNever<TMetadata>, TActions>];
|
|
18
20
|
/**
|
|
19
21
|
* @description
|
|
20
22
|
* Use this function to create a custom global hook which contains a fragment of the state of another hook or a fragment
|
|
21
23
|
*/
|
|
22
|
-
export declare const createDerivate: <TState, TSetter, TMetadata, TDerivate>(useHook: StateHook<TState, TSetter, TMetadata>, selector_: SelectorCallback<TState, TDerivate>, config_?: UseHookConfig<TDerivate>) => <State = TDerivate>(selector?: SelectorCallback<TDerivate, State>, config?: UseHookConfig<State>) => [state: State,
|
|
24
|
+
export declare const createDerivate: <TState, TSetter, TMetadata, TDerivate>(useHook: StateHook<TState, TSetter, TMetadata>, selector_: SelectorCallback<TState, TDerivate>, config_?: UseHookConfig<TDerivate>) => <State = TDerivate>(selector?: SelectorCallback<TDerivate, State>, config?: UseHookConfig<State>) => [state: State, stateMutator: TSetter, metadata: TMetadata];
|
|
23
25
|
/**
|
|
24
26
|
* @description
|
|
25
27
|
* This function allows you to create a derivate emitter
|
|
26
28
|
* With this approach, you can subscribe to changes in a specific fragment or subset of the state.
|
|
27
29
|
*/
|
|
28
|
-
export declare const createDerivateEmitter: <TDerivate,
|
|
30
|
+
export declare const createDerivateEmitter: <TDerivate, TStateRetriever extends StateGetter<unknown>, TState = Exclude<ReturnType<TStateRetriever>, UnsubscribeCallback>>(getter: TStateRetriever, selector: SelectorCallback<TState, TDerivate>) => SubscribeToEmitter<TDerivate>;
|
|
@@ -26,7 +26,7 @@ setter: TState | ((state: TState) => TState),
|
|
|
26
26
|
* The hook to use the global state
|
|
27
27
|
* @returns {[State, StateSetter<TState>, TMetadata]} result - the state, the setter and the metadata
|
|
28
28
|
*/
|
|
29
|
-
export type StateHook<TState, TSetter, TMetadata> = <State = TState>(selector?: (state: TState) => State, config?: UseHookConfig<State>) => [state: State,
|
|
29
|
+
export type StateHook<TState, TSetter, TMetadata> = <State = TState>(selector?: (state: TState) => State, config?: UseHookConfig<State>) => [state: State, stateMutator: TSetter, metadata: TMetadata];
|
|
30
30
|
/**
|
|
31
31
|
* @description
|
|
32
32
|
* Type that prevent ts issues with merging never with other types
|
|
@@ -113,7 +113,7 @@ export interface ActionCollectionConfig<TState, TMetadata = null> {
|
|
|
113
113
|
* whatever data manipulation of the state should be executed through the custom actions with as access to the state and metadata
|
|
114
114
|
* @template {TState} TState - The state type
|
|
115
115
|
* @template {TMetadata} TMetadata - The metadata type
|
|
116
|
-
* @template {
|
|
116
|
+
* @template {TStateMutator} TStateMutator - The storeActionsConfig type (optional) - if you pass an storeActionsConfig the hook will return an object with the actions
|
|
117
117
|
*
|
|
118
118
|
* @example
|
|
119
119
|
*
|
|
@@ -133,8 +133,8 @@ export interface ActionCollectionConfig<TState, TMetadata = null> {
|
|
|
133
133
|
*
|
|
134
134
|
* console.log(state); // 0
|
|
135
135
|
*/
|
|
136
|
-
export type ActionCollectionResult<TState, TMetadata,
|
|
137
|
-
[key in keyof
|
|
136
|
+
export type ActionCollectionResult<TState, TMetadata, TStateMutator extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> = StateSetter<TState>> = TStateMutator extends ActionCollectionConfig<TState, TMetadata> ? {
|
|
137
|
+
[key in keyof TStateMutator]: (...params: Parameters<TStateMutator[key]>) => ReturnType<ReturnType<TStateMutator[key]>>;
|
|
138
138
|
} : null;
|
|
139
139
|
/**
|
|
140
140
|
* Common parameters of the store configuration callback functions
|
|
@@ -145,19 +145,19 @@ export type ActionCollectionResult<TState, TMetadata, TStateSetter extends Actio
|
|
|
145
145
|
* @param {ActionCollectionResult<TState, ActionCollectionConfig<TState, TMetadata>> | null} actions - the actions object returned by the hook when you pass an storeActionsConfig configuration otherwise null
|
|
146
146
|
* @template {TState} TState - The state type
|
|
147
147
|
* @template {TMetadata} TMetadata - The metadata type
|
|
148
|
-
* @template {
|
|
149
|
-
* @template {ActionCollectionResult<TState,
|
|
148
|
+
* @template {TStateMutator} TStateMutator - The storeActionsConfig type (optional) - if you pass an storeActionsConfig the hook will return an object with the actions
|
|
149
|
+
* @template {ActionCollectionResult<TState, TStateMutator>} TStateMutator - the result of the API (optional) - if you don't pass an API as a parameter, you can pass null
|
|
150
150
|
* */
|
|
151
|
-
export type StateConfigCallbackParam<TState = any, TMetadata = null,
|
|
152
|
-
actions: ActionCollectionResult<TState, TMetadata,
|
|
151
|
+
export type StateConfigCallbackParam<TState = any, TMetadata = null, TStateMutator extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> = StateSetter<TState>> = {
|
|
152
|
+
actions: ActionCollectionResult<TState, TMetadata, TStateMutator>;
|
|
153
153
|
} & StoreTools<TState, TMetadata>;
|
|
154
154
|
/**
|
|
155
155
|
* Parameters of the onStateChanged callback function
|
|
156
156
|
* @template {TState} TState - The state type
|
|
157
157
|
* @template {TMetadata} TMetadata - The metadata type
|
|
158
|
-
* @template {
|
|
158
|
+
* @template {TStateMutator} TStateMutator - The storeActionsConfig type (optional) - if you pass an storeActionsConfig the hook will return an object with the actions
|
|
159
159
|
*/
|
|
160
|
-
export type StateChangesParam<TState = any, TMetadata = null,
|
|
160
|
+
export type StateChangesParam<TState = any, TMetadata = null, TStateMutator extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> = StateSetter<TState>> = StateConfigCallbackParam<TState, TMetadata, TStateMutator> & StateChanges<TState>;
|
|
161
161
|
/**
|
|
162
162
|
* Configuration of the store (optional) - if you don't need to use the store configuration you don't need to pass this parameter
|
|
163
163
|
* @param {StateConfigCallbackParam<TState, TMetadata> => void} onInit - callback function called when the store is initialized
|
|
@@ -166,9 +166,9 @@ export type StateChangesParam<TState = any, TMetadata = null, TStateSetter exten
|
|
|
166
166
|
* @param {StateChangesParam<TState, TMetadata> => void} onStateChanged - callback function called every time the state is changed
|
|
167
167
|
* @template TState - the type of the state
|
|
168
168
|
* @template TMetadata - the type of the metadata (optional) - if you don't pass an metadata as a parameter, you can pass null
|
|
169
|
-
* @template {ActionCollectionConfig<TState,TMetadata> | null}
|
|
169
|
+
* @template {ActionCollectionConfig<TState,TMetadata> | null} TStateMutator - the configuration of the API (optional) - if you don't pass an API as a parameter, you can pass null
|
|
170
170
|
* */
|
|
171
|
-
export type GlobalStoreConfig<TState, TMetadata,
|
|
171
|
+
export type GlobalStoreConfig<TState, TMetadata, TStateMutator extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> = StateSetter<TState>> = {
|
|
172
172
|
/**
|
|
173
173
|
* @param {StateConfigCallbackParam<TState, TMetadata> => void} metadata - the initial value of the metadata
|
|
174
174
|
* */
|
|
@@ -177,22 +177,22 @@ export type GlobalStoreConfig<TState, TMetadata, TStateSetter extends ActionColl
|
|
|
177
177
|
* @param {StateConfigCallbackParam<TState, TMetadata> => void} onInit - callback function called when the store is initialized
|
|
178
178
|
* @returns {void} result - void
|
|
179
179
|
* */
|
|
180
|
-
onInit?: (parameters: StateConfigCallbackParam<TState, TMetadata,
|
|
180
|
+
onInit?: (parameters: StateConfigCallbackParam<TState, TMetadata, TStateMutator>) => void;
|
|
181
181
|
/**
|
|
182
182
|
* @param {StateChangesParam<TState, TMetadata> => void} onStateChanged - callback function called every time the state is changed
|
|
183
183
|
* @returns {void} result - void
|
|
184
184
|
*/
|
|
185
|
-
onStateChanged?: (parameters: StateChangesParam<TState, TMetadata,
|
|
185
|
+
onStateChanged?: (parameters: StateChangesParam<TState, TMetadata, TStateMutator>) => void;
|
|
186
186
|
/**
|
|
187
187
|
* @param {StateConfigCallbackParam<TState, TMetadata> => void} onSubscribed - callback function called every time a component is subscribed to the store
|
|
188
188
|
* @returns {void} result - void
|
|
189
189
|
*/
|
|
190
|
-
onSubscribed?: (parameters: StateConfigCallbackParam<TState, TMetadata,
|
|
190
|
+
onSubscribed?: (parameters: StateConfigCallbackParam<TState, TMetadata, TStateMutator>) => void;
|
|
191
191
|
/**
|
|
192
192
|
* @param {StateChangesParam<TState, TMetadata> => boolean} computePreventStateChange - callback function called every time the state is about to change and it allows you to prevent the state change
|
|
193
193
|
* @returns {boolean} result - true if you want to prevent the state change, false otherwise
|
|
194
194
|
*/
|
|
195
|
-
computePreventStateChange?: (parameters: StateChangesParam<TState, TMetadata,
|
|
195
|
+
computePreventStateChange?: (parameters: StateChangesParam<TState, TMetadata, TStateMutator>) => boolean;
|
|
196
196
|
} | null;
|
|
197
197
|
export type UseHookConfig<TState> = {
|
|
198
198
|
/**
|
|
@@ -261,7 +261,7 @@ export type CustomGlobalHookBuilderParams<TInheritMetadata = null, TCustomConfig
|
|
|
261
261
|
* @description
|
|
262
262
|
* This function is called when the state is changed.
|
|
263
263
|
*/
|
|
264
|
-
onChange: ({ setState, setMetadata, getMetadata, getState, actions
|
|
264
|
+
onChange: ({ setState, setMetadata, getMetadata, getState, actions }: StateChangesParam<any, TInheritMetadata>, config: TCustomConfig) => void;
|
|
265
265
|
};
|
|
266
266
|
/**
|
|
267
267
|
* @description
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { StateSetter, StateConfigCallbackParam, StateChangesParam, ActionCollectionConfig, GlobalStoreConfig } from
|
|
2
|
-
import { GlobalStore } from
|
|
1
|
+
import { StateSetter, StateConfigCallbackParam, StateChangesParam, ActionCollectionConfig, GlobalStoreConfig } from './GlobalStore.types';
|
|
2
|
+
import { GlobalStore } from './GlobalStore';
|
|
3
3
|
/**
|
|
4
4
|
* @description
|
|
5
5
|
* Use this class to extends the capabilities of the GlobalStore.
|
|
6
6
|
* by implementing the abstract methods onInitialize and onChange.
|
|
7
7
|
* You can use this class to create a store with async storage.
|
|
8
8
|
*/
|
|
9
|
-
export declare abstract class GlobalStoreAbstract<TState, TMetadata = null,
|
|
10
|
-
constructor(state: TState, config?: GlobalStoreConfig<TState, TMetadata,
|
|
11
|
-
protected onInit: (parameters: StateConfigCallbackParam<TState, TMetadata,
|
|
12
|
-
protected onStateChanged: (parameters: StateChangesParam<TState, TMetadata,
|
|
13
|
-
protected abstract onInitialize: ({ setState, setMetadata, getMetadata, getState, actions, }: StateConfigCallbackParam<TState, TMetadata,
|
|
14
|
-
protected abstract onChange: ({ setState, setMetadata, getMetadata, getState, actions, }: StateChangesParam<TState, TMetadata,
|
|
9
|
+
export declare abstract class GlobalStoreAbstract<TState, TMetadata = null, TStateMutator extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> = StateSetter<TState>> extends GlobalStore<TState, TMetadata, TStateMutator> {
|
|
10
|
+
constructor(state: TState, config?: GlobalStoreConfig<TState, TMetadata, TStateMutator>, actionsConfig?: TStateMutator | null);
|
|
11
|
+
protected onInit: (parameters: StateConfigCallbackParam<TState, TMetadata, TStateMutator>) => void;
|
|
12
|
+
protected onStateChanged: (parameters: StateChangesParam<TState, TMetadata, TStateMutator>) => void;
|
|
13
|
+
protected abstract onInitialize: ({ setState, setMetadata, getMetadata, getState, actions, }: StateConfigCallbackParam<TState, TMetadata, TStateMutator>) => void;
|
|
14
|
+
protected abstract onChange: ({ setState, setMetadata, getMetadata, getState, actions, }: StateChangesParam<TState, TMetadata, TStateMutator>) => void;
|
|
15
15
|
}
|
package/lib/src/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
1
|
+
export * from 'json-storage-formatter';
|
|
2
|
+
export * from './GlobalStore.types';
|
|
3
|
+
export * from './GlobalStore';
|
|
4
|
+
export * from './GlobalStoreAbstract';
|
|
5
|
+
export * from './GlobalStore.functionHooks';
|
|
6
|
+
export * from './GlobalStore.utils';
|
|
7
|
+
export * from './GlobalStore.combiners';
|
|
8
|
+
export * from './GlobalStore.context';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-hooks-global-states",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "This is a package to easily handling global-state across your react-components No-redux",
|
|
5
5
|
"main": "lib/bundle.js",
|
|
6
6
|
"types": "lib/src/index.d.ts",
|
|
@@ -47,7 +47,6 @@
|
|
|
47
47
|
"@babel/preset-react": "^7.18.6",
|
|
48
48
|
"@babel/preset-typescript": "^7.21.0",
|
|
49
49
|
"@types/jest": "^26.0.17",
|
|
50
|
-
"@types/lodash": "^4.14.165",
|
|
51
50
|
"@types/react": "^17.0.0",
|
|
52
51
|
"@types/react-dom": "^17.0.0",
|
|
53
52
|
"@types/react-test-renderer": "^17.0.0",
|