react-native-global-state-hooks 2.1.4 → 2.1.6
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 +86 -37
- package/lib/GlobalStore.d.ts +15 -18
- package/lib/GlobalStore.d.ts.map +1 -1
- package/lib/GlobalStore.js +19 -12
- package/lib/GlobalStore.types.d.ts +2 -1
- package/lib/GlobalStore.types.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -140,34 +140,71 @@ You could just use this code right as it is by just adding also into your projec
|
|
|
140
140
|
```ts
|
|
141
141
|
export class GlobalStoreAsync<
|
|
142
142
|
TState,
|
|
143
|
-
TMetadata extends { readonly isAsyncStorageReady:
|
|
143
|
+
TMetadata extends { readonly isAsyncStorageReady: never },
|
|
144
|
+
// this restriction is needed to avoid the consumers to set the isAsyncStorageReady property from outside the store,
|
|
145
|
+
// even when the value will be ignored is better to avoid it to avoid confusion
|
|
144
146
|
TStateSetter extends
|
|
145
|
-
| ActionCollectionConfig<
|
|
147
|
+
| ActionCollectionConfig<
|
|
148
|
+
TState,
|
|
149
|
+
Omit<TMetadata, 'isAsyncStorageReady'> & {
|
|
150
|
+
readonly isAsyncStorageReady: boolean;
|
|
151
|
+
}
|
|
152
|
+
>
|
|
146
153
|
| StateSetter<TState>
|
|
147
154
|
| null = StateSetter<TState>
|
|
148
|
-
> extends GlobalStore<
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
+
> extends GlobalStore<
|
|
156
|
+
TState,
|
|
157
|
+
Omit<TMetadata, 'isAsyncStorageReady'> & {
|
|
158
|
+
readonly isAsyncStorageReady: boolean;
|
|
159
|
+
},
|
|
160
|
+
TStateSetter
|
|
161
|
+
> {
|
|
162
|
+
protected config: NonNullable<
|
|
163
|
+
GlobalStoreConfig<
|
|
164
|
+
TState,
|
|
165
|
+
Omit<TMetadata, 'isAsyncStorageReady'> & {
|
|
166
|
+
readonly isAsyncStorageReady: boolean;
|
|
167
|
+
},
|
|
168
|
+
NonNullable<TStateSetter>
|
|
169
|
+
>
|
|
155
170
|
> & {
|
|
156
|
-
asyncStorageKey: string;
|
|
171
|
+
asyncStorageKey: string;
|
|
157
172
|
};
|
|
158
173
|
|
|
159
174
|
constructor(
|
|
160
175
|
state: TState,
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
176
|
+
config: GlobalStoreConfig<
|
|
177
|
+
TState,
|
|
178
|
+
TMetadata,
|
|
179
|
+
ActionCollectionConfig<TState, TMetadata> | StateSetter<TState>
|
|
180
|
+
> & {
|
|
181
|
+
asyncStorageKey: string;
|
|
182
|
+
},
|
|
183
|
+
setterConfig: TStateSetter | null = null
|
|
169
184
|
) {
|
|
170
|
-
|
|
185
|
+
type TConfig = NonNullable<
|
|
186
|
+
GlobalStoreConfig<
|
|
187
|
+
TState,
|
|
188
|
+
Omit<TMetadata, 'isAsyncStorageReady'> & {
|
|
189
|
+
readonly isAsyncStorageReady: boolean;
|
|
190
|
+
},
|
|
191
|
+
NonNullable<TStateSetter>
|
|
192
|
+
>
|
|
193
|
+
> & {
|
|
194
|
+
asyncStorageKey: string;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const { onInit: onInitConfig, ...configParameters } = config as TConfig;
|
|
198
|
+
|
|
199
|
+
super(state, configParameters, setterConfig as TStateSetter);
|
|
200
|
+
|
|
201
|
+
this.config = {
|
|
202
|
+
...config,
|
|
203
|
+
metadata: {
|
|
204
|
+
...configParameters.metadata,
|
|
205
|
+
isAsyncStorageReady: false,
|
|
206
|
+
},
|
|
207
|
+
} as TConfig;
|
|
171
208
|
|
|
172
209
|
const parameters = this.getConfigCallbackParam({});
|
|
173
210
|
|
|
@@ -177,7 +214,7 @@ export class GlobalStoreAsync<
|
|
|
177
214
|
|
|
178
215
|
/**
|
|
179
216
|
* This method will be called once the store is created after the constructor,
|
|
180
|
-
* this method is different from the onInit of the
|
|
217
|
+
* this method is different from the onInit of the config property and it won't be overriden
|
|
181
218
|
*/
|
|
182
219
|
protected onInit = async ({
|
|
183
220
|
setState,
|
|
@@ -185,20 +222,20 @@ export class GlobalStoreAsync<
|
|
|
185
222
|
getMetadata,
|
|
186
223
|
}: StateConfigCallbackParam<
|
|
187
224
|
TState,
|
|
188
|
-
TMetadata,
|
|
225
|
+
Omit<TMetadata, 'isAsyncStorageReady'> & {
|
|
226
|
+
readonly isAsyncStorageReady: boolean;
|
|
227
|
+
},
|
|
189
228
|
NonNullable<TStateSetter>
|
|
190
229
|
>) => {
|
|
191
230
|
const { asyncStorageKey } = this.config;
|
|
192
231
|
const storedItem: string = await asyncStorage.getItem(asyncStorageKey);
|
|
193
232
|
|
|
194
|
-
this.isAsyncStorageReady = true;
|
|
195
|
-
|
|
196
233
|
setMetadata({
|
|
197
234
|
...getMetadata(),
|
|
198
235
|
isAsyncStorageReady: true,
|
|
199
236
|
});
|
|
200
237
|
|
|
201
|
-
if (
|
|
238
|
+
if (storedItem === null) return;
|
|
202
239
|
|
|
203
240
|
const jsonParsed = JSON.parse(storedItem);
|
|
204
241
|
const items = formatFromStore<TState>(jsonParsed);
|
|
@@ -208,13 +245,21 @@ export class GlobalStoreAsync<
|
|
|
208
245
|
|
|
209
246
|
protected onStateChanged = ({
|
|
210
247
|
getState,
|
|
211
|
-
}: StateChangesParam<
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
248
|
+
}: StateChangesParam<
|
|
249
|
+
TState,
|
|
250
|
+
Omit<TMetadata, 'isAsyncStorageReady'> & {
|
|
251
|
+
readonly isAsyncStorageReady: boolean;
|
|
252
|
+
},
|
|
253
|
+
NonNullable<TStateSetter>
|
|
254
|
+
>) => {
|
|
215
255
|
const { asyncStorageKey } = this.config;
|
|
216
256
|
|
|
217
|
-
|
|
257
|
+
const state = getState();
|
|
258
|
+
const formattedObject = formatToStore(state, {
|
|
259
|
+
stringify: true,
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
asyncStorage.setItem(asyncStorageKey, formattedObject);
|
|
218
263
|
};
|
|
219
264
|
}
|
|
220
265
|
```
|
|
@@ -251,13 +296,17 @@ It's super common to have the wish or the necessity of restricting the manipulat
|
|
|
251
296
|
```ts
|
|
252
297
|
const initialValue = 0;
|
|
253
298
|
|
|
254
|
-
|
|
255
|
-
//
|
|
256
|
-
|
|
299
|
+
const config = {
|
|
300
|
+
// this is not reactive information that you could also store in the async storage
|
|
301
|
+
// upating the metadata will not trigger the onStateChanged method or any update on the components
|
|
302
|
+
metadata: null,
|
|
303
|
+
|
|
304
|
+
// The lifecycle callbacks are: onInit, onStateChanged, onSubscribed and computePreventStateChange
|
|
305
|
+
};
|
|
257
306
|
|
|
258
307
|
const countStore = new GlobalStore(
|
|
259
308
|
initialValue,
|
|
260
|
-
|
|
309
|
+
config,
|
|
261
310
|
{
|
|
262
311
|
log: (message: string) => (): void => {
|
|
263
312
|
console.log(message);
|
|
@@ -309,7 +358,7 @@ import { GlobalStore } from 'react-native-global-state-hooks';
|
|
|
309
358
|
|
|
310
359
|
const initialValue = 0;
|
|
311
360
|
|
|
312
|
-
const store = new GlobalStore(0,
|
|
361
|
+
const store = new GlobalStore(0, {
|
|
313
362
|
onInit: async ({ setMetadata, setState }) => {
|
|
314
363
|
const data = await someApiCall();
|
|
315
364
|
|
|
@@ -330,7 +379,7 @@ This method will be called every time the state is changed
|
|
|
330
379
|
```ts
|
|
331
380
|
import { GlobalStore } from 'react-native-global-state-hooks';
|
|
332
381
|
|
|
333
|
-
const store = new GlobalStore(0,
|
|
382
|
+
const store = new GlobalStore(0, {
|
|
334
383
|
onStateChanged: ({ getState }) => {
|
|
335
384
|
const state = getState();
|
|
336
385
|
|
|
@@ -350,7 +399,7 @@ This method will be called every time a component is subscribed to the store
|
|
|
350
399
|
```ts
|
|
351
400
|
import { GlobalStore } from 'react-native-global-state-hooks';
|
|
352
401
|
|
|
353
|
-
const store = new GlobalStore(0,
|
|
402
|
+
const store = new GlobalStore(0, {
|
|
354
403
|
onSubscribed: ({ getState }) => {
|
|
355
404
|
console.log('A component was subscribed to the store');
|
|
356
405
|
},
|
|
@@ -368,7 +417,7 @@ This method will be called every time the state is going to be changed, if it re
|
|
|
368
417
|
```ts
|
|
369
418
|
import { GlobalStore } from 'react-native-global-state-hooks';
|
|
370
419
|
|
|
371
|
-
const store = new GlobalStore(0,
|
|
420
|
+
const store = new GlobalStore(0, {
|
|
372
421
|
computePreventStateChange: ({ getState }) => {
|
|
373
422
|
const state = getState();
|
|
374
423
|
const shouldPrevent = state < 0;
|
package/lib/GlobalStore.d.ts
CHANGED
|
@@ -13,14 +13,19 @@ export declare const formatToStore: <TValue, TStringify extends boolean = false>
|
|
|
13
13
|
* */
|
|
14
14
|
export declare class GlobalStore<TState, TMetadata = null, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> | null = StateSetter<TState>> {
|
|
15
15
|
protected state: TState;
|
|
16
|
-
protected metadata: TMetadata;
|
|
17
16
|
protected setterConfig: TStateSetter | null;
|
|
18
|
-
protected config: GlobalStoreConfig<TState, TMetadata, TStateSetter>;
|
|
19
17
|
/**
|
|
20
18
|
* list of all the subscribers setState functions
|
|
21
19
|
* @template {TState} TState - The type of the state object
|
|
22
20
|
* */
|
|
23
21
|
subscribers: Set<StateSetter<TState>>;
|
|
22
|
+
/**
|
|
23
|
+
* additional configuration for the store
|
|
24
|
+
* @template {TState} TState - The type of the state object
|
|
25
|
+
* @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
|
|
26
|
+
* @template {TStateSetter} TStateSetter - The type of the setterConfig object (optional) (default: null) if a configuration is passed, the hook will return an object with the actions then all the store manipulation will be done through the actions
|
|
27
|
+
*/
|
|
28
|
+
protected config: GlobalStoreConfig<TState, TMetadata, TStateSetter>;
|
|
24
29
|
/**
|
|
25
30
|
* execute once the store is created
|
|
26
31
|
* @template {TState} TState - The type of the state object
|
|
@@ -75,22 +80,14 @@ export declare class GlobalStore<TState, TMetadata = null, TStateSetter extends
|
|
|
75
80
|
* @param {TState} state - The initial state
|
|
76
81
|
* */
|
|
77
82
|
constructor(state: TState);
|
|
78
|
-
/**
|
|
79
|
-
* Create a simple global store with also contains a metadata object
|
|
80
|
-
* The metadata object is not reactive, but it can be used to share information with the subscribers
|
|
81
|
-
* @param {TState} state - The initial state
|
|
82
|
-
* @param {TMetadata} metadata - The metadata object (optional) (default: null) no reactive information set to share with the subscribers
|
|
83
|
-
* */
|
|
84
|
-
constructor(state: TState, metadata: TMetadata);
|
|
85
83
|
/**
|
|
86
84
|
* Create a new global store with custom action
|
|
87
85
|
* The metadata object could be null if not needed
|
|
88
86
|
* The setter Object is used to define the actions that will be used to manipulate the state
|
|
89
87
|
* @param {TState} state - The initial state
|
|
90
|
-
* @param {TMetadata} metadata - The metadata object (optional) (default: null) no reactive information set to share with the subscribers
|
|
91
88
|
* @param {TStateSetter} setterConfig - The actions configuration object (optional) (default: null) if not null the store manipulation will be done through the actions
|
|
92
89
|
* */
|
|
93
|
-
constructor(state: TState,
|
|
90
|
+
constructor(state: TState, config: GlobalStoreConfig<TState, TMetadata, TStateSetter>);
|
|
94
91
|
/**
|
|
95
92
|
* Create a new global store with custom action
|
|
96
93
|
* The metadata object could be null if not needed
|
|
@@ -98,15 +95,15 @@ export declare class GlobalStore<TState, TMetadata = null, TStateSetter extends
|
|
|
98
95
|
* The config object is used to define the callbacks that will be executed during the store lifecycle
|
|
99
96
|
* The lifecycle callbacks are: onInit, onStateChanged, onSubscribed and computePreventStateChange
|
|
100
97
|
* @param {TState} state - The initial state
|
|
101
|
-
* @param {TMetadata} metadata - The metadata object (optional) (default: null) no reactive information set to share with the subscribers
|
|
102
|
-
* @param {TStateSetter} setterConfig - The actions configuration object (optional) (default: null) if not null the store manipulation will be done through the actions
|
|
103
98
|
* @param {GlobalStoreConfig<TState, TMetadata>} config - The configuration object (optional) (default: { metadata: null })
|
|
104
|
-
* @param {
|
|
105
|
-
* @param {
|
|
106
|
-
* @param {
|
|
107
|
-
* @param {
|
|
99
|
+
* @param {GlobalStoreConfig<TState, TMetadata>} config.metadata - The metadata object (optional) (default: null) if not null the metadata object will be reactive
|
|
100
|
+
* @param {GlobalStoreConfig<TState, TMetadata>} config.onInit - The callback to execute when the store is initialized (optional) (default: null)
|
|
101
|
+
* @param {GlobalStoreConfig<TState, TMetadata>} config.onStateChanged - The callback to execute when the state is changed (optional) (default: null)
|
|
102
|
+
* @param {GlobalStoreConfig<TState, TMetadata>} config.onSubscribed - The callback to execute when a new component gets subscribed to the store (optional) (default: null)
|
|
103
|
+
* @param {GlobalStoreConfig<TState, TMetadata>} config.computePreventStateChange - The callback to execute everytime a state change is triggered and before the state is updated, it allows to prevent the state change by returning true (optional) (default: null)
|
|
104
|
+
* @param {TStateSetter} setterConfig - The actions configuration object (optional) (default: null) if not null the store manipulation will be done through the actions
|
|
108
105
|
* */
|
|
109
|
-
constructor(state: TState,
|
|
106
|
+
constructor(state: TState, config: GlobalStoreConfig<TState, TMetadata, TStateSetter>, setterConfig: TStateSetter);
|
|
110
107
|
protected onInitializeStore: () => void;
|
|
111
108
|
/**
|
|
112
109
|
* gets a clone of the state
|
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":"AACA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAuB,MAAM,OAAO,CAAC;AAEtE,OAAO,EACL,sBAAsB,EACtB,WAAW,EACX,iBAAiB,EACjB,sBAAsB,EACtB,wBAAwB,EAEzB,MAAM,qBAAqB,CAAC;AAU7B,eAAO,MAAM,KAAK,kBAA6B,CAAC;AAChD,eAAO,MAAM,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CAAC,MAAM,CAAC,CAAC;UAC5D,uBAAuB,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,CAgDzD;CACH;AAED,eAAe,WAAW,CAAC"}
|
package/lib/GlobalStore.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.GlobalStore = exports.formatToStore = exports.formatFromStore = exports.clone = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
-
// jsonStorageFormatter
|
|
6
5
|
const jsonStorageFormatter = tslib_1.__importStar(require("json-storage-formatter"));
|
|
7
6
|
const react_1 = require("react");
|
|
8
7
|
const throwWrongKeyOnActionCollectionConfig = (action_key) => {
|
|
@@ -25,24 +24,30 @@ class GlobalStore {
|
|
|
25
24
|
/**
|
|
26
25
|
* Create a new instance of the GlobalStore
|
|
27
26
|
* @param {TState} state - The initial state
|
|
28
|
-
* @param {TMetadata} metadata - The metadata object (optional) (default: null) no reactive information set to share with the subscribers
|
|
29
|
-
* @param {TStateSetter} setterConfig - The actions configuration object (optional) (default: null) if not null the store manipulation will be done through the actions
|
|
30
27
|
* @param {GlobalStoreConfig<TState, TMetadata>} config - The configuration object (optional) (default: { metadata: null })
|
|
31
|
-
* @param {
|
|
32
|
-
* @param {
|
|
33
|
-
* @param {
|
|
34
|
-
* @param {
|
|
28
|
+
* @param {GlobalStoreConfig<TState, TMetadata>} config.metadata - The metadata object (optional) (default: null) if not null the metadata object will be reactive
|
|
29
|
+
* @param {GlobalStoreConfig<TState, TMetadata>} config.onInit - The callback to execute when the store is initialized (optional) (default: null)
|
|
30
|
+
* @param {GlobalStoreConfig<TState, TMetadata>} config.onStateChanged - The callback to execute when the state is changed (optional) (default: null)
|
|
31
|
+
* @param {GlobalStoreConfig<TState, TMetadata>} config.onSubscribed - The callback to execute when a new component gets subscribed to the store (optional) (default: null)
|
|
32
|
+
* @param {GlobalStoreConfig<TState, TMetadata>} config.computePreventStateChange - The callback to execute everytime a state change is triggered and before the state is updated, it allows to prevent the state change by returning true (optional) (default: null) * @param {TStateSetter} setterConfig - The actions configuration object (optional) (default: null) if not null the store manipulation will be done through the actions
|
|
35
33
|
* */
|
|
36
|
-
constructor(state,
|
|
34
|
+
constructor(state, config = {}, setterConfig = null) {
|
|
37
35
|
this.state = state;
|
|
38
|
-
this.metadata = metadata;
|
|
39
36
|
this.setterConfig = setterConfig;
|
|
40
|
-
this.config = config;
|
|
41
37
|
/**
|
|
42
38
|
* list of all the subscribers setState functions
|
|
43
39
|
* @template {TState} TState - The type of the state object
|
|
44
40
|
* */
|
|
45
41
|
this.subscribers = new Set();
|
|
42
|
+
/**
|
|
43
|
+
* additional configuration for the store
|
|
44
|
+
* @template {TState} TState - The type of the state object
|
|
45
|
+
* @template {TMetadata} TMetadata - The type of the metadata object (optional) (default: null) no reactive information set to share with the subscribers
|
|
46
|
+
* @template {TStateSetter} TStateSetter - The type of the setterConfig object (optional) (default: null) if a configuration is passed, the hook will return an object with the actions then all the store manipulation will be done through the actions
|
|
47
|
+
*/
|
|
48
|
+
this.config = {
|
|
49
|
+
metadata: null,
|
|
50
|
+
};
|
|
46
51
|
/**
|
|
47
52
|
* execute once the store is created
|
|
48
53
|
* @template {TState} TState - The type of the state object
|
|
@@ -110,7 +115,7 @@ class GlobalStore {
|
|
|
110
115
|
* gets a clone of the metadata
|
|
111
116
|
* @returns {TMetadata} - The metadata clone
|
|
112
117
|
* */
|
|
113
|
-
this.getMetadataClone = () => jsonStorageFormatter.clone(this.metadata);
|
|
118
|
+
this.getMetadataClone = () => { var _a, _b; return jsonStorageFormatter.clone((_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.metadata) !== null && _b !== void 0 ? _b : null); };
|
|
114
119
|
/**
|
|
115
120
|
* set the state and update all the subscribers
|
|
116
121
|
* @param {StateSetter<TState>} setter - The setter function or the value to set
|
|
@@ -133,11 +138,12 @@ class GlobalStore {
|
|
|
133
138
|
* @param {StateSetter<TMetadata>} setter - The setter function or the value to set
|
|
134
139
|
* */
|
|
135
140
|
this.setMetadata = (setter) => {
|
|
141
|
+
var _a;
|
|
136
142
|
const isSetterFunction = typeof setter === 'function';
|
|
137
143
|
const metadata = isSetterFunction
|
|
138
144
|
? setter(this.getMetadataClone())
|
|
139
145
|
: setter;
|
|
140
|
-
this.
|
|
146
|
+
this.config = Object.assign(Object.assign({}, ((_a = this.config) !== null && _a !== void 0 ? _a : {})), { metadata });
|
|
141
147
|
};
|
|
142
148
|
/**
|
|
143
149
|
* get the parameters object to pass to the callback functions (onInit, onStateChanged, onSubscribed, computePreventStateChange)
|
|
@@ -295,6 +301,7 @@ class GlobalStore {
|
|
|
295
301
|
} })), {});
|
|
296
302
|
return actions;
|
|
297
303
|
};
|
|
304
|
+
this.config = Object.assign({ metadata: null }, (config !== null && config !== void 0 ? config : {}));
|
|
298
305
|
this.onInitializeStore();
|
|
299
306
|
}
|
|
300
307
|
/**
|
|
@@ -100,9 +100,10 @@ export type StateChangesParam<TState, TMetadata, TStateSetter extends ActionColl
|
|
|
100
100
|
* @template {ActionCollectionConfig<TState,TMetadata> | null} TStateSetter - the configuration of the API (optional) - if you don't pass an API as a parameter, you can pass null
|
|
101
101
|
* */
|
|
102
102
|
export type GlobalStoreConfig<TState, TMetadata, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> = StateSetter<TState>> = {
|
|
103
|
+
metadata?: TMetadata;
|
|
103
104
|
onInit?: (parameters: StateConfigCallbackParam<TState, TMetadata, TStateSetter>) => void;
|
|
104
105
|
onStateChanged?: (parameters: StateChangesParam<TState, TMetadata, TStateSetter>) => void;
|
|
105
106
|
onSubscribed?: (parameters: StateConfigCallbackParam<TState, TMetadata, TStateSetter>) => void;
|
|
106
107
|
computePreventStateChange?: (parameters: StateChangesParam<TState, TMetadata, TStateSetter>) => boolean;
|
|
107
|
-
};
|
|
108
|
+
} | null;
|
|
108
109
|
//# sourceMappingURL=GlobalStore.types.d.ts.map
|
|
@@ -1 +1 @@
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