react-global-state-hooks 1.0.10 → 1.0.12
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 +6 -5
- package/lib/GlobalStore.d.ts.map +1 -1
- package/lib/GlobalStore.js +19 -37
- package/package.json +7 -4
package/lib/GlobalStore.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
/// <reference types="lodash" />
|
|
2
1
|
import * as IGlobalStore from './GlobalStoreTypes';
|
|
3
|
-
export
|
|
2
|
+
export type IValueWithMedaData = {
|
|
3
|
+
_type_?: 'map' | 'set' | 'date';
|
|
4
|
+
value?: unknown;
|
|
5
|
+
};
|
|
6
|
+
export declare const debounce: <T extends Function>(callback: T, wait?: number) => T;
|
|
4
7
|
/**
|
|
5
8
|
* This is a class to create global-store objects
|
|
6
9
|
* @template IState
|
|
@@ -18,10 +21,8 @@ export declare class GlobalStore<IState, IActions extends IGlobalStore.IActionCo
|
|
|
18
21
|
get isPersistStore(): boolean;
|
|
19
22
|
constructor(state: IState, actions?: IActions, persistStoreAs?: string | null);
|
|
20
23
|
private storedStateItem;
|
|
21
|
-
protected formatItemFromStore<T>(obj: T): any;
|
|
22
24
|
protected localStorageGetItem(): string | null;
|
|
23
25
|
protected getStoreItem(): IState;
|
|
24
|
-
protected formatToStore<T>(obj: T): any;
|
|
25
26
|
protected localStorageSetItem(valueToStore: string): void;
|
|
26
27
|
protected setStoreItem(): void;
|
|
27
28
|
protected getPersistStoreValue: () => IState;
|
|
@@ -47,7 +48,7 @@ export declare class GlobalStore<IState, IActions extends IGlobalStore.IActionCo
|
|
|
47
48
|
protected globalSetterAsync: (setter: Partial<IState> | ((state: IState) => Partial<IState>)) => Promise<void>;
|
|
48
49
|
protected globalSetterToPersistStoreAsync: (setter: Partial<IState> | ((state: IState) => Partial<IState>)) => Promise<void>;
|
|
49
50
|
static ExecutePendingBatchesCallbacks: (() => void)[];
|
|
50
|
-
static ExecutePendingBatches:
|
|
51
|
+
static ExecutePendingBatches: () => void;
|
|
51
52
|
protected getActions: <IApi extends IGlobalStore.IActionCollectionResult<IState, IGlobalStore.IActionCollectionConfig<IState>>>() => IApi;
|
|
52
53
|
}
|
|
53
54
|
export default GlobalStore;
|
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":"AAAA,OAAO,KAAK,YAAY,MAAM,oBAAoB,CAAC;AAWnD,MAAM,MAAM,kBAAkB,GAAG;IAC/B,MAAM,CAAC,EAAE,KAAK,GAAG,KAAK,GAAG,MAAM,CAAC;IAChC,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF,eAAO,MAAM,QAAQ,uDAUpB,CAAC;AAEF;;;;;;;;IAQI;AACJ,qBAAa,WAAW,CACtB,MAAM,EACN,QAAQ,SAAS,YAAY,CAAC,uBAAuB,CAAC,MAAM,CAAC,GAAG,IAAI,GAAG,IAAI,CAC3E,YAAW,YAAY,CAAC,YAAY,CAAC,MAAM,EAAE,QAAQ,CAAC;IAQ1C,SAAS,CAAC,KAAK,EAAE,MAAM;IAAE,SAAS,CAAC,OAAO,EAAE,QAAQ;IAAqB,SAAS,CAAC,cAAc,EAAE,MAAM,GAAG,IAAI;IAN5H,SAAS,CAAC,WAAW,EAAE,YAAY,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAM;IAE/D,IAAW,cAAc,YAExB;gBAEqB,KAAK,EAAE,MAAM,EAAY,OAAO,GAAE,QAA2B,EAAY,cAAc,GAAE,MAAM,GAAG,IAAW;IAEnI,OAAO,CAAC,eAAe,CAAiC;IAExD,SAAS,CAAC,mBAAmB,IAAI,MAAM,GAAG,IAAI;IAI9C,SAAS,CAAC,YAAY,IAAI,MAAM;IAgBhC,SAAS,CAAC,mBAAmB,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI;IAIzD,SAAS,CAAC,YAAY,IAAI,IAAI;IAU9B,SAAS,CAAC,oBAAoB,QAAO,MAAM,CAAwB;IAEnE,SAAS,CAAC,YAAY,QAAO,MAAM,CAAqC;IAExE;;;OAGG;IACI,OAAO,6OAsBZ;IAEF;;;;OAIG;IACI,gBAAgB,mLAIf,MAAM,oDASZ;IAEF,OAAO,CAAC,kBAAkB,CAA0G;IAEpI,SAAS,KAAK,iBAAiB,IAAI,YAAY,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,YAAY,CAAC,uBAAuB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAY3H;IAED;;OAEG;IACH,SAAS,CAAC,MAAM,CAAC,cAAc,EAAE,CAAC,MAAM,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,CAAM;IAErE,SAAS,CAAC,YAAY,sCAAuC,MAAM,KAAK,QAAQ,MAAM,CAAC,aAAa,MAAM,IAAI,UAyB5G;IAEF,SAAS,CAAC,iBAAiB,sCACiB,MAAM,KAAK,QAAQ,MAAM,CAAC,MACpE,QAAQ,IAAI,CAAC,CAAyE;IAExF,SAAS,CAAC,+BAA+B,sCAA6C,MAAM,KAAK,QAAQ,MAAM,CAAC,MAAI,QAAQ,IAAI,CAAC,CAG/H;IAEF,MAAM,CAAC,8BAA8B,EAAE,CAAC,MAAM,IAAI,CAAC,EAAE,CAAM;IAG3D,MAAM,CAAC,qBAAqB,aAWtB;IAEN,SAAS,CAAC,UAAU,sHAwBlB;CAEH;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_dom_1 = tslib_1.__importDefault(require("react-dom"));
|
|
6
|
-
const
|
|
6
|
+
const json_storage_formatter_1 = require("json-storage-formatter");
|
|
7
7
|
const react_1 = require("react");
|
|
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
|
/**
|
|
11
19
|
* This is a class to create global-store objects
|
|
12
20
|
* @template IState
|
|
@@ -27,7 +35,7 @@ class GlobalStore {
|
|
|
27
35
|
this.subscribers = [];
|
|
28
36
|
this.storedStateItem = undefined;
|
|
29
37
|
this.getPersistStoreValue = () => this.getStoreItem();
|
|
30
|
-
this.getStateCopy = () => Object.freeze((0,
|
|
38
|
+
this.getStateCopy = () => Object.freeze((0, json_storage_formatter_1.clone)(this.state));
|
|
31
39
|
/**
|
|
32
40
|
* Returns a global hook that will share information across components by subscribing them to a specific store.
|
|
33
41
|
* @return [currentState, GlobalState.IHookResult<IState, IActions, IApi>]
|
|
@@ -61,7 +69,7 @@ class GlobalStore {
|
|
|
61
69
|
this._stateOrchestrator = null;
|
|
62
70
|
this.globalSetter = (setter, callback) => {
|
|
63
71
|
const partialState = typeof setter === 'function' ? setter(this.getStateCopy()) : setter;
|
|
64
|
-
let newState = (0,
|
|
72
|
+
let newState = (0, json_storage_formatter_1.isPrimitive)(partialState) || Array.isArray(partialState)
|
|
65
73
|
? partialState : Object.assign(Object.assign({}, this.state), partialState);
|
|
66
74
|
// avoid perform multiple update batches by accumulating state changes of the same hook
|
|
67
75
|
GlobalStore.batchedUpdates = GlobalStore.batchedUpdates.filter(([, hook, previousState]) => {
|
|
@@ -69,7 +77,7 @@ class GlobalStore {
|
|
|
69
77
|
if (isSameHook) {
|
|
70
78
|
// eslint-disable-next-line no-console
|
|
71
79
|
console.warn('You should try avoid call the same state-setter multiple times at one execution line');
|
|
72
|
-
newState = (0,
|
|
80
|
+
newState = (0, json_storage_formatter_1.isPrimitive)(newState) || Array.isArray(partialState)
|
|
73
81
|
? newState : Object.assign(Object.assign({}, previousState), newState);
|
|
74
82
|
}
|
|
75
83
|
return !isSameHook;
|
|
@@ -102,21 +110,6 @@ class GlobalStore {
|
|
|
102
110
|
}) })), {});
|
|
103
111
|
};
|
|
104
112
|
}
|
|
105
|
-
formatItemFromStore(obj) {
|
|
106
|
-
const isArray = Array.isArray(obj);
|
|
107
|
-
if (isArray) {
|
|
108
|
-
return obj.map((item) => this.formatItemFromStore(item));
|
|
109
|
-
}
|
|
110
|
-
return Object.keys(obj).filter((key) => !key.includes('_type')).reduce((acumulator, key) => {
|
|
111
|
-
const type = obj[`${key}_type`];
|
|
112
|
-
const unformatedValue = obj[key];
|
|
113
|
-
const isDateType = type === 'date';
|
|
114
|
-
if (isDateType) {
|
|
115
|
-
return Object.assign(Object.assign({}, acumulator), { [key]: new Date(unformatedValue) });
|
|
116
|
-
}
|
|
117
|
-
return Object.assign(Object.assign({}, acumulator), { [key]: (0, exports.isPrimitive)(unformatedValue) ? unformatedValue : this.formatItemFromStore(unformatedValue) });
|
|
118
|
-
}, {});
|
|
119
|
-
}
|
|
120
113
|
localStorageGetItem() {
|
|
121
114
|
return localStorage.getItem(this.persistStoreAs);
|
|
122
115
|
}
|
|
@@ -126,23 +119,12 @@ class GlobalStore {
|
|
|
126
119
|
const item = this.localStorageGetItem();
|
|
127
120
|
if (item) {
|
|
128
121
|
const value = JSON.parse(item);
|
|
129
|
-
const primitive = (0,
|
|
130
|
-
const newState =
|
|
122
|
+
const primitive = (0, json_storage_formatter_1.isPrimitive)(value);
|
|
123
|
+
const newState = (0, json_storage_formatter_1.formatFromStore)(value);
|
|
131
124
|
this.state = primitive || Array.isArray(value) ? newState : Object.assign(Object.assign({}, this.state), newState);
|
|
132
125
|
}
|
|
133
126
|
return this.state;
|
|
134
127
|
}
|
|
135
|
-
formatToStore(obj) {
|
|
136
|
-
const isArray = Array.isArray(obj);
|
|
137
|
-
if (isArray) {
|
|
138
|
-
return obj.map((item) => this.formatToStore(item));
|
|
139
|
-
}
|
|
140
|
-
return Object.keys(obj).reduce((acumulator, key) => {
|
|
141
|
-
const value = obj[key];
|
|
142
|
-
const isDatetime = value instanceof Date;
|
|
143
|
-
return (Object.assign(Object.assign({}, acumulator), { [key]: (0, exports.isPrimitive)(value) || isDatetime ? value : this.formatToStore(value), [`${key}_type`]: isDatetime ? 'date' : typeof value }));
|
|
144
|
-
}, {});
|
|
145
|
-
}
|
|
146
128
|
localStorageSetItem(valueToStore) {
|
|
147
129
|
localStorage.setItem(this.persistStoreAs, valueToStore);
|
|
148
130
|
}
|
|
@@ -150,7 +132,7 @@ class GlobalStore {
|
|
|
150
132
|
if (this.storedStateItem === this.state)
|
|
151
133
|
return;
|
|
152
134
|
this.storedStateItem = this.state;
|
|
153
|
-
const valueToStore = (0,
|
|
135
|
+
const valueToStore = (0, json_storage_formatter_1.formatToStore)(this.state);
|
|
154
136
|
this.localStorageSetItem(JSON.stringify(valueToStore));
|
|
155
137
|
}
|
|
156
138
|
get stateOrchestrator() {
|
|
@@ -175,7 +157,7 @@ exports.GlobalStore = GlobalStore;
|
|
|
175
157
|
GlobalStore.batchedUpdates = [];
|
|
176
158
|
GlobalStore.ExecutePendingBatchesCallbacks = [];
|
|
177
159
|
// avoid multiples calls to batchedUpdates
|
|
178
|
-
GlobalStore.ExecutePendingBatches = (0,
|
|
160
|
+
GlobalStore.ExecutePendingBatches = (0, exports.debounce)(() => {
|
|
179
161
|
const reactBatchedUpdates = react_dom_1.default.unstable_batchedUpdates || ((mock) => mock());
|
|
180
162
|
reactBatchedUpdates(() => {
|
|
181
163
|
GlobalStore.batchedUpdates.forEach(([execute]) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-global-state-hooks",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.12",
|
|
4
4
|
"description": "This is a package to easily handling global-state across your react components No-redux, No-context.",
|
|
5
5
|
"main": "lib/GlobalStore.js",
|
|
6
6
|
"files": [
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
},
|
|
18
18
|
"repository": {
|
|
19
19
|
"type": "git",
|
|
20
|
-
"url": "git+https://github.com/johnny-quesada-
|
|
20
|
+
"url": "git+https://github.com/johnny-quesada-developer/react-global-state-hooks.git"
|
|
21
21
|
},
|
|
22
22
|
"keywords": [
|
|
23
23
|
"react",
|
|
@@ -37,7 +37,6 @@
|
|
|
37
37
|
"homepage": "https://github.com/johnny-quesada-developer/react-global-state-hooks#readme",
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@types/jest": "^26.0.17",
|
|
40
|
-
"@types/lodash": "^4.14.165",
|
|
41
40
|
"@types/react": "^17.0.0",
|
|
42
41
|
"@types/react-dom": "^17.0.0",
|
|
43
42
|
"@types/react-test-renderer": "^17.0.0",
|
|
@@ -50,13 +49,17 @@
|
|
|
50
49
|
"eslint-plugin-react": "^7.21.5",
|
|
51
50
|
"eslint-plugin-react-hooks": "^4.2.0",
|
|
52
51
|
"jest": "^26.6.3",
|
|
52
|
+
"react": "17.0.2",
|
|
53
|
+
"react-dom": "17.0.2",
|
|
53
54
|
"react-test-renderer": "^17.0.1",
|
|
54
55
|
"ts-jest": "^26.4.4",
|
|
55
56
|
"typescript": "^4.1.2"
|
|
56
57
|
},
|
|
57
58
|
"peerDependencies": {
|
|
58
|
-
"lodash": "workspace:*",
|
|
59
59
|
"react": "workspace:*",
|
|
60
60
|
"react-dom": "workspace:*"
|
|
61
|
+
},
|
|
62
|
+
"dependencies": {
|
|
63
|
+
"json-storage-formatter": "^1.0.0"
|
|
61
64
|
}
|
|
62
65
|
}
|