react-native-global-state-hooks 2.1.5 → 2.1.7

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 CHANGED
@@ -140,34 +140,71 @@ You could just use this code right as it is by just adding also into your projec
140
140
  ```ts
141
141
  export class GlobalStoreAsync<
142
142
  TState,
143
- TMetadata extends { readonly isAsyncStorageReady: boolean },
143
+ TMetadata extends { readonly isAsyncStorageReady: never },
144
+ // this restriction is needed to avoid the consumers to set the isAsyncStorageReady property from outside the store,
145
+ // even when the value will be ignored is better to avoid it to avoid confusion
144
146
  TStateSetter extends
145
- | ActionCollectionConfig<TState, TMetadata>
147
+ | ActionCollectionConfig<
148
+ TState,
149
+ Omit<TMetadata, 'isAsyncStorageReady'> & {
150
+ readonly isAsyncStorageReady: boolean;
151
+ }
152
+ >
146
153
  | StateSetter<TState>
147
154
  | null = StateSetter<TState>
148
- > extends GlobalStore<TState, TMetadata, TStateSetter> {
149
- protected isAsyncStorageReady: boolean = false;
150
-
151
- protected config: GlobalStoreConfig<
152
- TState,
153
- TMetadata,
154
- NonNullable<TStateSetter>
155
+ > extends GlobalStore<
156
+ TState,
157
+ Omit<TMetadata, 'isAsyncStorageReady'> & {
158
+ readonly isAsyncStorageReady: boolean;
159
+ },
160
+ TStateSetter
161
+ > {
162
+ protected config: NonNullable<
163
+ GlobalStoreConfig<
164
+ TState,
165
+ Omit<TMetadata, 'isAsyncStorageReady'> & {
166
+ readonly isAsyncStorageReady: boolean;
167
+ },
168
+ NonNullable<TStateSetter>
169
+ >
155
170
  > & {
156
- asyncStorageKey: string; // key of the async storage
171
+ asyncStorageKey: string;
157
172
  };
158
173
 
159
174
  constructor(
160
175
  state: TState,
161
- metadata: TMetadata = { isAsyncStorageReady: false } as TMetadata,
162
- setterConfig: TStateSetter | null = null,
163
- {
164
- onInit: onInitConfig,
165
- ...config
166
- }: GlobalStoreConfig<TState, TMetadata, NonNullable<TStateSetter>> & {
167
- asyncStorageKey: string; // key of the async storage
168
- }
176
+ config: GlobalStoreConfig<
177
+ TState,
178
+ TMetadata,
179
+ ActionCollectionConfig<TState, TMetadata> | StateSetter<TState>
180
+ > & {
181
+ asyncStorageKey: string;
182
+ },
183
+ setterConfig: TStateSetter | null = null
169
184
  ) {
170
- super(state, metadata, setterConfig, config);
185
+ type TConfig = NonNullable<
186
+ GlobalStoreConfig<
187
+ TState,
188
+ Omit<TMetadata, 'isAsyncStorageReady'> & {
189
+ readonly isAsyncStorageReady: boolean;
190
+ },
191
+ NonNullable<TStateSetter>
192
+ >
193
+ > & {
194
+ asyncStorageKey: string;
195
+ };
196
+
197
+ const { onInit: onInitConfig, ...configParameters } = config as TConfig;
198
+
199
+ super(state, configParameters, setterConfig as TStateSetter);
200
+
201
+ this.config = {
202
+ ...config,
203
+ metadata: {
204
+ ...configParameters.metadata,
205
+ isAsyncStorageReady: false,
206
+ },
207
+ } as TConfig;
171
208
 
172
209
  const parameters = this.getConfigCallbackParam({});
173
210
 
@@ -177,7 +214,7 @@ export class GlobalStoreAsync<
177
214
 
178
215
  /**
179
216
  * This method will be called once the store is created after the constructor,
180
- * this method is different from the onInit of the confg property and it won't be overriden
217
+ * this method is different from the onInit of the config property and it won't be overriden
181
218
  */
182
219
  protected onInit = async ({
183
220
  setState,
@@ -185,20 +222,20 @@ export class GlobalStoreAsync<
185
222
  getMetadata,
186
223
  }: StateConfigCallbackParam<
187
224
  TState,
188
- TMetadata,
225
+ Omit<TMetadata, 'isAsyncStorageReady'> & {
226
+ readonly isAsyncStorageReady: boolean;
227
+ },
189
228
  NonNullable<TStateSetter>
190
229
  >) => {
191
230
  const { asyncStorageKey } = this.config;
192
231
  const storedItem: string = await asyncStorage.getItem(asyncStorageKey);
193
232
 
194
- this.isAsyncStorageReady = true;
195
-
196
233
  setMetadata({
197
234
  ...getMetadata(),
198
235
  isAsyncStorageReady: true,
199
236
  });
200
237
 
201
- if (!storedItem) return;
238
+ if (storedItem === null) return;
202
239
 
203
240
  const jsonParsed = JSON.parse(storedItem);
204
241
  const items = formatFromStore<TState>(jsonParsed);
@@ -208,7 +245,13 @@ export class GlobalStoreAsync<
208
245
 
209
246
  protected onStateChanged = ({
210
247
  getState,
211
- }: StateChangesParam<TState, TMetadata, NonNullable<TStateSetter>>) => {
248
+ }: StateChangesParam<
249
+ TState,
250
+ Omit<TMetadata, 'isAsyncStorageReady'> & {
251
+ readonly isAsyncStorageReady: boolean;
252
+ },
253
+ NonNullable<TStateSetter>
254
+ >) => {
212
255
  const { asyncStorageKey } = this.config;
213
256
 
214
257
  const state = getState();
@@ -253,13 +296,17 @@ It's super common to have the wish or the necessity of restricting the manipulat
253
296
  ```ts
254
297
  const initialValue = 0;
255
298
 
256
- // this is not reactive information that you could also store in the async storage
257
- // upating the metadata will not trigger the onStateChanged method or any update on the components
258
- const metadata = null;
299
+ const config = {
300
+ // this is not reactive information that you could also store in the async storage
301
+ // upating the metadata will not trigger the onStateChanged method or any update on the components
302
+ metadata: null,
303
+
304
+ // The lifecycle callbacks are: onInit, onStateChanged, onSubscribed and computePreventStateChange
305
+ };
259
306
 
260
307
  const countStore = new GlobalStore(
261
308
  initialValue,
262
- metadata,
309
+ config,
263
310
  {
264
311
  log: (message: string) => (): void => {
265
312
  console.log(message);
@@ -311,7 +358,7 @@ import { GlobalStore } from 'react-native-global-state-hooks';
311
358
 
312
359
  const initialValue = 0;
313
360
 
314
- const store = new GlobalStore(0, null, null, {
361
+ const store = new GlobalStore(0, {
315
362
  onInit: async ({ setMetadata, setState }) => {
316
363
  const data = await someApiCall();
317
364
 
@@ -332,7 +379,7 @@ This method will be called every time the state is changed
332
379
  ```ts
333
380
  import { GlobalStore } from 'react-native-global-state-hooks';
334
381
 
335
- const store = new GlobalStore(0, null, null, {
382
+ const store = new GlobalStore(0, {
336
383
  onStateChanged: ({ getState }) => {
337
384
  const state = getState();
338
385
 
@@ -352,7 +399,7 @@ This method will be called every time a component is subscribed to the store
352
399
  ```ts
353
400
  import { GlobalStore } from 'react-native-global-state-hooks';
354
401
 
355
- const store = new GlobalStore(0, null, null, {
402
+ const store = new GlobalStore(0, {
356
403
  onSubscribed: ({ getState }) => {
357
404
  console.log('A component was subscribed to the store');
358
405
  },
@@ -370,7 +417,7 @@ This method will be called every time the state is going to be changed, if it re
370
417
  ```ts
371
418
  import { GlobalStore } from 'react-native-global-state-hooks';
372
419
 
373
- const store = new GlobalStore(0, null, null, {
420
+ const store = new GlobalStore(0, {
374
421
  computePreventStateChange: ({ getState }) => {
375
422
  const state = getState();
376
423
  const shouldPrevent = state < 0;
@@ -1,10 +1,6 @@
1
+ export * from 'json-storage-formatter';
1
2
  import { Dispatch, SetStateAction } from 'react';
2
3
  import { ActionCollectionConfig, StateSetter, GlobalStoreConfig, ActionCollectionResult, StateConfigCallbackParam } from './GlobalStore.types';
3
- export declare const clone: <T>(obj: T) => T;
4
- export declare const formatFromStore: <T = unknown>(value: unknown) => T;
5
- export declare const formatToStore: <TValue, TStringify extends boolean = false>(value: TValue, { stringify }?: {
6
- stringify: TStringify;
7
- }) => TStringify extends true ? string : unknown;
8
4
  /**
9
5
  * The GlobalStore class is the main class of the library and it is used to create a GlobalStore instances
10
6
  * @template {TState} TState - The type of the state object
@@ -13,14 +9,24 @@ export declare const formatToStore: <TValue, TStringify extends boolean = false>
13
9
  * */
14
10
  export declare class GlobalStore<TState, TMetadata = null, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> | null = StateSetter<TState>> {
15
11
  protected state: TState;
16
- protected metadata: TMetadata;
17
12
  protected setterConfig: TStateSetter | null;
18
- protected config: GlobalStoreConfig<TState, TMetadata, TStateSetter>;
19
13
  /**
20
14
  * list of all the subscribers setState functions
21
15
  * @template {TState} TState - The type of the state object
22
16
  * */
23
17
  subscribers: Set<StateSetter<TState>>;
18
+ /**
19
+ * additional configuration for the store
20
+ * @template {TState} TState - The type of the state object
21
+ * @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
22
+ * @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
23
+ * @property {GlobalStoreConfig<TState, TMetadata, TStateSetter>} config.metadata - The metadata to pass to the callbacks (optional) (default: null)
24
+ * @property {GlobalStoreConfig<TState, TMetadata, TStateSetter>} config.onInit - The callback to execute when the store is initialized (optional) (default: null)
25
+ * @property {GlobalStoreConfig<TState, TMetadata, TStateSetter>} config.onStateChanged - The callback to execute when the state is changed (optional) (default: null)
26
+ * @property {GlobalStoreConfig<TState, TMetadata, TStateSetter>} config.onSubscribed - The callback to execute when a component is subscribed to the store (optional) (default: null)
27
+ * @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)
28
+ */
29
+ protected config: GlobalStoreConfig<TState, TMetadata, TStateSetter>;
24
30
  /**
25
31
  * execute once the store is created
26
32
  * @template {TState} TState - The type of the state object
@@ -75,22 +81,14 @@ export declare class GlobalStore<TState, TMetadata = null, TStateSetter extends
75
81
  * @param {TState} state - The initial state
76
82
  * */
77
83
  constructor(state: TState);
78
- /**
79
- * Create a simple global store with also contains a metadata object
80
- * The metadata object is not reactive, but it can be used to share information with the subscribers
81
- * @param {TState} state - The initial state
82
- * @param {TMetadata} metadata - The metadata object (optional) (default: null) no reactive information set to share with the subscribers
83
- * */
84
- constructor(state: TState, metadata: TMetadata);
85
84
  /**
86
85
  * Create a new global store with custom action
87
86
  * The metadata object could be null if not needed
88
87
  * The setter Object is used to define the actions that will be used to manipulate the state
89
88
  * @param {TState} state - The initial state
90
- * @param {TMetadata} metadata - The metadata object (optional) (default: null) no reactive information set to share with the subscribers
91
89
  * @param {TStateSetter} setterConfig - The actions configuration object (optional) (default: null) if not null the store manipulation will be done through the actions
92
90
  * */
93
- constructor(state: TState, metadata: TMetadata, setterConfig: TStateSetter);
91
+ constructor(state: TState, config: GlobalStoreConfig<TState, TMetadata, TStateSetter>);
94
92
  /**
95
93
  * Create a new global store with custom action
96
94
  * The metadata object could be null if not needed
@@ -98,15 +96,15 @@ export declare class GlobalStore<TState, TMetadata = null, TStateSetter extends
98
96
  * The config object is used to define the callbacks that will be executed during the store lifecycle
99
97
  * The lifecycle callbacks are: onInit, onStateChanged, onSubscribed and computePreventStateChange
100
98
  * @param {TState} state - The initial state
101
- * @param {TMetadata} metadata - The metadata object (optional) (default: null) no reactive information set to share with the subscribers
102
- * @param {TStateSetter} setterConfig - The actions configuration object (optional) (default: null) if not null the store manipulation will be done through the actions
103
99
  * @param {GlobalStoreConfig<TState, TMetadata>} config - The configuration object (optional) (default: { metadata: null })
104
- * @param {StateConfigCallbackParam<TState, TMetadata>} config.onInit - The callback to execute when the store is initialized (optional) (default: null)
105
- * @param {StateConfigCallbackParam<TState, TMetadata>} config.onStateChanged - The callback to execute when the state is changed (optional) (default: null)
106
- * @param {StateConfigCallbackParam<TState, TMetadata>} config.onSubscribed - The callback to execute when a subscriber is added (optional) (default: null)
107
- * @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)
100
+ * @param {GlobalStoreConfig<TState, TMetadata>} config.metadata - The metadata object (optional) (default: null) if not null the metadata object will be reactive
101
+ * @param {GlobalStoreConfig<TState, TMetadata>} config.onInit - The callback to execute when the store is initialized (optional) (default: null)
102
+ * @param {GlobalStoreConfig<TState, TMetadata>} config.onStateChanged - The callback to execute when the state is changed (optional) (default: null)
103
+ * @param {GlobalStoreConfig<TState, TMetadata>} config.onSubscribed - The callback to execute when a new component gets subscribed to the store (optional) (default: null)
104
+ * @param {GlobalStoreConfig<TState, TMetadata>} config.computePreventStateChange - The callback to execute everytime a state change is triggered and before the state is updated, it allows to prevent the state change by returning true (optional) (default: null)
105
+ * @param {TStateSetter} setterConfig - The actions configuration object (optional) (default: null) if not null the store manipulation will be done through the actions
108
106
  * */
109
- constructor(state: TState, metadata: TMetadata, setterConfig: TStateSetter, config: GlobalStoreConfig<TState, TMetadata, TStateSetter>);
107
+ constructor(state: TState, config: GlobalStoreConfig<TState, TMetadata, TStateSetter>, setterConfig: TStateSetter);
110
108
  protected onInitializeStore: () => void;
111
109
  /**
112
110
  * gets a clone of the state
@@ -1 +1 @@
1
- {"version":3,"file":"GlobalStore.d.ts","sourceRoot":"","sources":["../src/GlobalStore.ts"],"names":[],"mappings":"AAEA,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;AAU7B,eAAO,MAAM,KAAK,kBAA6B,CAAC;AAChD,eAAO,MAAM,eAAe,oCAAuC,CAAC;AACpE,eAAO,MAAM,aAAa;;gDAAqC,CAAC;AAEhE;;;;;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;IAuI5B,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;IAxItE;;;SAGK;IACE,WAAW,EAAE,GAAG,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAa;IAEzD;;;;;;;;;;SAUK;IACL,SAAS,CAAC,MAAM,CAAC,EAAE,iBAAiB,CAClC,MAAM,EACN,SAAS,EACT,YAAY,CACb,CAAC,QAAQ,CAAC,CAAQ;IAEnB;;;;;;;;;;SAUK;IACL,SAAS,CAAC,cAAc,CAAC,EAAE,iBAAiB,CAC1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IT,MAAM;0BACK,cAAc,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;eAc9D;IAEF;;;SAGK;IACL,SAAS,CAAC,WAAW,EAAE,WAAW,CAAC,SAAS,CAAC,CAW3C;IAEF;;;;;;SAMK;IACL,SAAS,CAAC,sBAAsB;0BAGZ,cAAc,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;UAC5D,yBAAyB,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,CAsB3D;IAEF;;;SAGK;IACE,OAAO,4JA8BZ;IAEF;;;SAGK;IACE,gBAAgB,eAMb,MAAM,kIAIN,SAAS,EAEjB;IAEF;;;;SAIK;IACL,SAAS,CAAC,kBAAkB;0BAGR,cAAc,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;UACvD,YAAY,MAAM,CAAC,CAM1B;IAEF;;;;SAIK;IACL,SAAS,CAAC,oBAAoB,CAC5B,eAAe,CAAC,EAAE,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;IAWhE;;;SAGK;IACL,SAAS,CAAC,iBAAiB,QAAO,OAAO,CAgBvC;IAEF;;;;;;OAMG;IACH,SAAS,CAAC,eAAe;gBAIf,YAAY,MAAM,CAAC;0BACT,cAAc,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;eAwD9D;IAEF;;;;SAIK;IACL,SAAS,CAAC,kBAAkB;0BAGR,cAAc,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;UAC5D,uBAAuB,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,CAgDzD;CACH;AAED,eAAe,WAAW,CAAC"}
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GlobalStore = exports.formatToStore = exports.formatFromStore = exports.clone = void 0;
3
+ exports.GlobalStore = void 0;
4
4
  const tslib_1 = require("tslib");
5
- // jsonStorageFormatter
6
- const jsonStorageFormatter = tslib_1.__importStar(require("json-storage-formatter"));
5
+ tslib_1.__exportStar(require("json-storage-formatter"), exports);
6
+ const json_storage_formatter_1 = require("json-storage-formatter");
7
7
  const react_1 = require("react");
8
8
  const throwWrongKeyOnActionCollectionConfig = (action_key) => {
9
9
  throw new Error(`[WRONG CONFIGURATION!]: Every key inside the storeActionsConfig must be a higher order function that returns a function \n[${action_key}]: key is not a valid function, try something like this: \n{\n
@@ -12,9 +12,6 @@ const throwWrongKeyOnActionCollectionConfig = (action_key) => {
12
12
  }\n
13
13
  }\n`);
14
14
  };
15
- exports.clone = jsonStorageFormatter.clone;
16
- exports.formatFromStore = jsonStorageFormatter.formatFromStore;
17
- exports.formatToStore = jsonStorageFormatter.formatToStore;
18
15
  /**
19
16
  * The GlobalStore class is the main class of the library and it is used to create a GlobalStore instances
20
17
  * @template {TState} TState - The type of the state object
@@ -25,24 +22,35 @@ class GlobalStore {
25
22
  /**
26
23
  * Create a new instance of the GlobalStore
27
24
  * @param {TState} state - The initial state
28
- * @param {TMetadata} metadata - The metadata object (optional) (default: null) no reactive information set to share with the subscribers
29
- * @param {TStateSetter} setterConfig - The actions configuration object (optional) (default: null) if not null the store manipulation will be done through the actions
30
25
  * @param {GlobalStoreConfig<TState, TMetadata>} config - The configuration object (optional) (default: { metadata: null })
31
- * @param {StateConfigCallbackParam<TState, TMetadata>} config.onInit - The callback to execute when the store is initialized (optional) (default: null)
32
- * @param {StateConfigCallbackParam<TState, TMetadata>} config.onStateChanged - The callback to execute when the state is changed (optional) (default: null)
33
- * @param {StateConfigCallbackParam<TState, TMetadata>} config.onSubscribed - The callback to execute when a subscriber is added (optional) (default: null)
34
- * @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)
26
+ * @param {GlobalStoreConfig<TState, TMetadata>} config.metadata - The metadata object (optional) (default: null) if not null the metadata object will be reactive
27
+ * @param {GlobalStoreConfig<TState, TMetadata>} config.onInit - The callback to execute when the store is initialized (optional) (default: null)
28
+ * @param {GlobalStoreConfig<TState, TMetadata>} config.onStateChanged - The callback to execute when the state is changed (optional) (default: null)
29
+ * @param {GlobalStoreConfig<TState, TMetadata>} config.onSubscribed - The callback to execute when a new component gets subscribed to the store (optional) (default: null)
30
+ * @param {GlobalStoreConfig<TState, TMetadata>} config.computePreventStateChange - The callback to execute everytime a state change is triggered and before the state is updated, it allows to prevent the state change by returning true (optional) (default: null) * @param {TStateSetter} setterConfig - The actions configuration object (optional) (default: null) if not null the store manipulation will be done through the actions
35
31
  * */
36
- constructor(state, metadata = null, setterConfig = null, config = {}) {
32
+ constructor(state, config = {}, setterConfig = null) {
37
33
  this.state = state;
38
- this.metadata = metadata;
39
34
  this.setterConfig = setterConfig;
40
- this.config = config;
41
35
  /**
42
36
  * list of all the subscribers setState functions
43
37
  * @template {TState} TState - The type of the state object
44
38
  * */
45
39
  this.subscribers = new Set();
40
+ /**
41
+ * additional configuration for the store
42
+ * @template {TState} TState - The type of the state object
43
+ * @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
44
+ * @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
45
+ * @property {GlobalStoreConfig<TState, TMetadata, TStateSetter>} config.metadata - The metadata to pass to the callbacks (optional) (default: null)
46
+ * @property {GlobalStoreConfig<TState, TMetadata, TStateSetter>} config.onInit - The callback to execute when the store is initialized (optional) (default: null)
47
+ * @property {GlobalStoreConfig<TState, TMetadata, TStateSetter>} config.onStateChanged - The callback to execute when the state is changed (optional) (default: null)
48
+ * @property {GlobalStoreConfig<TState, TMetadata, TStateSetter>} config.onSubscribed - The callback to execute when a component is subscribed to the store (optional) (default: null)
49
+ * @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)
50
+ */
51
+ this.config = {
52
+ metadata: null,
53
+ };
46
54
  /**
47
55
  * execute once the store is created
48
56
  * @template {TState} TState - The type of the state object
@@ -105,12 +113,12 @@ class GlobalStore {
105
113
  * gets a clone of the state
106
114
  * @returns {TState} - The state clone
107
115
  * */
108
- this.getStateClone = () => jsonStorageFormatter.clone(this.state);
116
+ this.getStateClone = () => (0, json_storage_formatter_1.clone)(this.state);
109
117
  /**
110
118
  * gets a clone of the metadata
111
119
  * @returns {TMetadata} - The metadata clone
112
120
  * */
113
- this.getMetadataClone = () => jsonStorageFormatter.clone(this.metadata);
121
+ this.getMetadataClone = () => { var _a, _b; return (0, json_storage_formatter_1.clone)((_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.metadata) !== null && _b !== void 0 ? _b : null); };
114
122
  /**
115
123
  * set the state and update all the subscribers
116
124
  * @param {StateSetter<TState>} setter - The setter function or the value to set
@@ -133,11 +141,12 @@ class GlobalStore {
133
141
  * @param {StateSetter<TMetadata>} setter - The setter function or the value to set
134
142
  * */
135
143
  this.setMetadata = (setter) => {
144
+ var _a;
136
145
  const isSetterFunction = typeof setter === 'function';
137
146
  const metadata = isSetterFunction
138
147
  ? setter(this.getMetadataClone())
139
148
  : setter;
140
- this.metadata = metadata;
149
+ this.config = Object.assign(Object.assign({}, ((_a = this.config) !== null && _a !== void 0 ? _a : {})), { metadata });
141
150
  };
142
151
  /**
143
152
  * get the parameters object to pass to the callback functions (onInit, onStateChanged, onSubscribed, computePreventStateChange)
@@ -295,6 +304,7 @@ class GlobalStore {
295
304
  } })), {});
296
305
  return actions;
297
306
  };
307
+ this.config = Object.assign({ metadata: null }, (config !== null && config !== void 0 ? config : {}));
298
308
  this.onInitializeStore();
299
309
  }
300
310
  /**
@@ -100,9 +100,10 @@ export type StateChangesParam<TState, TMetadata, TStateSetter extends ActionColl
100
100
  * @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
101
101
  * */
102
102
  export type GlobalStoreConfig<TState, TMetadata, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> = StateSetter<TState>> = {
103
+ metadata?: TMetadata;
103
104
  onInit?: (parameters: StateConfigCallbackParam<TState, TMetadata, TStateSetter>) => void;
104
105
  onStateChanged?: (parameters: StateChangesParam<TState, TMetadata, TStateSetter>) => void;
105
106
  onSubscribed?: (parameters: StateConfigCallbackParam<TState, TMetadata, TStateSetter>) => void;
106
107
  computePreventStateChange?: (parameters: StateChangesParam<TState, TMetadata, TStateSetter>) => boolean;
107
- };
108
+ } | null;
108
109
  //# sourceMappingURL=GlobalStore.types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"GlobalStore.types.d.ts","sourceRoot":"","sources":["../src/GlobalStore.types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,WAAW,CAAC,MAAM,IAAI,CAChC,MAAM,EAAE,MAAM,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC,KACzC,IAAI,CAAC;AAEV;;;;IAII;AACJ,MAAM,MAAM,YAAY,CAAC,MAAM,IAAI;IACjC,KAAK,EAAE,MAAM,CAAC;IACd,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF;;;;;;;;IAQI;AACJ,MAAM,MAAM,UAAU,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,IAAI;IACjD,WAAW,EAAE,WAAW,CAAC,SAAS,CAAC,CAAC;IACpC,QAAQ,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAC9B,QAAQ,EAAE,MAAM,MAAM,CAAC;IACvB,WAAW,EAAE,MAAM,SAAS,CAAC;CAC9B,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,WAAW,sBAAsB,CAAC,MAAM,EAAE,SAAS;IACvD,CAAC,GAAG,EAAE,MAAM,GAAG,CACb,GAAG,UAAU,EAAE,GAAG,EAAE,KACjB,CAAC,UAAU,EAAE,UAAU,CAAC,MAAM,EAAE,SAAS,CAAC,KAAK,OAAO,GAA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,IAC3C,wBAAwB,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,GAC3D,YAAY,CAAC,MAAM,CAAC,CAAC;AAEvB;;;;;;;;;KASK;AACL,MAAM,MAAM,iBAAiB,CAC3B,MAAM,EACN,SAAS,EACT,YAAY,SACR,sBAAsB,CAAC,MAAM,EAAE,SAAS,CAAC,GACzC,WAAW,CAAC,MAAM,CAAC,GAAG,WAAW,CAAC,MAAM,CAAC,IAC3C;IACF,QAAQ,CAAC,EAAE,SAAS,CAAC;IAErB,MAAM,CAAC,EAAE,CACP,UAAU,EAAE,wBAAwB,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,KAClE,IAAI,CAAC;IAEV,cAAc,CAAC,EAAE,CACf,UAAU,EAAE,iBAAiB,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,KAC3D,IAAI,CAAC;IAEV,YAAY,CAAC,EAAE,CACb,UAAU,EAAE,wBAAwB,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,KAClE,IAAI,CAAC;IAEV,yBAAyB,CAAC,EAAE,CAC1B,UAAU,EAAE,iBAAiB,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,KAC3D,OAAO,CAAC;CACd,GAAG,IAAI,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-global-state-hooks",
3
- "version": "2.1.5",
3
+ "version": "2.1.7",
4
4
  "description": "This is a package to easily handling global-state across your react-native-components No-redux",
5
5
  "main": "lib/GlobalStore.js",
6
6
  "files": [