react-native-global-state-hooks 2.1.17 → 2.1.18
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 +23 -8
- package/lib/GlobalStore.d.ts +1 -1
- package/lib/GlobalStore.d.ts.map +1 -1
- package/lib/GlobalStore.js +7 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -140,6 +140,17 @@ Here is an example of how you could create your custom store that for example st
|
|
|
140
140
|
You could just use this code right as it is by just adding also into your project **@react-native-async-storage** or whatever another async storage library.
|
|
141
141
|
|
|
142
142
|
```ts
|
|
143
|
+
import { GlobalStore as GlobalStoreBase } from 'react-native-global-state-hooks';
|
|
144
|
+
import { formatFromStore, formatToStore } from 'json-storage-formatter';
|
|
145
|
+
import asyncStorage from '@react-native-async-storage/async-storage';
|
|
146
|
+
|
|
147
|
+
import {
|
|
148
|
+
StateSetter,
|
|
149
|
+
StateConfigCallbackParam,
|
|
150
|
+
StateChangesParam,
|
|
151
|
+
ActionCollectionConfig,
|
|
152
|
+
} from 'react-native-global-state-hooks/lib/GlobalStore.types';
|
|
153
|
+
|
|
143
154
|
/**
|
|
144
155
|
* GlobalStore is an store that could also persist the state in the async storage
|
|
145
156
|
* @template {TState} TState - The state of the store
|
|
@@ -150,7 +161,10 @@ export class GlobalStore<
|
|
|
150
161
|
TState,
|
|
151
162
|
// this restriction is needed to avoid the consumers to set the isAsyncStorageReady property from outside the store,
|
|
152
163
|
// ... even when the value will be ignored is better to avoid it to avoid confusion
|
|
153
|
-
TMetadata extends { readonly isAsyncStorageReady
|
|
164
|
+
TMetadata extends { readonly isAsyncStorageReady?: never } & Record<
|
|
165
|
+
string,
|
|
166
|
+
unknown
|
|
167
|
+
> = {},
|
|
154
168
|
TStateSetter extends StorageSetter<TState, TMetadata> = StateSetter<TState>
|
|
155
169
|
> extends GlobalStoreBase<TState, StorageMetadata<TMetadata>, TStateSetter> {
|
|
156
170
|
/**
|
|
@@ -160,7 +174,7 @@ export class GlobalStore<
|
|
|
160
174
|
* @template {TMetadata} TMetadata - The metadata of the store
|
|
161
175
|
* @template {TStateSetter} TStateSetter - The storeActionsConfig of the store
|
|
162
176
|
**/
|
|
163
|
-
protected config: StorageConfig<TState, TMetadata, TStateSetter
|
|
177
|
+
protected config: StorageConfig<TState, TMetadata, TStateSetter> = {};
|
|
164
178
|
|
|
165
179
|
/**
|
|
166
180
|
* Creates a new instance of the GlobalStore
|
|
@@ -179,20 +193,21 @@ export class GlobalStore<
|
|
|
179
193
|
config: StorageConfig<TState, TMetadata, TStateSetter> | null = null,
|
|
180
194
|
setterConfig: TStateSetter | null = null
|
|
181
195
|
) {
|
|
182
|
-
const { onInit, asyncStorageKey, ...configParameters } =
|
|
196
|
+
const { onInit, asyncStorageKey, ...configParameters } =
|
|
197
|
+
config ?? ({} as StorageConfig<TState, TMetadata, TStateSetter>);
|
|
183
198
|
|
|
184
199
|
super(state, configParameters, setterConfig as TStateSetter);
|
|
185
200
|
|
|
186
201
|
// if there is not async storage key this is not a persistent store
|
|
187
|
-
const isAsyncStorageReady = asyncStorageKey ? false : null;
|
|
202
|
+
const isAsyncStorageReady: boolean | null = asyncStorageKey ? false : null;
|
|
188
203
|
|
|
189
204
|
this.config = {
|
|
190
205
|
...config,
|
|
191
206
|
metadata: {
|
|
192
|
-
...configParameters.metadata,
|
|
207
|
+
...((configParameters.metadata ?? {}) as TMetadata),
|
|
193
208
|
isAsyncStorageReady,
|
|
194
209
|
},
|
|
195
|
-
}
|
|
210
|
+
};
|
|
196
211
|
|
|
197
212
|
const hasInitCallbacks = !!(asyncStorageKey || onInit);
|
|
198
213
|
if (!hasInitCallbacks) return;
|
|
@@ -258,7 +273,7 @@ export class GlobalStore<
|
|
|
258
273
|
* @template {TMetadata} TMetadata - The metadata type which also contains the isAsyncStorageReady property
|
|
259
274
|
*/
|
|
260
275
|
type StorageMetadata<TMetadata> = Omit<TMetadata, 'isAsyncStorageReady'> & {
|
|
261
|
-
readonly isAsyncStorageReady
|
|
276
|
+
readonly isAsyncStorageReady?: boolean | null;
|
|
262
277
|
};
|
|
263
278
|
|
|
264
279
|
/**
|
|
@@ -280,7 +295,7 @@ type StorageSetter<TState, TMetadata> =
|
|
|
280
295
|
*/
|
|
281
296
|
type StorageConfig<
|
|
282
297
|
TState,
|
|
283
|
-
TMetadata extends { readonly isAsyncStorageReady
|
|
298
|
+
TMetadata extends { readonly isAsyncStorageReady?: never },
|
|
284
299
|
TStateSetter extends
|
|
285
300
|
| ActionCollectionConfig<TState, StorageMetadata<TMetadata>>
|
|
286
301
|
| StateSetter<TState>
|
package/lib/GlobalStore.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { formatFromStore, formatToStore } from 'json-storage-formatter';
|
|
2
2
|
import { Dispatch, SetStateAction } from 'react';
|
|
3
3
|
import { ActionCollectionConfig, StateSetter, GlobalStoreConfig, ActionCollectionResult, StateConfigCallbackParam } from './GlobalStore.types';
|
|
4
4
|
/**
|
package/lib/GlobalStore.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GlobalStore.d.ts","sourceRoot":"","sources":["../src/GlobalStore.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"GlobalStore.d.ts","sourceRoot":"","sources":["../src/GlobalStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAExE,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;;;;;KAKK;AACL,qBAAa,WAAW,CACtB,MAAM,EACN,SAAS,GAAG,IAAI,EAChB,YAAY,SACR,sBAAsB,CAAC,MAAM,EAAE,SAAS,CAAC,GACzC,WAAW,CAAC,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true });
|
|
3
|
-
exports.GlobalStore = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
exports.GlobalStore = exports.formatToStore = exports.formatFromStore = void 0;
|
|
4
|
+
var json_storage_formatter_1 = require("json-storage-formatter");
|
|
5
|
+
Object.defineProperty(exports, "formatFromStore", { enumerable: true, get: function () { return json_storage_formatter_1.formatFromStore; } });
|
|
6
|
+
Object.defineProperty(exports, "formatToStore", { enumerable: true, get: function () { return json_storage_formatter_1.formatToStore; } });
|
|
7
|
+
const json_storage_formatter_2 = require("json-storage-formatter");
|
|
7
8
|
const react_1 = require("react");
|
|
8
9
|
const throwWrongKeyOnActionCollectionConfig = (action_key) => {
|
|
9
10
|
throw new Error(`[WRONG CONFIGURATION!]: Every key inside the storeActionsConfig must be a higher order function that returns a function \n[${action_key}]: key is not a valid function, try something like this: \n{\n
|
|
@@ -113,12 +114,12 @@ class GlobalStore {
|
|
|
113
114
|
* gets a clone of the state
|
|
114
115
|
* @returns {TState} - The state clone
|
|
115
116
|
* */
|
|
116
|
-
this.getStateClone = () => (0,
|
|
117
|
+
this.getStateClone = () => (0, json_storage_formatter_2.clone)(this.state);
|
|
117
118
|
/**
|
|
118
119
|
* gets a clone of the metadata
|
|
119
120
|
* @returns {TMetadata} - The metadata clone
|
|
120
121
|
* */
|
|
121
|
-
this.getMetadataClone = () => { var _a, _b; return (0,
|
|
122
|
+
this.getMetadataClone = () => { var _a, _b; return (0, json_storage_formatter_2.clone)((_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.metadata) !== null && _b !== void 0 ? _b : null); };
|
|
122
123
|
/**
|
|
123
124
|
* set the state and update all the subscribers
|
|
124
125
|
* @param {StateSetter<TState>} setter - The setter function or the value to set
|
package/package.json
CHANGED