react-native-global-state-hooks 5.0.15 → 6.0.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.
@@ -1,10 +1 @@
1
1
  /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */
2
-
3
- /**
4
- * @license
5
- * Lodash <https://lodash.com/>
6
- * Copyright OpenJS Foundation and other contributors <https://openjsf.org/>
7
- * Released under MIT license <https://lodash.com/license>
8
- * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
9
- * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
10
- */
@@ -1,201 +1,28 @@
1
- import { ActionCollectionConfig, StateSetter, GlobalStoreConfig, ActionCollectionResult, StateConfigCallbackParam, MetadataSetter, UseHookConfig, StateGetter, SubscribeCallbackConfig, SubscribeCallback, SelectorCallback, SubscriberParameters, SubscriptionCallback, MetadataGetter } from "./GlobalStore.types";
2
- export declare const throwNoSubscribersWereAdded: () => never;
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 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
- * */
9
- export declare class GlobalStore<TState, TMetadata = null, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> = StateSetter<TState>> {
10
- protected actionsConfig: TStateSetter | null;
11
- /**
12
- * list of all the subscribers setState functions
13
- * @template {TState} TState - The type of the state object
14
- * */
15
- subscribers: Map<string, SubscriberParameters>;
16
- /**
17
- * Actions of the store
18
- */
19
- actions?: ActionCollectionResult<TState, TMetadata, TStateSetter>;
20
- /**
21
- * additional configuration for the store
22
- * @template {TState} TState - The type of the state object
23
- * @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
24
- * @template {TStateSetter} TStateSetter - 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, TStateSetter>} config.metadata - The metadata to pass to the callbacks (optional) (default: null)
26
- * @property {GlobalStoreConfig<TState, TMetadata, TStateSetter>} config.onInit - The callback to execute when the store is initialized (optional) (default: null)
27
- * @property {GlobalStoreConfig<TState, TMetadata, TStateSetter>} config.onStateChanged - The callback to execute when the state is changed (optional) (default: null)
28
- * @property {GlobalStoreConfig<TState, TMetadata, TStateSetter>} config.onSubscribed - The callback to execute when a component is subscribed to the store (optional) (default: null)
29
- * @property {GlobalStoreConfig<TState, TMetadata, TStateSetter>} config.computePreventStateChange - The callback to execute when the state is changed to compute if the state change should be prevented (optional) (default: null)
30
- */
1
+ import * as GlobalStoreBase from "react-hooks-global-states";
2
+ import { GlobalStoreConfig, StateChangesParam, TMetadataResult, ActionCollectionConfig, ActionCollectionResult } from "./GlobalStore.types";
3
+ import { GlobalStoreAbstract, MetadataGetter, StateGetter, StateSetter } from "react-hooks-global-states";
4
+ export declare class GlobalStore<TState, TMetadata = null, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> = StateSetter<TState>> extends GlobalStoreAbstract<TState, TMetadata, NonNullable<TStateSetter>> {
31
5
  protected config: GlobalStoreConfig<TState, TMetadata, TStateSetter>;
32
- /**
33
- * execute once the store is created
34
- * @template {TState} TState - The type of the state object
35
- * @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
36
- * @template {TStateSetter} TStateSetter - 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, TStateSetter>} parameters - The parameters object brings the following properties: setState, getState, setMetadata, getMetadata
38
- * @param {Dispatch<SetStateAction<TState>>} parameters.setState - The setState function to update the state
39
- * @param {() => TState} parameters.getState - The getState function to get the state
40
- * @param {Dispatch<SetStateAction<TMetadata>>} parameters.setMetadata - The setMetadata function to update the metadata
41
- * @param {() => TMetadata} parameters.getMetadata - The getMetadata function to get the metadata
42
- * */
43
- protected onInit?: GlobalStoreConfig<TState, TMetadata, TStateSetter>["onInit"];
44
- /**
45
- * execute every time the state is changed
46
- * @template {TState} TState - The type of the state object
47
- * @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
48
- * @template {TStateSetter} TStateSetter - 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, TStateSetter>} parameters - The parameters object brings the following properties: setState, getState, setMetadata, getMetadata
50
- * @param {Dispatch<SetStateAction<TState>>} parameters.setState - The setState function to update the state
51
- * @param {() => TState} parameters.getState - The getState function to get the state
52
- * @param {Dispatch<SetStateAction<TMetadata>>} parameters.setMetadata - The setMetadata function to update the metadata
53
- * @param {() => TMetadata} parameters.getMetadata - The getMetadata function to get the metadata
54
- * */
55
- protected onStateChanged?: GlobalStoreConfig<TState, TMetadata, TStateSetter>["onStateChanged"];
56
- /**
57
- * Execute each time a new component gets subscribed to the store
58
- * @template {TState} TState - The type of the state object
59
- * @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
60
- * @template {TStateSetter} TStateSetter - 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, TStateSetter>} parameters - The parameters object brings the following properties: setState, getState, setMetadata, getMetadata
62
- * @param {Dispatch<SetStateAction<TState>>} parameters.setState - The setState function to update the state
63
- * @param {() => TState} parameters.getState - The getState function to get the state
64
- * @param {Dispatch<SetStateAction<TMetadata>>} parameters.setMetadata - The setMetadata function to update the metadata
65
- * @param {() => TMetadata} parameters.getMetadata - The getMetadata function to get the metadata
66
- * */
67
- protected onSubscribed?: GlobalStoreConfig<TState, TMetadata, TStateSetter>["onSubscribed"];
68
- /**
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
- * @template {TState} TState - The type of the state object
71
- * @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
72
- * @template {TStateSetter} TStateSetter - 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, TStateSetter>} parameters - The parameters object brings the following properties: setState, getState, setMetadata, getMetadata
74
- * @param {Dispatch<SetStateAction<TState>>} parameters.setState - The setState function to update the state
75
- * @param {() => TState} parameters.getState - The getState function to get the state
76
- * @param {Dispatch<SetStateAction<TMetadata>>} parameters.setMetadata - The setMetadata function to update the metadata
77
- * @param {() => TMetadata} parameters.getMetadata - The getMetadata function to get the metadata
78
- * @returns {boolean} - true to prevent the state change, false to allow the state change
79
- * */
80
- protected computePreventStateChange?: GlobalStoreConfig<TState, TMetadata, TStateSetter>["computePreventStateChange"];
81
- /**
82
- * We use a wrapper in order to be able to force the state update when necessary even with primitive types
83
- */
84
- protected stateWrapper: {
85
- state: TState;
86
- };
87
- /**
88
- * @deprecated direct modifications of the state could end up in unexpected behaviors
89
- */
90
- protected get state(): TState;
91
- /**
92
- * Create a simple global store
93
- * @param {TState} state - The initial state
94
- * */
95
- constructor(state: TState);
96
- /**
97
- * Create a new global store with custom action
98
- * The metadata object could be null if not needed
99
- * The setter Object is used to define the actions that will be used to manipulate the state
100
- * @param {TState} state - The initial state
101
- * @param {TStateSetter} actionsConfig - The actions configuration object (optional) (default: null) if not null the store manipulation will be done through the actions
102
- * */
103
- constructor(state: TState, config: GlobalStoreConfig<TState, TMetadata, TStateSetter>);
104
- /**
105
- * Create a new global store with custom action
106
- * The metadata object could be null if not needed
107
- * The setter Object is used to define the actions that will be used to manipulate the state
108
- * The config object is used to define the callbacks that will be executed during the store lifecycle
109
- * The lifecycle callbacks are: onInit, onStateChanged, onSubscribed and computePreventStateChange
110
- * @param {TState} state - The initial state
111
- * @param {GlobalStoreConfig<TState, TMetadata>} config - The configuration object (optional) (default: { metadata: null })
112
- * @param {GlobalStoreConfig<TState, TMetadata>} config.metadata - The metadata object (optional) (default: null) if not null the metadata object will be reactive
113
- * @param {GlobalStoreConfig<TState, TMetadata>} config.onInit - The callback to execute when the store is initialized (optional) (default: null)
114
- * @param {GlobalStoreConfig<TState, TMetadata>} config.onStateChanged - The callback to execute when the state is changed (optional) (default: null)
115
- * @param {GlobalStoreConfig<TState, TMetadata>} config.onSubscribed - The callback to execute when a new component gets subscribed to the store (optional) (default: null)
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 {TStateSetter} actionsConfig - The actions configuration object (optional) (default: null) if not null the store manipulation will be done through the actions
118
- * */
119
- constructor(state: TState, config: GlobalStoreConfig<TState, TMetadata, TStateSetter>, actionsConfig: TStateSetter);
120
- protected initialize: () => Promise<void>;
121
- /**
122
- * set the state and update all the subscribers
123
- * @param {StateSetter<TState>} setter - The setter function or the value to set
124
- * */
125
- protected setState: ({ state, forceUpdate, }: {
126
- state: TState;
127
- forceUpdate: boolean;
128
- }) => void;
129
- /**
130
- * Set the value of the metadata property, this is no reactive and will not trigger a re-render
131
- * @param {MetadataSetter<TMetadata>} setter - The setter function or the value to set
132
- * */
133
- protected setMetadata: MetadataSetter<TMetadata>;
134
- protected getMetadata: () => TMetadata;
135
- protected createChangesSubscriber: ({ callback, selector, config, }: {
136
- selector?: SelectorCallback<unknown, unknown>;
137
- callback: SubscribeCallback<unknown>;
138
- config: SubscribeCallbackConfig<unknown>;
139
- }) => {
140
- stateWrapper: {
141
- state: unknown;
142
- };
143
- subscriptionCallback: SubscriptionCallback;
144
- };
145
- /**
146
- * Return current state of the store
147
- * Optionally you can use this method to subscribe a callback to the store changes
148
- * @param {UseHookConfig<TState, TDerivate>} config - The configuration object (optional) (default: { selector: null, subscriptionCallback: null, config: null })
149
- * @param {TSelector} config.selector - The selector function to derive the state (optional) (default: null)
150
- * @param {TSubscriptionCallback} config.subscriptionCallback - The callback to execute every time the state is changed
151
- * @param {UseHookConfig<TState, TDerivate>} config.config - The configuration for the callback (optional) (default: null)
152
- * @param {UseHookConfig<TState, TDerivate>} config.config.isEqual - The compare function to check if the state is changed (optional) (default: shallowCompare)
153
- * @returns The state of the store, optionally if you provide a subscriptionCallback it this method will return the unsubscribe function
154
- */
155
- protected getState: StateGetter<TState>;
156
- /**
157
- * get the parameters object to pass to the callback functions (onInit, onStateChanged, onSubscribed, computePreventStateChange)
158
- * this parameters object brings the following properties: setState, getState, setMetadata, getMetadata
159
- * this parameter object allows to update the state, get the state, update the metadata, get the metadata
160
- * @returns {StateConfigCallbackParam<TState, TMetadata>} - The parameters object
161
- * */
162
- protected getConfigCallbackParam: () => StateConfigCallbackParam<TState, TMetadata, TStateSetter>;
163
- protected updateSubscription: ({ subscriptionId, callback, selector, config, stateWrapper: { state }, }: Omit<SubscriberParameters, "currentState"> & {
164
- stateWrapper: {
165
- state: unknown;
166
- };
167
- }) => SubscriberParameters;
168
- protected executeOnSubscribed: () => void;
169
- /**
170
- * Returns a custom hook that allows to handle a global state
171
- * @returns {[TState, TStateSetter, TMetadata]} - The state, the state setter or the actions map, the metadata
172
- * */
173
- getHook: () => <State = TState>(selector?: SelectorCallback<TState, State>, config?: UseHookConfig<State>) => [state: State extends null ? TState : State, setter: keyof TStateSetter extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TStateSetter>, metadata: TMetadata];
6
+ constructor(state: TState, config?: GlobalStoreConfig<TState, TMetadata, TStateSetter>, actionsConfig?: TStateSetter | null);
7
+ protected getMetadata: () => TMetadata | TMetadataResult<TMetadata>;
174
8
  /**
175
9
  * 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
10
  * @returns {[() => TState, TStateSetter, () => TMetadata]} - The state getter, the state setter or the actions map, the metadata getter
177
11
  * */
178
- getHookDecoupled: () => [StateGetter<TState>, keyof TStateSetter extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TStateSetter>, MetadataGetter<TMetadata>];
12
+ getHookDecoupled: () => [
13
+ StateGetter<TState>,
14
+ keyof TStateSetter extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TStateSetter>,
15
+ MetadataGetter<TMetadataResult<TMetadata>>
16
+ ];
179
17
  /**
180
- * Returns the state setter or the actions map
181
- * @returns {TStateSetter} - The state setter or the actions map
182
- * */
183
- protected getStateOrchestrator: () => keyof TStateSetter extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TStateSetter>;
184
- /**
185
- * Calculate whenever or not we should compute the callback parameters on the state change
186
- * @returns {boolean} - True if we should compute the callback parameters on the state change
187
- * */
188
- protected hasStateCallbacks: () => boolean;
189
- /**
190
- * This is responsible for defining whenever or not the state change should be allowed or prevented
191
- * the function also execute the functions:
192
- * - onStateChanged (if defined) - this function is executed after the state change
193
- * - 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
194
- */
195
- protected setStateWrapper: StateSetter<TState>;
196
- /**
197
- * This creates a map of actions that can be used to modify or interact with the state
198
- * @returns {ActionCollectionResult<TState, TMetadata, TStateSetter>} - The actions map result of the configuration object passed to the constructor
18
+ * Returns a custom hook that allows to handle a global state
19
+ * @returns {[TState, TStateSetter, TMetadata]} - The state, the state setter or the actions map, the metadata
199
20
  * */
200
- protected getStoreActionsMap: () => ActionCollectionResult<TState, TMetadata, TStateSetter>;
21
+ getHook: () => () => [
22
+ state: TState,
23
+ setter: keyof TStateSetter extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TStateSetter>,
24
+ metadata: MetadataGetter<TMetadataResult<TMetadata>>
25
+ ];
26
+ protected onInitialize: ({ setState, getState, setMetadata, }: GlobalStoreBase.StateConfigCallbackParam<TState, TMetadata>) => void;
27
+ protected onChange: ({ getState, }: StateChangesParam<TState, TMetadata, TStateSetter>) => void;
201
28
  }
@@ -1,28 +1,19 @@
1
- import { ActionCollectionConfig, StateSetter, ActionCollectionResult, UseHookConfig, AvoidNever, UnsubscribeCallback, StateHook, StateGetter, createStateConfig, CustomGlobalHookBuilderParams, CustomGlobalHookParams, SelectorCallback, SubscribeToEmitter } from "./GlobalStore.types";
1
+ import * as GlobalStoreBase from "react-hooks-global-states";
2
+ import { createStateConfig, CustomGlobalHookParams, TMetadataResult, ActionCollectionConfig, ActionCollectionResult } from "./GlobalStore.types";
3
+ import { AvoidNever, CustomGlobalHookBuilderParams } from "react-hooks-global-states";
2
4
  /**
3
5
  * Creates a global state with the given state and config.
4
6
  * @returns {} [HOOK, DECOUPLED_GETTER, DECOUPLED_SETTER] this is an array with the hook, the decoupled getState function and the decoupled setter of the state
5
7
  */
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>];
8
+ export declare const createGlobalStateWithDecoupledFuncs: <TState, TMetadata = null, TActions extends ActionCollectionConfig<TState, TMetadata> = null>(state: TState, { actions, ...config }?: createStateConfig<TState, TMetadata, TActions>) => [hook: GlobalStoreBase.StateHook<TState, keyof TActions extends never ? GlobalStoreBase.StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TActions>, TMetadataResult<TMetadata>>, getter: GlobalStoreBase.StateGetter<TState>, setter: keyof TActions extends never ? GlobalStoreBase.StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TActions>];
7
9
  /**
8
10
  * Creates a global hook that can be used to access the state and actions across the application
9
11
  * @returns {} - () => [TState, Setter, TMetadata] the hook that can be used to access the state and the setter of the state
10
12
  */
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>;
13
+ export declare const createGlobalState: <TState, TMetadata = null, TActions extends ActionCollectionConfig<TState, TMetadata> = null>(state: TState, config?: createStateConfig<TState, TMetadata, TActions>) => GlobalStoreBase.StateHook<TState, keyof TActions extends never ? GlobalStoreBase.StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TActions>, TMetadataResult<TMetadata>>;
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>];
18
- /**
19
- * @description
20
- * Use this function to create a custom global hook which contains a fragment of the state of another hook or a fragment
21
- */
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, setter: TSetter, metadata: TMetadata];
23
- /**
24
- * @description
25
- * This function allows you to create a derivate emitter
26
- * With this approach, you can subscribe to changes in a specific fragment or subset of the state.
27
- */
28
- export declare const createDerivateEmitter: <TDerivate, TGetter extends StateGetter<unknown>, TState = Exclude<ReturnType<TGetter>, UnsubscribeCallback>>(getter: TGetter, selector: SelectorCallback<TState, TDerivate>) => SubscribeToEmitter<TDerivate>;
19
+ export declare const createCustomGlobalStateWithDecoupledFuncs: <TInheritMetadata extends Record<string, unknown>, TCustomConfig extends Record<string, unknown>>({ onInitialize, onChange, }: GlobalStoreBase.CustomGlobalHookBuilderParams<GlobalStoreBase.AvoidNever<TInheritMetadata>, GlobalStoreBase.AvoidNever<TCustomConfig>>) => <TState, TMetadata = null, TActions extends ActionCollectionConfig<TState, TMetadata> = null>(state: TState, { config: customConfig, onInit, onStateChanged, ...parameters }?: GlobalStoreBase.CustomGlobalHookParams<TCustomConfig, TState, TMetadata, TActions>) => [hook: GlobalStoreBase.StateHook<TState, keyof TActions extends never ? GlobalStoreBase.StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TActions>, TMetadataResult<TMetadata>>, getter: GlobalStoreBase.StateGetter<TState>, setter: keyof TActions extends never ? GlobalStoreBase.StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TActions>];
@@ -1,62 +1,31 @@
1
+ import * as GlobalStoreBase from "react-hooks-global-states";
2
+ import { MetadataSetter, StateSetter } from "react-hooks-global-states";
1
3
  /**
2
- * @param {StateSetter<TState>} setter - set the state
3
- * @returns {void} result - void
4
+ * Configuration of the async storage (optional)
4
5
  */
5
- export type StateSetter<TState> = (
6
- /**
7
- * @param {StateSetter<TState>} setter - set the state
8
- * @param {{ forceUpdate?: boolean }} options - Options to be passed to the setter
9
- * @param {{ forceUpdate?: boolean }} options.forceUpdate - Force the re-render of the subscribers even if the state is the same
10
- * @returns {void} result - void
11
- * */
12
- setter: TState | ((state: TState) => TState),
13
- /**
14
- * This parameter indicate whether we should force the re-render of the subscribers even if the state is the same,
15
- * Do
16
- */
17
- { forceUpdate, }?: {
6
+ export type AsyncStorageConfig = {
18
7
  /**
19
- * @deprecated forceUpdate normally should not be used inside components
20
- * Use this flag just in custom implementations of the global store
8
+ * async storage configuration
21
9
  */
22
- forceUpdate?: boolean;
23
- }) => void;
24
- /**
25
- * @description
26
- * The hook to use the global state
27
- * @returns {[State, StateSetter<TState>, TMetadata]} result - the state, the setter and the metadata
28
- */
29
- export type StateHook<TState, TSetter, TMetadata> = <State = TState>(selector?: (state: TState) => State, config?: UseHookConfig<State>) => [state: State, setter: TSetter, metadata: TMetadata];
30
- /**
31
- * @description
32
- * Type that prevent ts issues with merging never with other types
33
- */
34
- export type AvoidNever<T> = T extends never | null | undefined ? {} : T;
35
- /**
36
- * @param {TMetadata} setter - set the metadata
37
- * @returns {void} result - void
38
- */
39
- export type MetadataSetter<TMetadata> = (
40
- /**
41
- * @param {TMetadata} setter - set the metadata
42
- * @returns {void} result - void
43
- * */
44
- setter: TMetadata | ((metadata: TMetadata) => TMetadata)) => void;
45
- /**
46
- * Parameters of the onStateChanged callback function
47
- * @param {TState} state - the new state
48
- * @param {TState} previousState - the previous state
49
- **/
50
- export type StateChanges<TState> = {
51
- /**
52
- * The new state
53
- * */
54
- state: TState;
55
- /**
56
- * The previous state
57
- * */
58
- previousState?: TState;
10
+ asyncStorage?: {
11
+ /**
12
+ * The key of the async storage
13
+ */
14
+ key?: string;
15
+ /**
16
+ * The function used to encrypt the async storage, it can be a custom function or a boolean value (true = atob)
17
+ */
18
+ encrypt?: boolean | ((value: string) => string);
19
+ /**
20
+ * The function used to decrypt the async storage, it can be a custom function or a boolean value (true = atob)
21
+ */
22
+ decrypt?: boolean | ((value: string) => string);
23
+ };
59
24
  };
25
+ export type TMetadataResult<TMetadata = null> = {
26
+ isAsyncStorageReady?: boolean;
27
+ asyncStorageKey?: string;
28
+ } & Omit<NonNullable<TMetadata>, "isAsyncStorageReady" | "asyncStorageKey">;
60
29
  /**
61
30
  * Callbacks to be passed to the configurations function of the store
62
31
  * @template {TState} TState - The state type
@@ -67,34 +36,9 @@ export type StateChanges<TState> = {
67
36
  * @property {() => TMetadata} getMetadata - Get the metadata
68
37
  * @property {ActionCollectionResult<TState, TMetadata>} actions - The actions collection if any
69
38
  **/
70
- export type StoreTools<TState = any, TMetadata = any, TActions = any> = {
71
- /**
72
- * Set the metadata
73
- * @param {TMetadata} setter - The metadata or a function that will receive the metadata and return the new metadata
74
- * @returns {void} result - void
75
- * */
76
- setMetadata: MetadataSetter<TMetadata>;
77
- /**
78
- * Set the state
79
- * @param {TState} setter - The state or a function that will receive the state and return the new state
80
- * @param {{ forceUpdate?: boolean }} options - Options
81
- * @returns {void} result - void
82
- * */
83
- setState: StateSetter<TState>;
84
- /**
85
- * Get the state
86
- * @returns {TState} result - The state
87
- * */
88
- getState: StateGetter<TState>;
89
- /**
90
- * Get the metadata
91
- * @returns {TMetadata} result - The metadata
92
- * */
93
- getMetadata: () => TMetadata;
94
- /**
95
- * Actions of the hook
96
- */
97
- actions: TActions;
39
+ export type StoreTools<TState = any, TMetadata = any, TActions = any> = Omit<GlobalStoreBase.StoreTools<TState, TMetadata, TActions>, "getMetadata" | "setMetadata"> & {
40
+ getMetadata: () => TMetadataResult<TMetadata>;
41
+ setMetadata: MetadataSetter<TMetadataResult<TMetadata>> | MetadataSetter<TMetadata>;
98
42
  };
99
43
  /**
100
44
  * Basic contract for the storeActionsConfig configuration
@@ -105,7 +49,7 @@ export type StoreTools<TState = any, TMetadata = any, TActions = any> = {
105
49
  * @returns {ActionCollectionConfig<TState, TMetadata>} result - The action collection configuration
106
50
  */
107
51
  export interface ActionCollectionConfig<TState, TMetadata = null> {
108
- [key: string]: (...parameters: any[]) => (storeTools: StoreTools<TState, TMetadata>) => unknown | void;
52
+ [key: string]: (...parameters: any[]) => (storeTools: any) => unknown | void;
109
53
  }
110
54
  /**
111
55
  * This is the actions object returned by the hook when you pass an storeActionsConfig configuration
@@ -148,7 +92,7 @@ export type ActionCollectionResult<TState, TMetadata, TStateSetter extends Actio
148
92
  * @template {TStateSetter} TStateSetter - The storeActionsConfig type (optional) - if you pass an storeActionsConfig the hook will return an object with the actions
149
93
  * @template {ActionCollectionResult<TState, TStateSetter>} TStateSetter - the result of the API (optional) - if you don't pass an API as a parameter, you can pass null
150
94
  * */
151
- export type StateConfigCallbackParam<TState, TMetadata, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> = StateSetter<TState>> = {
95
+ export type StateConfigCallbackParam<TState, TMetadata, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | GlobalStoreBase.StateSetter<TState> = GlobalStoreBase.StateSetter<TState>> = {
152
96
  actions: ActionCollectionResult<TState, TMetadata, TStateSetter>;
153
97
  } & StoreTools<TState, TMetadata>;
154
98
  /**
@@ -157,7 +101,7 @@ export type StateConfigCallbackParam<TState, TMetadata, TStateSetter extends Act
157
101
  * @template {TMetadata} TMetadata - The metadata type
158
102
  * @template {TStateSetter} TStateSetter - The storeActionsConfig type (optional) - if you pass an storeActionsConfig the hook will return an object with the actions
159
103
  */
160
- export type StateChangesParam<TState, TMetadata, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> = StateSetter<TState>> = StateConfigCallbackParam<TState, TMetadata, TStateSetter> & StateChanges<TState>;
104
+ export type StateChangesParam<TState, TMetadata, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | GlobalStoreBase.StateSetter<TState> = GlobalStoreBase.StateSetter<TState>> = StateConfigCallbackParam<TState, TMetadata, TStateSetter> & GlobalStoreBase.StateChanges<TState>;
161
105
  /**
162
106
  * Configuration of the store (optional) - if you don't need to use the store configuration you don't need to pass this parameter
163
107
  * @param {StateConfigCallbackParam<TState, TMetadata> => void} onInit - callback function called when the store is initialized
@@ -168,149 +112,13 @@ export type StateChangesParam<TState, TMetadata, TStateSetter extends ActionColl
168
112
  * @template TMetadata - the type of the metadata (optional) - if you don't pass an metadata as a parameter, you can pass null
169
113
  * @template {ActionCollectionConfig<TState,TMetadata> | null} TStateSetter - the configuration of the API (optional) - if you don't pass an API as a parameter, you can pass null
170
114
  * */
171
- export type GlobalStoreConfig<TState, TMetadata, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> = StateSetter<TState>> = {
172
- /**
173
- * @param {StateConfigCallbackParam<TState, TMetadata> => void} metadata - the initial value of the metadata
174
- * */
175
- metadata?: TMetadata;
176
- /**
177
- * @param {StateConfigCallbackParam<TState, TMetadata> => void} onInit - callback function called when the store is initialized
178
- * @returns {void} result - void
179
- * */
180
- onInit?: (parameters: StateConfigCallbackParam<TState, TMetadata, TStateSetter>) => void;
181
- /**
182
- * @param {StateChangesParam<TState, TMetadata> => void} onStateChanged - callback function called every time the state is changed
183
- * @returns {void} result - void
184
- */
185
- onStateChanged?: (parameters: StateChangesParam<TState, TMetadata, TStateSetter>) => void;
186
- /**
187
- * @param {StateConfigCallbackParam<TState, TMetadata> => void} onSubscribed - callback function called every time a component is subscribed to the store
188
- * @returns {void} result - void
189
- */
190
- onSubscribed?: (parameters: StateConfigCallbackParam<TState, TMetadata, TStateSetter>) => void;
191
- /**
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
- * @returns {boolean} result - true if you want to prevent the state change, false otherwise
194
- */
195
- computePreventStateChange?: (parameters: StateChangesParam<TState, TMetadata, TStateSetter>) => boolean;
196
- } | null;
197
- export type UseHookConfig<TState> = {
198
- /**
199
- * The callback to execute when the state is changed to check if the same really changed
200
- * If the function is not provided the derived state will perform a shallow comparison
201
- */
202
- isEqual?: (current: TState, next: TState) => boolean;
203
- };
204
- /**
205
- * Callback function to unsubscribe from the store
206
- */
207
- export type UnsubscribeCallback = () => void;
208
- /**
209
- * Configuration of the subscribe callbacks
210
- */
211
- export type SubscribeCallbackConfig<TState> = UseHookConfig<TState> & {
212
- /**
213
- * By default the callback is executed immediately after the subscription
214
- */
215
- skipFirst?: boolean;
216
- };
217
- /**
218
- * Callback function to subscribe to the store changes
219
- */
220
- export type SubscribeCallback<TState> = (state: TState) => void;
221
- /**
222
- * Callback function to subscribe to the store changes from a getter
223
- */
224
- export type SubscriberCallback<TState> = (subscribe: SubscribeToEmitter<TState>) => void;
225
- /**
226
- * Callback function to get the current state of the store or to subscribe to the store changes
227
- * @template TState - the type of the state
228
- * @param {SubscriberCallback<TState> | null} callback - the callback function to subscribe to the store changes (optional)
229
- * use the methods subscribe and subscribeSelect to subscribe to the store changes
230
- * if you don't pass a callback function the hook will return the current state of the store
231
- * @returns {UnsubscribeCallback | TState} result - the state or the unsubscribe callback if you pass a callback function
232
- */
233
- export type StateGetter<TState> = <Subscription extends Subscribe | false = false>(
234
- /**
235
- * @param {SubscriberCallback<TState> | null} callback - the callback function to subscribe to the store changes (optional)
236
- * use the methods subscribe and subscribeSelect to subscribe to the store changes
237
- */
238
- callback?: Subscription extends Subscribe ? SubscriberCallback<TState> : null) => Subscription extends Subscribe ? UnsubscribeCallback : TState;
239
- export type MetadataGetter<TMetadata> = () => TMetadata;
240
- /**
241
- * Constant value type to indicate that the getter is a subscription
242
- */
243
- export type Subscribe = true;
115
+ export type GlobalStoreConfig<TState, TMetadata, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | GlobalStoreBase.StateSetter<TState> = GlobalStoreBase.StateSetter<TState>> = GlobalStoreBase.GlobalStoreConfig<TState, TMetadata, TStateSetter> & AsyncStorageConfig;
244
116
  /**
245
117
  * Configuration of the state (optional) - if you don't need to use the state configuration you don't need to pass this parameter
246
118
  */
247
- export type createStateConfig<TState, TMetadata, TActions extends ActionCollectionConfig<TState, TMetadata> | null = null> = {
248
- /**
249
- * @description
250
- * 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
251
- */
252
- actions?: TActions;
253
- } & GlobalStoreConfig<TState, TMetadata, TActions>;
254
- export type CustomGlobalHookBuilderParams<TInheritMetadata = null, TCustomConfig = {}> = {
255
- /**
256
- * @description
257
- * This function is called when the state is initialized.
258
- */
259
- onInitialize: ({ setState, setMetadata, getMetadata, getState, actions, }: StateConfigCallbackParam<any, TInheritMetadata>, config: TCustomConfig) => void;
260
- /**
261
- * @description
262
- * This function is called when the state is changed.
263
- */
264
- onChange: ({ setState, setMetadata, getMetadata, getState, actions, }: StateChangesParam<any, TInheritMetadata>, config: TCustomConfig) => void;
265
- };
119
+ export type createStateConfig<TState, TMetadata, TActions extends ActionCollectionConfig<TState, TMetadata> | null = null> = GlobalStoreBase.createStateConfig<TState, TMetadata, TActions> & AsyncStorageConfig;
266
120
  /**
267
121
  * @description
268
122
  * Configuration of the custom global hook
269
123
  */
270
- export type CustomGlobalHookParams<TCustomConfig, TState, TMetadata, TActions extends ActionCollectionConfig<TState, TMetadata> | null> = {
271
- /**
272
- * @description
273
- * Type of the configuration object that the custom hook will require or accept
274
- */
275
- config?: TCustomConfig;
276
- /**
277
- * @description
278
- * 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
279
- */
280
- actions?: TActions;
281
- } & GlobalStoreConfig<TState, TMetadata, TActions>;
282
- export type SelectorCallback<TState, TDerivate> = (state: TState) => TDerivate;
283
- /**
284
- * @description
285
- * Function to subscribe to the store changes
286
- * @returns {UnsubscribeCallback} result - Function to unsubscribe from the store
287
- */
288
- export type SubscribeToEmitter<TState> = <TParam1 extends SubscribeCallback<TState> | SelectorCallback<TState, unknown>, TResult = ReturnType<TParam1>, TConfig = TResult extends void | null | undefined | never ? SubscribeCallbackConfig<TState> : SubscribeCallbackConfig<TResult>, TParam2 extends SubscribeCallbackConfig<TState> | SubscribeCallback<TResult> = TResult extends void | null | undefined | never ? TConfig : SubscribeCallback<TResult>, TParam3 = TResult extends void | null | undefined | never ? never : TConfig>(
289
- /**
290
- * @description
291
- * The callback function to subscribe to the store changes or a selector function to derive the state
292
- */
293
- param1: TParam1,
294
- /**
295
- * @description
296
- * The configuration object or the callback function to subscribe to the store changes
297
- */
298
- param2?: TParam2,
299
- /**
300
- * @description
301
- * The configuration object
302
- */
303
- param3?: TParam3) => UnsubscribeCallback;
304
- export type SubscriberParameters = {
305
- subscriptionId: string;
306
- selector: SelectorCallback<any, any>;
307
- config: UseHookConfig<any> | SubscribeCallbackConfig<any>;
308
- currentState: unknown;
309
- callback: SubscriptionCallback;
310
- };
311
- export type SubscriptionCallback = (params: {
312
- state: unknown;
313
- }) => void;
314
- export type SetStateCallback = (parameters: {
315
- state: unknown;
316
- }) => void;
124
+ export type CustomGlobalHookParams<TCustomConfig, TState, TMetadata, TActions extends ActionCollectionConfig<TState, TMetadata> | null> = GlobalStoreBase.CustomGlobalHookParams<TCustomConfig, TState, TMetadata, TActions> & AsyncStorageConfig;
@@ -1,13 +1,16 @@
1
+ import { GlobalStoreConfig } from "./GlobalStore.types";
1
2
  /**
2
- * Shallow compare two values and return true if they are equal.
3
- * This function just compare the first level of the values.
4
- * @param value1
5
- * @param value2
6
- * @returns {boolean} true if the values are equal, false otherwise
3
+ * @description
4
+ * Get an item from local storage using the config provided.
7
5
  */
8
- export declare const shallowCompare: <T>(value1: T, value2: T) => boolean;
6
+ export declare const getAsyncStorageItem: <TState, TMetadata = null>({ config, }: {
7
+ config: GlobalStoreConfig<TState, TMetadata, import("react-hooks-global-states").StateSetter<TState>>;
8
+ }) => Promise<TState>;
9
9
  /**
10
- * Debounce a function.
10
+ * @description
11
+ * Set an item to local storage using the config provided.
11
12
  */
12
- export declare const debounce: <T extends (...args: any[]) => any>(callback: T, delay?: number) => (...args: Parameters<T>) => ReturnType<T>;
13
- export declare const uniqueId: () => string;
13
+ export declare const setAsyncStorageItem: <TState, TMetadata = null>({ item, config, }: {
14
+ config: GlobalStoreConfig<TState, TMetadata, import("react-hooks-global-states").StateSetter<TState>>;
15
+ item: TState;
16
+ }) => Promise<void>;
@@ -1,5 +1,6 @@
1
- import { StateSetter, StateConfigCallbackParam, StateChangesParam, ActionCollectionConfig, GlobalStoreConfig } from "./GlobalStore.types";
1
+ import { StateSetter } from "react-hooks-global-states";
2
2
  import { GlobalStore } from "./GlobalStore";
3
+ import { GlobalStoreConfig, StateConfigCallbackParam, StateChangesParam, ActionCollectionConfig } from "./GlobalStore.types";
3
4
  /**
4
5
  * @description
5
6
  * Use this class to extends the capabilities of the GlobalStore.