react-native-global-state-hooks 2.0.4 → 2.0.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/lib/GlobalStore.d.ts +2 -5
- package/lib/GlobalStore.d.ts.map +1 -1
- package/lib/GlobalStore.js +16 -77
- package/package.json +6 -2
package/lib/GlobalStore.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
/// <reference types="lodash" />
|
|
2
1
|
import * as IGlobalStore from './GlobalStoreTypes';
|
|
3
|
-
export declare const isPrimitive: <T>(value: T) => boolean;
|
|
4
2
|
export declare type IValueWithMedaData = {
|
|
5
3
|
_type_?: 'map' | 'set' | 'date';
|
|
6
4
|
value?: unknown;
|
|
7
5
|
};
|
|
6
|
+
export declare const debounce: <T extends Function>(callback: T, wait?: number) => T;
|
|
8
7
|
export declare class GlobalStore<IState, IPersist extends string | null, IActions extends IGlobalStore.IActionCollectionConfig<IState> | null = null> implements IGlobalStore.IGlobalState<IState, IPersist, IActions> {
|
|
9
8
|
protected state: IState;
|
|
10
9
|
protected actions: IActions;
|
|
@@ -22,8 +21,6 @@ export declare class GlobalStore<IState, IPersist extends string | null, IAction
|
|
|
22
21
|
onPersistStorageLoad?: (obj: unknown) => (IState | null));
|
|
23
22
|
private get isStoredStateItemUpdated();
|
|
24
23
|
private storedStateItem;
|
|
25
|
-
protected formatItemFromStore<T>(_obj: T): unknown;
|
|
26
|
-
protected formatToStore<T>(obj: T): unknown;
|
|
27
24
|
protected getAsyncStoreItemPromise: Promise<IState> | null;
|
|
28
25
|
protected asyncStorageGetItem(): Promise<string | null>;
|
|
29
26
|
protected getAsyncStoreItem(): Promise<IState>;
|
|
@@ -45,7 +42,7 @@ export declare class GlobalStore<IState, IPersist extends string | null, IAction
|
|
|
45
42
|
/**
|
|
46
43
|
* React native cannot use unstable_batchedUpdates, it does not have any effect
|
|
47
44
|
*/
|
|
48
|
-
static ExecutePendingBatches:
|
|
45
|
+
static ExecutePendingBatches: () => void;
|
|
49
46
|
protected getActions: <IApi extends IGlobalStore.IActionCollectionResult<IState, IGlobalStore.IActionCollectionConfig<IState>>>() => IApi;
|
|
50
47
|
deleteAsyncStoreItem: () => Promise<void>;
|
|
51
48
|
}
|
package/lib/GlobalStore.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GlobalStore.d.ts","sourceRoot":"","sources":["../src/GlobalStore.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"GlobalStore.d.ts","sourceRoot":"","sources":["../src/GlobalStore.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,YAAY,MAAM,oBAAoB,CAAC;AAEnD,oBAAY,kBAAkB,GAAG;IAC/B,MAAM,CAAC,EAAE,KAAK,GAAG,KAAK,GAAG,MAAM,CAAC;IAChC,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAA;AAED,eAAO,MAAM,QAAQ,uDAUpB,CAAC;AAEF,qBAAa,WAAW,CACtB,MAAM,EACN,QAAQ,SAAS,MAAM,GAAG,IAAI,EAC9B,QAAQ,SAAS,YAAY,CAAC,uBAAuB,CAAC,MAAM,CAAC,GAAG,IAAI,GAAG,IAAI,CAC3E,YAAW,YAAY,CAAC,YAAY,CAAC,MAAM,EAAE,QAAQ,EAAE,QAAQ,CAAC;IAS9D,SAAS,CAAC,KAAK,EAAE,MAAM;IACvB,SAAS,CAAC,OAAO,EAAE,QAAQ;IACpB,cAAc,EAAE,QAAQ;IAE/B;;MAEE;IACK,oBAAoB,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC;IAdzD,WAAW,EAAE,YAAY,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAM;IAE5D,IAAW,cAAc,IAAI,OAAO,CAEnC;gBAGW,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,QAA2B,EACvC,cAAc,GAAE,QAA2B;IAElD;;MAEE;IACK,oBAAoB,GA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SAAS,CAAC,UAAU,sHA0BlB;IAEK,oBAAoB,QAAa,QAAQ,IAAI,CAAC,CAIpD;CAEF;AAED,eAAe,WAAW,CAAC"}
|
package/lib/GlobalStore.js
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.GlobalStore = exports.
|
|
3
|
+
exports.GlobalStore = exports.debounce = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const react_1 = require("react");
|
|
6
|
-
const
|
|
6
|
+
const json_storage_formatter_1 = require("json-storage-formatter");
|
|
7
7
|
const async_storage_1 = (0, tslib_1.__importDefault)(require("@react-native-async-storage/async-storage"));
|
|
8
|
-
const
|
|
9
|
-
|
|
8
|
+
const debounce = (callback, wait = 300) => {
|
|
9
|
+
let timer;
|
|
10
|
+
return ((...args) => {
|
|
11
|
+
clearTimeout(timer);
|
|
12
|
+
timer = setTimeout(() => {
|
|
13
|
+
callback(...args);
|
|
14
|
+
}, wait);
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
exports.debounce = debounce;
|
|
10
18
|
class GlobalStore {
|
|
11
19
|
constructor(state, actions = null, persistStoreAs = null,
|
|
12
20
|
/**
|
|
@@ -20,7 +28,7 @@ class GlobalStore {
|
|
|
20
28
|
this.subscribers = [];
|
|
21
29
|
this.storedStateItem = undefined;
|
|
22
30
|
this.getAsyncStoreItemPromise = null;
|
|
23
|
-
this.getStateCopy = () => Object.freeze((0,
|
|
31
|
+
this.getStateCopy = () => Object.freeze((0, json_storage_formatter_1.clone)(this.state));
|
|
24
32
|
this.getHook = () => () => {
|
|
25
33
|
const [value, setter] = (0, react_1.useState)(() => this.state);
|
|
26
34
|
(0, react_1.useEffect)(() => {
|
|
@@ -98,75 +106,6 @@ class GlobalStore {
|
|
|
98
106
|
get isStoredStateItemUpdated() {
|
|
99
107
|
return this.storedStateItem !== undefined;
|
|
100
108
|
}
|
|
101
|
-
formatItemFromStore(_obj) {
|
|
102
|
-
var _a, _b;
|
|
103
|
-
const obj = _obj;
|
|
104
|
-
if ((0, exports.isPrimitive)(obj)) {
|
|
105
|
-
return obj;
|
|
106
|
-
}
|
|
107
|
-
const isMetaDate = (obj === null || obj === void 0 ? void 0 : obj._type_) === 'date';
|
|
108
|
-
if (isMetaDate) {
|
|
109
|
-
return new Date(obj.value);
|
|
110
|
-
}
|
|
111
|
-
const isMetaMap = (obj === null || obj === void 0 ? void 0 : obj._type_) === 'map';
|
|
112
|
-
if (isMetaMap) {
|
|
113
|
-
const mapData = ((_a = obj.value) !== null && _a !== void 0 ? _a : []).map(([key, item]) => [
|
|
114
|
-
key,
|
|
115
|
-
this.formatItemFromStore(item),
|
|
116
|
-
]);
|
|
117
|
-
return new Map(mapData);
|
|
118
|
-
}
|
|
119
|
-
const isMetaSet = (obj === null || obj === void 0 ? void 0 : obj._type_) === 'set';
|
|
120
|
-
if (isMetaSet) {
|
|
121
|
-
const setData = (_b = obj.value) !== null && _b !== void 0 ? _b : [].map((item) => this.formatItemFromStore(item));
|
|
122
|
-
return new Set(setData);
|
|
123
|
-
}
|
|
124
|
-
const isArray = Array.isArray(obj);
|
|
125
|
-
if (isArray) {
|
|
126
|
-
return obj.map((item) => this.formatItemFromStore(item));
|
|
127
|
-
}
|
|
128
|
-
const keys = Object.keys(obj);
|
|
129
|
-
return keys.reduce((acumulator, key) => {
|
|
130
|
-
const unformatedValue = obj[key];
|
|
131
|
-
return Object.assign(Object.assign({}, acumulator), { [key]: this.formatItemFromStore(unformatedValue) });
|
|
132
|
-
}, {});
|
|
133
|
-
}
|
|
134
|
-
formatToStore(obj) {
|
|
135
|
-
if ((0, exports.isPrimitive)(obj)) {
|
|
136
|
-
return obj;
|
|
137
|
-
}
|
|
138
|
-
const isArray = Array.isArray(obj);
|
|
139
|
-
if (isArray) {
|
|
140
|
-
return obj.map((item) => this.formatToStore(item));
|
|
141
|
-
}
|
|
142
|
-
const isMap = obj instanceof Map;
|
|
143
|
-
if (isMap) {
|
|
144
|
-
const pairs = Array.from(obj.entries());
|
|
145
|
-
return {
|
|
146
|
-
_type_: 'map',
|
|
147
|
-
value: pairs.map((pair) => this.formatToStore(pair)),
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
const isSet = obj instanceof Set;
|
|
151
|
-
if (isSet) {
|
|
152
|
-
const values = Array.from(obj.values());
|
|
153
|
-
return {
|
|
154
|
-
_type_: 'set',
|
|
155
|
-
value: values.map((value) => this.formatToStore(value)),
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
if ((0, lodash_1.isDate)(obj)) {
|
|
159
|
-
return {
|
|
160
|
-
_type_: 'date',
|
|
161
|
-
value: obj.toISOString(),
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
const keys = Object.keys(obj);
|
|
165
|
-
return keys.reduce((acumulator, key) => {
|
|
166
|
-
const value = obj[key];
|
|
167
|
-
return (Object.assign(Object.assign({}, acumulator), { [key]: this.formatToStore(value) }));
|
|
168
|
-
}, {});
|
|
169
|
-
}
|
|
170
109
|
asyncStorageGetItem() {
|
|
171
110
|
return async_storage_1.default.getItem(this.persistStoreAs);
|
|
172
111
|
}
|
|
@@ -184,7 +123,7 @@ class GlobalStore {
|
|
|
184
123
|
let value = JSON.parse(item);
|
|
185
124
|
/** This allow users to review what is been stored */
|
|
186
125
|
value = (_a = this.onPersistStorageLoad(value)) !== null && _a !== void 0 ? _a : value;
|
|
187
|
-
const newState =
|
|
126
|
+
const newState = (0, json_storage_formatter_1.formatFromStore)(value);
|
|
188
127
|
yield this.globalSetterAsync(newState);
|
|
189
128
|
}
|
|
190
129
|
resolve(this.state);
|
|
@@ -203,7 +142,7 @@ class GlobalStore {
|
|
|
203
142
|
if (this.storedStateItem === this.state)
|
|
204
143
|
return;
|
|
205
144
|
this.storedStateItem = this.state;
|
|
206
|
-
const valueToStore =
|
|
145
|
+
const valueToStore = (0, json_storage_formatter_1.formatToStore)(this.state);
|
|
207
146
|
yield this.asyncStorageSetItem(JSON.stringify(valueToStore));
|
|
208
147
|
});
|
|
209
148
|
}
|
|
@@ -231,7 +170,7 @@ GlobalStore.ExecutePendingBatchesCallbacks = [];
|
|
|
231
170
|
/**
|
|
232
171
|
* React native cannot use unstable_batchedUpdates, it does not have any effect
|
|
233
172
|
*/
|
|
234
|
-
GlobalStore.ExecutePendingBatches = (0,
|
|
173
|
+
GlobalStore.ExecutePendingBatches = (0, exports.debounce)(() => {
|
|
235
174
|
GlobalStore.batchedUpdates.forEach(([execute]) => {
|
|
236
175
|
execute();
|
|
237
176
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-global-state-hooks",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.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": [
|
|
@@ -40,6 +40,7 @@
|
|
|
40
40
|
},
|
|
41
41
|
"homepage": "https://github.com/johnny-quesada-developer/react-native-global-state-hooks#readme",
|
|
42
42
|
"devDependencies": {
|
|
43
|
+
"@react-native-async-storage/async-storage": "workspace:*",
|
|
43
44
|
"@types/jest": "^26.0.17",
|
|
44
45
|
"@types/lodash": "^4.14.165",
|
|
45
46
|
"@types/react": "^17.0.0",
|
|
@@ -54,13 +55,16 @@
|
|
|
54
55
|
"eslint-plugin-react": "^7.21.5",
|
|
55
56
|
"eslint-plugin-react-hooks": "^4.2.0",
|
|
56
57
|
"jest": "^26.6.3",
|
|
58
|
+
"react": "workspace:*",
|
|
57
59
|
"react-test-renderer": "^17.0.1",
|
|
58
60
|
"ts-jest": "^26.4.4",
|
|
59
61
|
"typescript": "^4.1.2"
|
|
60
62
|
},
|
|
61
63
|
"peerDependencies": {
|
|
62
64
|
"@react-native-async-storage/async-storage": "workspace:*",
|
|
63
|
-
"lodash": "workspace:*",
|
|
64
65
|
"react": "workspace:*"
|
|
66
|
+
},
|
|
67
|
+
"dependencies": {
|
|
68
|
+
"json-storage-formatter": "^1.0.0"
|
|
65
69
|
}
|
|
66
70
|
}
|