react-native-global-state-hooks 2.1.0 → 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
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
# react-native-global-state-hooks
|
|
2
2
|
|
|
3
|
-
This is a package to easily handling global-state across your react-native-components
|
|
3
|
+
This is a package to easily handling global-state across your react-native-components
|
|
4
4
|
|
|
5
|
-
This utility
|
|
5
|
+
This utility uses the **useState** hook within a subscription pattern and **HOFs** to create a more intuitive, atomic and easy way of sharing state between components
|
|
6
6
|
|
|
7
7
|
...
|
|
8
8
|
|
|
9
9
|
...
|
|
10
10
|
|
|
11
|
-
# Creating a global store
|
|
11
|
+
# Creating a global store
|
|
12
12
|
|
|
13
13
|
We are gonna create a global count example **useCountGlobal.ts**:
|
|
14
14
|
|
|
@@ -148,15 +148,31 @@ export class GlobalStoreAsync<
|
|
|
148
148
|
> extends GlobalStore<TState, TMetadata, TStateSetter> {
|
|
149
149
|
protected isAsyncStorageReady: boolean = false;
|
|
150
150
|
|
|
151
|
+
protected config: GlobalStoreConfig<
|
|
152
|
+
TState,
|
|
153
|
+
TMetadata,
|
|
154
|
+
NonNullable<TStateSetter>
|
|
155
|
+
> & {
|
|
156
|
+
asyncStorageKey: string; // key of the async storage
|
|
157
|
+
};
|
|
158
|
+
|
|
151
159
|
constructor(
|
|
152
160
|
state: TState,
|
|
153
161
|
metadata: TMetadata = { isAsyncStorageReady: false } as TMetadata,
|
|
154
162
|
setterConfig: TStateSetter | null = null,
|
|
155
|
-
|
|
163
|
+
{
|
|
164
|
+
onInit: onInitConfig,
|
|
165
|
+
...config
|
|
166
|
+
}: GlobalStoreConfig<TState, TMetadata, NonNullable<TStateSetter>> & {
|
|
156
167
|
asyncStorageKey: string; // key of the async storage
|
|
157
168
|
}
|
|
158
169
|
) {
|
|
159
170
|
super(state, metadata, setterConfig, config);
|
|
171
|
+
|
|
172
|
+
const parameters = this.getConfigCallbackParam({});
|
|
173
|
+
|
|
174
|
+
this.onInit(parameters);
|
|
175
|
+
onInitConfig?.(parameters);
|
|
160
176
|
}
|
|
161
177
|
|
|
162
178
|
/**
|
|
@@ -172,7 +188,8 @@ export class GlobalStoreAsync<
|
|
|
172
188
|
TMetadata,
|
|
173
189
|
NonNullable<TStateSetter>
|
|
174
190
|
>) => {
|
|
175
|
-
const
|
|
191
|
+
const { asyncStorageKey } = this.config;
|
|
192
|
+
const storedItem: string = await asyncStorage.getItem(asyncStorageKey);
|
|
176
193
|
|
|
177
194
|
this.isAsyncStorageReady = true;
|
|
178
195
|
|
|
@@ -193,10 +210,11 @@ export class GlobalStoreAsync<
|
|
|
193
210
|
getState,
|
|
194
211
|
}: StateChangesParam<TState, TMetadata, NonNullable<TStateSetter>>) => {
|
|
195
212
|
const state = getState();
|
|
196
|
-
const formattedObject
|
|
213
|
+
const formattedObject = formatToStore(state);
|
|
197
214
|
const jsonValue = JSON.stringify(formattedObject);
|
|
215
|
+
const { asyncStorageKey } = this.config;
|
|
198
216
|
|
|
199
|
-
asyncStorage.setItem(
|
|
217
|
+
asyncStorage.setItem(asyncStorageKey, jsonValue);
|
|
200
218
|
};
|
|
201
219
|
}
|
|
202
220
|
```
|
package/lib/GlobalStore.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export declare class GlobalStore<TState, TMetadata = null, TStateSetter extends
|
|
|
10
10
|
protected state: TState;
|
|
11
11
|
protected metadata: TMetadata;
|
|
12
12
|
protected setterConfig: TStateSetter | null;
|
|
13
|
-
|
|
13
|
+
protected config: GlobalStoreConfig<TState, TMetadata, TStateSetter>;
|
|
14
14
|
/**
|
|
15
15
|
* list of all the subscribers setState functions
|
|
16
16
|
* @template {TState} TState - The type of the state object
|
package/lib/GlobalStore.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GlobalStore.d.ts","sourceRoot":"","sources":["../src/GlobalStore.ts"],"names":[],"mappings":"AAAA,OAA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AAO,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,10 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
3
|
-
/**
|
|
4
|
-
* Debounce function to prevent multiple calls to the same function in a short period of time
|
|
5
|
-
* @param callback Function to be called
|
|
6
|
-
* @param wait Time to wait before calling the function
|
|
7
|
-
* @returns Function to be called
|
|
8
|
-
*/
|
|
9
|
-
export declare const debounce: <T extends Function>(callback: T, wait?: number) => T;
|
|
1
|
+
import { clone } from 'json-storage-formatter';
|
|
2
|
+
export { clone };
|
|
10
3
|
//# sourceMappingURL=GlobalStore.utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GlobalStore.utils.d.ts","sourceRoot":"","sources":["../src/GlobalStore.utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"GlobalStore.utils.d.ts","sourceRoot":"","sources":["../src/GlobalStore.utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAC;AAG/C,OAAO,EAAE,KAAK,EAAE,CAAC"}
|
package/lib/GlobalStore.utils.js
CHANGED
|
@@ -1,23 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.clone = void 0;
|
|
4
4
|
const json_storage_formatter_1 = require("json-storage-formatter");
|
|
5
|
-
Object.defineProperty(exports, "formatToStore", { enumerable: true, get: function () { return json_storage_formatter_1.formatToStore; } });
|
|
6
|
-
Object.defineProperty(exports, "formatFromStore", { enumerable: true, get: function () { return json_storage_formatter_1.formatFromStore; } });
|
|
7
5
|
Object.defineProperty(exports, "clone", { enumerable: true, get: function () { return json_storage_formatter_1.clone; } });
|
|
8
|
-
/**
|
|
9
|
-
* Debounce function to prevent multiple calls to the same function in a short period of time
|
|
10
|
-
* @param callback Function to be called
|
|
11
|
-
* @param wait Time to wait before calling the function
|
|
12
|
-
* @returns Function to be called
|
|
13
|
-
*/
|
|
14
|
-
const debounce = (callback, wait = 300) => {
|
|
15
|
-
let timer;
|
|
16
|
-
return ((...args) => {
|
|
17
|
-
clearTimeout(timer);
|
|
18
|
-
timer = setTimeout(() => {
|
|
19
|
-
callback(...args);
|
|
20
|
-
}, wait);
|
|
21
|
-
});
|
|
22
|
-
};
|
|
23
|
-
exports.debounce = debounce;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-global-state-hooks",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.1",
|
|
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": [
|
|
@@ -65,6 +65,6 @@
|
|
|
65
65
|
"react": "workspace:*"
|
|
66
66
|
},
|
|
67
67
|
"dependencies": {
|
|
68
|
-
"json-storage-formatter": "^1.0.
|
|
68
|
+
"json-storage-formatter": "^1.0.3"
|
|
69
69
|
}
|
|
70
70
|
}
|