react-native-global-state-hooks 2.1.17 → 2.1.19
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 +25 -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
|
@@ -4,6 +4,8 @@ This is a package to easily handling global-state across your react-native-compo
|
|
|
4
4
|
|
|
5
5
|
This utility uses the **useState** hook within a subscription pattern and **HOFs** to create a more intuitive, atomic and easy way of sharing state between components... You can see the introduction video [here!](https://www.youtube.com/watch?v=WfoMhO1zZ04&t=8s)
|
|
6
6
|
|
|
7
|
+
To see TODO-LIST with the hooks and async storage take a look here: [todo-list-with-global-hooks](https://github.com/johnny-quesada-developer/todo-list-with-global-hooks.git)
|
|
8
|
+
|
|
7
9
|
For seen a running example of the hooks, you can check the following link: [react-global-state-hooks-example](https://johnny-quesada-developer.github.io/react-global-state-hooks-example/)
|
|
8
10
|
|
|
9
11
|
...
|
|
@@ -140,6 +142,17 @@ Here is an example of how you could create your custom store that for example st
|
|
|
140
142
|
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
143
|
|
|
142
144
|
```ts
|
|
145
|
+
import { GlobalStore as GlobalStoreBase } from 'react-native-global-state-hooks';
|
|
146
|
+
import { formatFromStore, formatToStore } from 'json-storage-formatter';
|
|
147
|
+
import asyncStorage from '@react-native-async-storage/async-storage';
|
|
148
|
+
|
|
149
|
+
import {
|
|
150
|
+
StateSetter,
|
|
151
|
+
StateConfigCallbackParam,
|
|
152
|
+
StateChangesParam,
|
|
153
|
+
ActionCollectionConfig,
|
|
154
|
+
} from 'react-native-global-state-hooks/lib/GlobalStore.types';
|
|
155
|
+
|
|
143
156
|
/**
|
|
144
157
|
* GlobalStore is an store that could also persist the state in the async storage
|
|
145
158
|
* @template {TState} TState - The state of the store
|
|
@@ -150,7 +163,10 @@ export class GlobalStore<
|
|
|
150
163
|
TState,
|
|
151
164
|
// this restriction is needed to avoid the consumers to set the isAsyncStorageReady property from outside the store,
|
|
152
165
|
// ... even when the value will be ignored is better to avoid it to avoid confusion
|
|
153
|
-
TMetadata extends { readonly isAsyncStorageReady
|
|
166
|
+
TMetadata extends { readonly isAsyncStorageReady?: never } & Record<
|
|
167
|
+
string,
|
|
168
|
+
unknown
|
|
169
|
+
> = {},
|
|
154
170
|
TStateSetter extends StorageSetter<TState, TMetadata> = StateSetter<TState>
|
|
155
171
|
> extends GlobalStoreBase<TState, StorageMetadata<TMetadata>, TStateSetter> {
|
|
156
172
|
/**
|
|
@@ -160,7 +176,7 @@ export class GlobalStore<
|
|
|
160
176
|
* @template {TMetadata} TMetadata - The metadata of the store
|
|
161
177
|
* @template {TStateSetter} TStateSetter - The storeActionsConfig of the store
|
|
162
178
|
**/
|
|
163
|
-
protected config: StorageConfig<TState, TMetadata, TStateSetter
|
|
179
|
+
protected config: StorageConfig<TState, TMetadata, TStateSetter> = {};
|
|
164
180
|
|
|
165
181
|
/**
|
|
166
182
|
* Creates a new instance of the GlobalStore
|
|
@@ -179,20 +195,21 @@ export class GlobalStore<
|
|
|
179
195
|
config: StorageConfig<TState, TMetadata, TStateSetter> | null = null,
|
|
180
196
|
setterConfig: TStateSetter | null = null
|
|
181
197
|
) {
|
|
182
|
-
const { onInit, asyncStorageKey, ...configParameters } =
|
|
198
|
+
const { onInit, asyncStorageKey, ...configParameters } =
|
|
199
|
+
config ?? ({} as StorageConfig<TState, TMetadata, TStateSetter>);
|
|
183
200
|
|
|
184
201
|
super(state, configParameters, setterConfig as TStateSetter);
|
|
185
202
|
|
|
186
203
|
// if there is not async storage key this is not a persistent store
|
|
187
|
-
const isAsyncStorageReady = asyncStorageKey ? false : null;
|
|
204
|
+
const isAsyncStorageReady: boolean | null = asyncStorageKey ? false : null;
|
|
188
205
|
|
|
189
206
|
this.config = {
|
|
190
207
|
...config,
|
|
191
208
|
metadata: {
|
|
192
|
-
...configParameters.metadata,
|
|
209
|
+
...((configParameters.metadata ?? {}) as TMetadata),
|
|
193
210
|
isAsyncStorageReady,
|
|
194
211
|
},
|
|
195
|
-
}
|
|
212
|
+
};
|
|
196
213
|
|
|
197
214
|
const hasInitCallbacks = !!(asyncStorageKey || onInit);
|
|
198
215
|
if (!hasInitCallbacks) return;
|
|
@@ -258,7 +275,7 @@ export class GlobalStore<
|
|
|
258
275
|
* @template {TMetadata} TMetadata - The metadata type which also contains the isAsyncStorageReady property
|
|
259
276
|
*/
|
|
260
277
|
type StorageMetadata<TMetadata> = Omit<TMetadata, 'isAsyncStorageReady'> & {
|
|
261
|
-
readonly isAsyncStorageReady
|
|
278
|
+
readonly isAsyncStorageReady?: boolean | null;
|
|
262
279
|
};
|
|
263
280
|
|
|
264
281
|
/**
|
|
@@ -280,7 +297,7 @@ type StorageSetter<TState, TMetadata> =
|
|
|
280
297
|
*/
|
|
281
298
|
type StorageConfig<
|
|
282
299
|
TState,
|
|
283
|
-
TMetadata extends { readonly isAsyncStorageReady
|
|
300
|
+
TMetadata extends { readonly isAsyncStorageReady?: never },
|
|
284
301
|
TStateSetter extends
|
|
285
302
|
| ActionCollectionConfig<TState, StorageMetadata<TMetadata>>
|
|
286
303
|
| 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.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AAC,iBAAiB,QAAO,OAAO,CAgBvC;IAEF;;;;;;OAMG;IACH,SAAS,CAAC,eAAe;gBAIf,YAAY,MAAM,CAAC;0BACT,cAAc,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;eAwD9D;IAEF;;;;SAIK;IACL,SAAS,CAAC,kBAAkB;0BAGR,cAAc,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;UAC5D,uBAAuB,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,CAgDzD;CACH"}
|
package/lib/GlobalStore.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: 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