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

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
@@ -209,12 +209,14 @@ export class GlobalStoreAsync<
209
209
  protected onStateChanged = ({
210
210
  getState,
211
211
  }: StateChangesParam<TState, TMetadata, NonNullable<TStateSetter>>) => {
212
- const state = getState();
213
- const formattedObject = formatToStore(state);
214
- const jsonValue = JSON.stringify(formattedObject);
215
212
  const { asyncStorageKey } = this.config;
216
213
 
217
- asyncStorage.setItem(asyncStorageKey, jsonValue);
214
+ const state = getState();
215
+ const formattedObject = formatToStore(state, {
216
+ stringify: true,
217
+ });
218
+
219
+ asyncStorage.setItem(asyncStorageKey, formattedObject);
218
220
  };
219
221
  }
220
222
  ```
@@ -223,6 +225,14 @@ The methods **formatToStore** and **formatFromStore** are part of another librar
223
225
 
224
226
  this super small library will help you to transform objects into json string and get them back without losing any of the original data types... no more problems when **stringify** Dates, Maps, Sets, etc...
225
227
 
228
+ ## How to use the **GlobalStoreAsync**
229
+
230
+ It will work exactly the same as the **GlobalStore**, the main difference is that by default the metadata object will include the {**isAsyncStorageReady** } which will allow you to know if the async data was already retrieved.
231
+
232
+ ```ts
233
+ const [count, setCount, { isAsyncStorageReady }] = useCountGlobal();
234
+ ```
235
+
226
236
  ...
227
237
 
228
238
  Originally the library was implementing persistent storage by using the package **@react-native-async-storage**, but not all people want to use it or need to use it... so it has been removed. Feel free to use the above example to get back that functionality if you were using the previous versions of the package.
@@ -1,5 +1,10 @@
1
1
  import { Dispatch, SetStateAction } from 'react';
2
2
  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;
3
8
  /**
4
9
  * The GlobalStore class is the main class of the library and it is used to create a GlobalStore instances
5
10
  * @template {TState} TState - The type of the state object
@@ -1 +1 @@
1
- {"version":3,"file":"GlobalStore.d.ts","sourceRoot":"","sources":["../src/GlobalStore.ts"],"names":[],"mappings":"AACA,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;;;;;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,CAC1C,MAAM,EACN,SAAS,EACT,YAAY,CACb,CAAC,gBAAgB,CAAC,CAAQ;IAE3B;;;;;;;;;;SAUK;IACL,SAAS,CAAC,YAAY,CAAC,EAAE,iBAAiB,CACxC,MAAM,EACN,SAAS,EACT,YAAY,CACb,CAAC,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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,CAQ3C;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,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GlobalStore = void 0;
4
- const json_storage_formatter_1 = require("json-storage-formatter");
3
+ exports.GlobalStore = exports.formatToStore = exports.formatFromStore = exports.clone = void 0;
4
+ const tslib_1 = require("tslib");
5
+ // jsonStorageFormatter
6
+ const jsonStorageFormatter = tslib_1.__importStar(require("json-storage-formatter"));
5
7
  const react_1 = require("react");
6
8
  const throwWrongKeyOnActionCollectionConfig = (action_key) => {
7
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
@@ -10,6 +12,9 @@ const throwWrongKeyOnActionCollectionConfig = (action_key) => {
10
12
  }\n
11
13
  }\n`);
12
14
  };
15
+ exports.clone = jsonStorageFormatter.clone;
16
+ exports.formatFromStore = jsonStorageFormatter.formatFromStore;
17
+ exports.formatToStore = jsonStorageFormatter.formatToStore;
13
18
  /**
14
19
  * The GlobalStore class is the main class of the library and it is used to create a GlobalStore instances
15
20
  * @template {TState} TState - The type of the state object
@@ -100,12 +105,12 @@ class GlobalStore {
100
105
  * gets a clone of the state
101
106
  * @returns {TState} - The state clone
102
107
  * */
103
- this.getStateClone = () => (0, json_storage_formatter_1.clone)(this.state);
108
+ this.getStateClone = () => jsonStorageFormatter.clone(this.state);
104
109
  /**
105
110
  * gets a clone of the metadata
106
111
  * @returns {TMetadata} - The metadata clone
107
112
  * */
108
- this.getMetadataClone = () => (0, json_storage_formatter_1.clone)(this.metadata);
113
+ this.getMetadataClone = () => jsonStorageFormatter.clone(this.metadata);
109
114
  /**
110
115
  * set the state and update all the subscribers
111
116
  * @param {StateSetter<TState>} setter - The setter function or the value to set
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-global-state-hooks",
3
- "version": "2.1.3",
3
+ "version": "2.1.5",
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": [