react-global-state-hooks 1.0.11 → 1.0.13

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.
@@ -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 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
  /**
9
8
  * This is a class to create global-store objects
10
9
  * @template IState
@@ -22,10 +21,8 @@ export declare class GlobalStore<IState, IActions extends IGlobalStore.IActionCo
22
21
  get isPersistStore(): boolean;
23
22
  constructor(state: IState, actions?: IActions, persistStoreAs?: string | null);
24
23
  private storedStateItem;
25
- protected formatItemFromStore<T>(_obj: T): unknown;
26
24
  protected localStorageGetItem(): string | null;
27
25
  protected getStoreItem(): IState;
28
- protected formatToStore<T>(obj: T): unknown;
29
26
  protected localStorageSetItem(valueToStore: string): void;
30
27
  protected setStoreItem(): void;
31
28
  protected getPersistStoreValue: () => IState;
@@ -51,7 +48,7 @@ export declare class GlobalStore<IState, IActions extends IGlobalStore.IActionCo
51
48
  protected globalSetterAsync: (setter: Partial<IState> | ((state: IState) => Partial<IState>)) => Promise<void>;
52
49
  protected globalSetterToPersistStoreAsync: (setter: Partial<IState> | ((state: IState) => Partial<IState>)) => Promise<void>;
53
50
  static ExecutePendingBatchesCallbacks: (() => void)[];
54
- static ExecutePendingBatches: import("lodash").DebouncedFunc<() => void>;
51
+ static ExecutePendingBatches: () => void;
55
52
  protected getActions: <IApi extends IGlobalStore.IActionCollectionResult<IState, IGlobalStore.IActionCollectionConfig<IState>>>() => IApi;
56
53
  }
57
54
  export default GlobalStore;
@@ -1 +1 @@
1
- {"version":3,"file":"GlobalStore.d.ts","sourceRoot":"","sources":["../src/GlobalStore.ts"],"names":[],"mappings":";AAAA,OAAO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AyE;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"}
@@ -1,12 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GlobalStore = exports.isPrimitive = void 0;
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 lodash_1 = require("lodash");
6
+ const json_storage_formatter_1 = require("json-storage-formatter");
7
7
  const react_1 = require("react");
8
- const isPrimitive = (value) => (0, lodash_1.isNil)(value) || (0, lodash_1.isNumber)(value) || (0, lodash_1.isBoolean)(value) || (0, lodash_1.isString)(value) || typeof value === 'symbol';
9
- exports.isPrimitive = isPrimitive;
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, lodash_1.cloneDeep)(this.state));
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, exports.isPrimitive)(partialState) || Array.isArray(partialState)
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, exports.isPrimitive)(newState) || Array.isArray(partialState)
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,39 +110,6 @@ class GlobalStore {
102
110
  }) })), {});
103
111
  };
104
112
  }
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
- }
128
- const isArray = Array.isArray(obj);
129
- if (isArray) {
130
- return obj.map((item) => this.formatItemFromStore(item));
131
- }
132
- const keys = Object.keys(obj);
133
- return keys.reduce((acumulator, key) => {
134
- const unformatedValue = obj[key];
135
- return Object.assign(Object.assign({}, acumulator), { [key]: this.formatItemFromStore(unformatedValue) });
136
- }, {});
137
- }
138
113
  localStorageGetItem() {
139
114
  return localStorage.getItem(this.persistStoreAs);
140
115
  }
@@ -144,48 +119,11 @@ class GlobalStore {
144
119
  const item = this.localStorageGetItem();
145
120
  if (item) {
146
121
  const value = JSON.parse(item);
147
- const primitive = (0, exports.isPrimitive)(value);
148
- const newState = this.formatItemFromStore(value);
149
- this.state = primitive || Array.isArray(value) ? newState : Object.assign(Object.assign({}, this.state), newState);
122
+ const newState = (0, json_storage_formatter_1.formatFromStore)(value);
123
+ this.state = newState;
150
124
  }
151
125
  return this.state;
152
126
  }
153
- formatToStore(obj) {
154
- if ((0, exports.isPrimitive)(obj)) {
155
- return obj;
156
- }
157
- const isArray = Array.isArray(obj);
158
- if (isArray) {
159
- return obj.map((item) => this.formatToStore(item));
160
- }
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) => {
185
- const value = obj[key];
186
- return (Object.assign(Object.assign({}, acumulator), { [key]: this.formatToStore(value) }));
187
- }, {});
188
- }
189
127
  localStorageSetItem(valueToStore) {
190
128
  localStorage.setItem(this.persistStoreAs, valueToStore);
191
129
  }
@@ -193,7 +131,7 @@ class GlobalStore {
193
131
  if (this.storedStateItem === this.state)
194
132
  return;
195
133
  this.storedStateItem = this.state;
196
- const valueToStore = this.formatToStore((0, lodash_1.cloneDeep)(this.state));
134
+ const valueToStore = (0, json_storage_formatter_1.formatToStore)(this.state);
197
135
  this.localStorageSetItem(JSON.stringify(valueToStore));
198
136
  }
199
137
  get stateOrchestrator() {
@@ -218,7 +156,7 @@ exports.GlobalStore = GlobalStore;
218
156
  GlobalStore.batchedUpdates = [];
219
157
  GlobalStore.ExecutePendingBatchesCallbacks = [];
220
158
  // avoid multiples calls to batchedUpdates
221
- GlobalStore.ExecutePendingBatches = (0, lodash_1.debounce)(() => {
159
+ GlobalStore.ExecutePendingBatches = (0, exports.debounce)(() => {
222
160
  const reactBatchedUpdates = react_dom_1.default.unstable_batchedUpdates || ((mock) => mock());
223
161
  reactBatchedUpdates(() => {
224
162
  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.11",
3
+ "version": "1.0.13",
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-developerreact-global-state-hooks.git"
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
  }