react-global-state-hooks 1.0.9 → 1.0.11
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 +1 -1
- package/lib/GlobalStore.d.ts +6 -2
- package/lib/GlobalStore.d.ts.map +1 -1
- package/lib/GlobalStore.js +56 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# react-global-state-hooks
|
|
2
|
-
This is a package to easily handling global-state across your react-
|
|
2
|
+
This is a package to easily handling global-state across your react-components **No-redux**, **No-context**
|
|
3
3
|
|
|
4
4
|
This utility follows the same style as the default **useState** hook, with a subscription pattern and **HOFs** to create a more intuitive, atomic and easy way of sharing state between components
|
|
5
5
|
|
package/lib/GlobalStore.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
/// <reference types="lodash" />
|
|
2
2
|
import * as IGlobalStore from './GlobalStoreTypes';
|
|
3
3
|
export declare const isPrimitive: <T>(value: T) => boolean;
|
|
4
|
+
export type IValueWithMedaData = {
|
|
5
|
+
_type_?: 'map' | 'set' | 'date';
|
|
6
|
+
value?: unknown;
|
|
7
|
+
};
|
|
4
8
|
/**
|
|
5
9
|
* This is a class to create global-store objects
|
|
6
10
|
* @template IState
|
|
@@ -18,10 +22,10 @@ export declare class GlobalStore<IState, IActions extends IGlobalStore.IActionCo
|
|
|
18
22
|
get isPersistStore(): boolean;
|
|
19
23
|
constructor(state: IState, actions?: IActions, persistStoreAs?: string | null);
|
|
20
24
|
private storedStateItem;
|
|
21
|
-
protected formatItemFromStore<T>(
|
|
25
|
+
protected formatItemFromStore<T>(_obj: T): unknown;
|
|
22
26
|
protected localStorageGetItem(): string | null;
|
|
23
27
|
protected getStoreItem(): IState;
|
|
24
|
-
protected formatToStore<T>(obj: T):
|
|
28
|
+
protected formatToStore<T>(obj: T): unknown;
|
|
25
29
|
protected localStorageSetItem(valueToStore: string): void;
|
|
26
30
|
protected setStoreItem(): void;
|
|
27
31
|
protected getPersistStoreValue: () => IState;
|
package/lib/GlobalStore.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GlobalStore.d.ts","sourceRoot":"","sources":["../src/GlobalStore.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,YAAY,MAAM,oBAAoB,CAAC;AAOnD,eAAO,MAAM,WAAW,0BAAuH,CAAC;AAEhJ;;;;;;;;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,CAAC,CAAC,EAAE,
|
|
1
|
+
{"version":3,"file":"GlobalStore.d.ts","sourceRoot":"","sources":["../src/GlobalStore.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,YAAY,MAAM,oBAAoB,CAAC;AAOnD,eAAO,MAAM,WAAW,0BAAuH,CAAC;AAEhJ,MAAM,MAAM,kBAAkB,GAAG;IAC/B,MAAM,CAAC,EAAE,KAAK,GAAG,KAAK,GAAG,MAAM,CAAC;IAChC,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF;;;;;;;;IAQI;AACJ,qBAAa,WAAW,CACtB,MAAM,EACN,QAAQ,SAA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AI,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,6CAWtB;IAEN,SAAS,CAAC,UAAU,sHAwBlB;CAEH;AAED,eAAe,WAAW,CAAC"}
|
package/lib/GlobalStore.js
CHANGED
|
@@ -102,19 +102,37 @@ class GlobalStore {
|
|
|
102
102
|
}) })), {});
|
|
103
103
|
};
|
|
104
104
|
}
|
|
105
|
-
formatItemFromStore(
|
|
105
|
+
formatItemFromStore(_obj) {
|
|
106
|
+
var _a, _b;
|
|
107
|
+
const obj = _obj;
|
|
108
|
+
if ((0, exports.isPrimitive)(obj)) {
|
|
109
|
+
return obj;
|
|
110
|
+
}
|
|
111
|
+
const isMetaDate = (obj === null || obj === void 0 ? void 0 : obj._type_) === 'date';
|
|
112
|
+
if (isMetaDate) {
|
|
113
|
+
return new Date(obj.value);
|
|
114
|
+
}
|
|
115
|
+
const isMetaMap = (obj === null || obj === void 0 ? void 0 : obj._type_) === 'map';
|
|
116
|
+
if (isMetaMap) {
|
|
117
|
+
const mapData = ((_a = obj.value) !== null && _a !== void 0 ? _a : []).map(([key, item]) => [
|
|
118
|
+
key,
|
|
119
|
+
this.formatItemFromStore(item),
|
|
120
|
+
]);
|
|
121
|
+
return new Map(mapData);
|
|
122
|
+
}
|
|
123
|
+
const isMetaSet = (obj === null || obj === void 0 ? void 0 : obj._type_) === 'set';
|
|
124
|
+
if (isMetaSet) {
|
|
125
|
+
const setData = (_b = obj.value) !== null && _b !== void 0 ? _b : [].map((item) => this.formatItemFromStore(item));
|
|
126
|
+
return new Set(setData);
|
|
127
|
+
}
|
|
106
128
|
const isArray = Array.isArray(obj);
|
|
107
129
|
if (isArray) {
|
|
108
130
|
return obj.map((item) => this.formatItemFromStore(item));
|
|
109
131
|
}
|
|
110
|
-
|
|
111
|
-
|
|
132
|
+
const keys = Object.keys(obj);
|
|
133
|
+
return keys.reduce((acumulator, key) => {
|
|
112
134
|
const unformatedValue = obj[key];
|
|
113
|
-
|
|
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) });
|
|
135
|
+
return Object.assign(Object.assign({}, acumulator), { [key]: this.formatItemFromStore(unformatedValue) });
|
|
118
136
|
}, {});
|
|
119
137
|
}
|
|
120
138
|
localStorageGetItem() {
|
|
@@ -127,20 +145,45 @@ class GlobalStore {
|
|
|
127
145
|
if (item) {
|
|
128
146
|
const value = JSON.parse(item);
|
|
129
147
|
const primitive = (0, exports.isPrimitive)(value);
|
|
130
|
-
const newState =
|
|
148
|
+
const newState = this.formatItemFromStore(value);
|
|
131
149
|
this.state = primitive || Array.isArray(value) ? newState : Object.assign(Object.assign({}, this.state), newState);
|
|
132
150
|
}
|
|
133
151
|
return this.state;
|
|
134
152
|
}
|
|
135
153
|
formatToStore(obj) {
|
|
154
|
+
if ((0, exports.isPrimitive)(obj)) {
|
|
155
|
+
return obj;
|
|
156
|
+
}
|
|
136
157
|
const isArray = Array.isArray(obj);
|
|
137
158
|
if (isArray) {
|
|
138
159
|
return obj.map((item) => this.formatToStore(item));
|
|
139
160
|
}
|
|
140
|
-
|
|
161
|
+
const isMap = obj instanceof Map;
|
|
162
|
+
if (isMap) {
|
|
163
|
+
const pairs = Array.from(obj.entries());
|
|
164
|
+
return {
|
|
165
|
+
_type_: 'map',
|
|
166
|
+
value: pairs.map((pair) => this.formatToStore(pair)),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
const isSet = obj instanceof Set;
|
|
170
|
+
if (isSet) {
|
|
171
|
+
const values = Array.from(obj.values());
|
|
172
|
+
return {
|
|
173
|
+
_type_: 'set',
|
|
174
|
+
value: values.map((value) => this.formatToStore(value)),
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
if ((0, lodash_1.isDate)(obj)) {
|
|
178
|
+
return {
|
|
179
|
+
_type_: 'date',
|
|
180
|
+
value: obj.toISOString(),
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
const keys = Object.keys(obj);
|
|
184
|
+
return keys.reduce((acumulator, key) => {
|
|
141
185
|
const value = obj[key];
|
|
142
|
-
|
|
143
|
-
return (Object.assign(Object.assign({}, acumulator), { [key]: (0, exports.isPrimitive)(value) || isDatetime ? value : this.formatToStore(value), [`${key}_type`]: isDatetime ? 'date' : typeof value }));
|
|
186
|
+
return (Object.assign(Object.assign({}, acumulator), { [key]: this.formatToStore(value) }));
|
|
144
187
|
}, {});
|
|
145
188
|
}
|
|
146
189
|
localStorageSetItem(valueToStore) {
|
|
@@ -150,7 +193,7 @@ class GlobalStore {
|
|
|
150
193
|
if (this.storedStateItem === this.state)
|
|
151
194
|
return;
|
|
152
195
|
this.storedStateItem = this.state;
|
|
153
|
-
const valueToStore =
|
|
196
|
+
const valueToStore = this.formatToStore((0, lodash_1.cloneDeep)(this.state));
|
|
154
197
|
this.localStorageSetItem(JSON.stringify(valueToStore));
|
|
155
198
|
}
|
|
156
199
|
get stateOrchestrator() {
|
package/package.json
CHANGED