react-native-global-state-hooks 2.0.7 → 2.1.1
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 +324 -60
- package/lib/GlobalStore.d.ts +160 -48
- package/lib/GlobalStore.d.ts.map +1 -1
- package/lib/GlobalStore.js +275 -134
- package/lib/GlobalStore.types.d.ts +108 -0
- package/lib/GlobalStore.types.d.ts.map +1 -0
- package/lib/{GlobalStoreTypes.js → GlobalStore.types.js} +0 -0
- package/lib/GlobalStore.utils.d.ts +3 -0
- package/lib/GlobalStore.utils.d.ts.map +1 -0
- package/lib/GlobalStore.utils.js +5 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +6 -0
- package/package.json +4 -5
- package/lib/GlobalStoreTypes.d.ts +0 -66
- package/lib/GlobalStoreTypes.d.ts.map +0 -1
package/lib/GlobalStore.d.ts
CHANGED
|
@@ -1,53 +1,165 @@
|
|
|
1
1
|
import { Dispatch, SetStateAction } from 'react';
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
protected
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
*
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
protected
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
2
|
+
import { ActionCollectionConfig, StateSetter, GlobalStoreConfig, ActionCollectionResult, StateConfigCallbackParam } from './GlobalStore.types';
|
|
3
|
+
/**
|
|
4
|
+
* The GlobalStore class is the main class of the library and it is used to create a GlobalStore instances
|
|
5
|
+
* @template {TState} TState - The type of the state object
|
|
6
|
+
* @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
|
|
7
|
+
* @template {TStateSetter} TStateSetter - The type of the setterConfig 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
|
+
* */
|
|
9
|
+
export declare class GlobalStore<TState, TMetadata = null, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> | null = StateSetter<TState>> {
|
|
10
|
+
protected state: TState;
|
|
11
|
+
protected metadata: TMetadata;
|
|
12
|
+
protected setterConfig: TStateSetter | null;
|
|
13
|
+
protected config: GlobalStoreConfig<TState, TMetadata, TStateSetter>;
|
|
14
|
+
/**
|
|
15
|
+
* list of all the subscribers setState functions
|
|
16
|
+
* @template {TState} TState - The type of the state object
|
|
17
|
+
* */
|
|
18
|
+
subscribers: Set<StateSetter<TState>>;
|
|
19
|
+
/**
|
|
20
|
+
* execute once the store is created
|
|
21
|
+
* @template {TState} TState - The type of the state object
|
|
22
|
+
* @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
|
|
23
|
+
* @template {TStateSetter} TStateSetter - The type of the setterConfig 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
|
|
24
|
+
* @param {StateConfigCallbackParam<TState, TMetadata, TStateSetter>} parameters - The parameters object brings the following properties: setState, getState, setMetadata, getMetadata
|
|
25
|
+
* @param {Dispatch<SetStateAction<TState>>} parameters.setState - The setState function to update the state
|
|
26
|
+
* @param {() => TState} parameters.getState - The getState function to get the state
|
|
27
|
+
* @param {Dispatch<SetStateAction<TMetadata>>} parameters.setMetadata - The setMetadata function to update the metadata
|
|
28
|
+
* @param {() => TMetadata} parameters.getMetadata - The getMetadata function to get the metadata
|
|
29
|
+
* */
|
|
30
|
+
protected onInit?: GlobalStoreConfig<TState, TMetadata, TStateSetter>['onInit'];
|
|
31
|
+
/**
|
|
32
|
+
* execute every time the state is changed
|
|
33
|
+
* @template {TState} TState - The type of the state object
|
|
34
|
+
* @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
|
|
35
|
+
* @template {TStateSetter} TStateSetter - The type of the setterConfig 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
|
|
36
|
+
* @param {StateConfigCallbackParam<TState, TMetadata, TStateSetter>} parameters - The parameters object brings the following properties: setState, getState, setMetadata, getMetadata
|
|
37
|
+
* @param {Dispatch<SetStateAction<TState>>} parameters.setState - The setState function to update the state
|
|
38
|
+
* @param {() => TState} parameters.getState - The getState function to get the state
|
|
39
|
+
* @param {Dispatch<SetStateAction<TMetadata>>} parameters.setMetadata - The setMetadata function to update the metadata
|
|
40
|
+
* @param {() => TMetadata} parameters.getMetadata - The getMetadata function to get the metadata
|
|
41
|
+
* */
|
|
42
|
+
protected onStateChanged?: GlobalStoreConfig<TState, TMetadata, TStateSetter>['onStateChanged'];
|
|
43
|
+
/**
|
|
44
|
+
* Execute each time a new component gets subscribed to the store
|
|
45
|
+
* @template {TState} TState - The type of the state object
|
|
46
|
+
* @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
|
|
47
|
+
* @template {TStateSetter} TStateSetter - The type of the setterConfig 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
|
|
48
|
+
* @param {StateConfigCallbackParam<TState, TMetadata, TStateSetter>} parameters - The parameters object brings the following properties: setState, getState, setMetadata, getMetadata
|
|
49
|
+
* @param {Dispatch<SetStateAction<TState>>} parameters.setState - The setState function to update the state
|
|
50
|
+
* @param {() => TState} parameters.getState - The getState function to get the state
|
|
51
|
+
* @param {Dispatch<SetStateAction<TMetadata>>} parameters.setMetadata - The setMetadata function to update the metadata
|
|
52
|
+
* @param {() => TMetadata} parameters.getMetadata - The getMetadata function to get the metadata
|
|
53
|
+
* */
|
|
54
|
+
protected onSubscribed?: GlobalStoreConfig<TState, TMetadata, TStateSetter>['onSubscribed'];
|
|
55
|
+
/**
|
|
56
|
+
* Execute everytime a state change is triggered and before the state is updated, it allows to prevent the state change by returning true
|
|
57
|
+
* @template {TState} TState - The type of the state object
|
|
58
|
+
* @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
|
|
59
|
+
* @template {TStateSetter} TStateSetter - The type of the setterConfig 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
|
|
60
|
+
* @param {StateConfigCallbackParam<TState, TMetadata, TStateSetter>} parameters - The parameters object brings the following properties: setState, getState, setMetadata, getMetadata
|
|
61
|
+
* @param {Dispatch<SetStateAction<TState>>} parameters.setState - The setState function to update the state
|
|
62
|
+
* @param {() => TState} parameters.getState - The getState function to get the state
|
|
63
|
+
* @param {Dispatch<SetStateAction<TMetadata>>} parameters.setMetadata - The setMetadata function to update the metadata
|
|
64
|
+
* @param {() => TMetadata} parameters.getMetadata - The getMetadata function to get the metadata
|
|
65
|
+
* @returns {boolean} - true to prevent the state change, false to allow the state change
|
|
66
|
+
* */
|
|
67
|
+
protected computePreventStateChange?: GlobalStoreConfig<TState, TMetadata, TStateSetter>['computePreventStateChange'];
|
|
68
|
+
/**
|
|
69
|
+
* Create a new instance of the GlobalStore
|
|
70
|
+
* @param {TState} state - The initial state
|
|
71
|
+
* @param {TMetadata} metadata - The metadata object (optional) (default: null) no reactive information set to share with the subscribers
|
|
72
|
+
* @param {TStateSetter} setterConfig - The actions configuration object (optional) (default: null) if not null the store manipulation will be done through the actions
|
|
73
|
+
* @param {GlobalStoreConfig<TState, TMetadata>} config - The configuration object (optional) (default: { metadata: null })
|
|
74
|
+
* @param {StateConfigCallbackParam<TState, TMetadata>} config.onInit - The callback to execute when the store is initialized (optional) (default: null)
|
|
75
|
+
* @param {StateConfigCallbackParam<TState, TMetadata>} config.onStateChanged - The callback to execute when the state is changed (optional) (default: null)
|
|
76
|
+
* @param {StateConfigCallbackParam<TState, TMetadata>} config.onSubscribed - The callback to execute when a subscriber is added (optional) (default: null)
|
|
77
|
+
* @param {StateConfigCallbackParam<TState, TMetadata>} config.computePreventStateChange - The callback to execute when the state is changed to compute if the state change should be prevented (optional) (default: null)
|
|
78
|
+
* */
|
|
79
|
+
constructor(state: TState, metadata?: TMetadata, setterConfig?: TStateSetter | null, config?: GlobalStoreConfig<TState, TMetadata, TStateSetter>);
|
|
80
|
+
protected onInitializeStore: () => void;
|
|
81
|
+
/**
|
|
82
|
+
* gets a clone of the state
|
|
83
|
+
* @returns {TState} - The state clone
|
|
84
|
+
* */
|
|
85
|
+
protected getStateClone: () => TState;
|
|
86
|
+
/**
|
|
87
|
+
* gets a clone of the metadata
|
|
88
|
+
* @returns {TMetadata} - The metadata clone
|
|
89
|
+
* */
|
|
90
|
+
protected getMetadataClone: () => TMetadata;
|
|
91
|
+
/**
|
|
92
|
+
* set the state and update all the subscribers
|
|
93
|
+
* @param {StateSetter<TState>} setter - The setter function or the value to set
|
|
94
|
+
* @param {React.Dispatch<React.SetStateAction<TState>>} invokerSetState - The setState function of the component that invoked the state change (optional) (default: null) this is used to updated first the component that invoked the state change
|
|
95
|
+
* */
|
|
96
|
+
protected setState: ({ invokerSetState, state, }: {
|
|
97
|
+
state: TState;
|
|
98
|
+
invokerSetState?: React.Dispatch<React.SetStateAction<TState>>;
|
|
42
99
|
}) => void;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
protected
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
100
|
+
/**
|
|
101
|
+
* Set the value of the metadata property, this is no reactive and will not trigger a re-render
|
|
102
|
+
* @param {StateSetter<TMetadata>} setter - The setter function or the value to set
|
|
103
|
+
* */
|
|
104
|
+
protected setMetadata: StateSetter<TMetadata>;
|
|
105
|
+
/**
|
|
106
|
+
* get the parameters object to pass to the callback functions (onInit, onStateChanged, onSubscribed, computePreventStateChange)
|
|
107
|
+
* this parameters object brings the following properties: setState, getState, setMetadata, getMetadata
|
|
108
|
+
* this parameter object allows to update the state, get the state, update the metadata, get the metadata
|
|
109
|
+
* @param {{ invokerSetState?: React.Dispatch<React.SetStateAction<TState>> }} parameters - The setState function of the component that invoked the state change (optional) (default: null) this is used to updated first the component that invoked the state change
|
|
110
|
+
* @returns {StateConfigCallbackParam<TState, TMetadata>} - The parameters object
|
|
111
|
+
* */
|
|
112
|
+
protected getConfigCallbackParam: ({ invokerSetState, }: {
|
|
113
|
+
invokerSetState?: React.Dispatch<React.SetStateAction<TState>>;
|
|
114
|
+
}) => StateConfigCallbackParam<TState, TMetadata, TStateSetter>;
|
|
115
|
+
/**
|
|
116
|
+
* Returns a custom hook that allows to handle a global state
|
|
117
|
+
* @returns {[TState, TStateSetter, TMetadata]} - The state, the state setter or the actions map, the metadata
|
|
118
|
+
* */
|
|
119
|
+
getHook: () => () => [TState, TStateSetter extends StateSetter<TState> ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TStateSetter>, TMetadata];
|
|
120
|
+
/**
|
|
121
|
+
* 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
|
|
122
|
+
* @returns {[() => TState, TStateSetter, () => TMetadata]} - The state getter, the state setter or the actions map, the metadata getter
|
|
123
|
+
* */
|
|
124
|
+
getHookDecoupled: () => [() => TState, TStateSetter extends StateSetter<TState> ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TStateSetter>, () => TMetadata];
|
|
125
|
+
/**
|
|
126
|
+
* returns a wrapper for the setState function that will update the state and all the subscribers
|
|
127
|
+
* @param {{ invokerSetState?: React.Dispatch<React.SetStateAction<TState>> }} parameters - The setState function of the component that invoked the state change (optional) (default: null) this is used to updated first the component that invoked the state change
|
|
128
|
+
* @returns {StateSetter<TState>} - The state setter
|
|
129
|
+
* */
|
|
130
|
+
protected getSetStateWrapper: ({ invokerSetState, }?: {
|
|
131
|
+
invokerSetState?: React.Dispatch<React.SetStateAction<TState>>;
|
|
132
|
+
}) => StateSetter<TState>;
|
|
133
|
+
/**
|
|
134
|
+
* Returns the state setter or the actions map
|
|
135
|
+
* @param {{ invokerSetState?: React.Dispatch<React.SetStateAction<TState>> }} parameters - The setState function of the component that invoked the state change (optional) (default: null) this is used to updated first the component that invoked the state change
|
|
136
|
+
* @returns {TStateSetter} - The state setter or the actions map
|
|
137
|
+
* */
|
|
138
|
+
protected getStateOrchestrator(invokerSetState?: React.Dispatch<React.SetStateAction<TState>>): StateSetter<TState> | ActionCollectionResult<TState, TMetadata, TStateSetter>;
|
|
139
|
+
/**
|
|
140
|
+
* Calculate whenever or not we should compute the callback parameters on the state change
|
|
141
|
+
* @returns {boolean} - True if we should compute the callback parameters on the state change
|
|
142
|
+
* */
|
|
143
|
+
protected hasStateCallbacks: () => boolean;
|
|
144
|
+
/**
|
|
145
|
+
* This is responsible for defining whenever or not the state change should be allowed or prevented
|
|
146
|
+
* the function also execute the functions:
|
|
147
|
+
* - onStateChanged (if defined) - this function is executed after the state change
|
|
148
|
+
* - computePreventStateChange (if defined) - this function is executed before the state change and it should return a boolean value that will be used to determine if the state change should be prevented or not
|
|
149
|
+
* @param {{ setter: StateSetter<TState>; invokerSetState?: React.Dispatch<React.SetStateAction<TState>> }} parameters - The state setter and the setState function of the component that invoked the state change (optional) (default: null) this is used to updated first the component that invoked the state change
|
|
150
|
+
*/
|
|
151
|
+
protected computeSetState: ({ setter, invokerSetState, }: {
|
|
152
|
+
setter: StateSetter<TState>;
|
|
153
|
+
invokerSetState?: React.Dispatch<React.SetStateAction<TState>>;
|
|
154
|
+
}) => void;
|
|
155
|
+
/**
|
|
156
|
+
* This creates a map of actions that can be used to modify or interact with the state
|
|
157
|
+
* @param {{ invokerSetState?: React.Dispatch<React.SetStateAction<TState>> }} parameters - The setState function of the component that invoked the state change (optional) (default: null) this is used to updated first the component that invoked the state change
|
|
158
|
+
* @returns {ActionCollectionResult<TState, TMetadata, TStateSetter>} - The actions map result of the configuration object passed to the constructor
|
|
159
|
+
* */
|
|
160
|
+
protected getStoreActionsMap: ({ invokerSetState, }: {
|
|
161
|
+
invokerSetState?: React.Dispatch<React.SetStateAction<TState>>;
|
|
162
|
+
}) => ActionCollectionResult<TState, TMetadata, TStateSetter>;
|
|
51
163
|
}
|
|
52
164
|
export default GlobalStore;
|
|
53
165
|
//# 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":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"GlobalStore.d.ts","sourceRoot":"","sources":["../src/GlobalStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAuB,MAAM,OAAO,CAAC;AAEtE,OAAO,EACL,sBAAsB,EACtB,WAAW,EACX,iBAAiB,EACjB,sBAAsB,EACtB,wBAAwB,EAEzB,MAAM,qBAAqB,CAAC;AAY7B;;;;;KAKK;AACL,qBAAa,WAAW,CACtB,MAAM,EACN,SAAS,GAAG,IAAI,EAChB,YAAY,SACR,sBAAsB,CAAC,MAAM,EAAE,SAAS,CAAC,GACzC,WAAW,CAAC,MAAM,CAAC,GACnB,IAAI,GAAG,WAAW,CAAC,MAAM,CAAC;IAyF5B,SAAS,CAAC,KAAK,EAAE,MAAM;IACvB,SAAS,CAAC,QAAQ,EAAE,SAAS;IAC7B,SAAS,CAAC,YAAY,EAAE,YAAY,GAAG,IAAI;IAC3C,SAAS,CAAC,MAAM,EAAE,iBAAiB,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC;IA1FtE;;;SAGK;IACE,WAAW,EAAE,GAAG,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAa;IAEzD;;;;;;;;;;SAUK;IACL,SAAS,CAAC,M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