react-native-global-state-hooks 7.0.2 → 8.0.0

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.
Files changed (50) hide show
  1. package/GlobalStore.d.ts +29 -0
  2. package/GlobalStore.js +1 -0
  3. package/GlobalStoreAbstract.d.ts +9 -0
  4. package/GlobalStoreAbstract.js +1 -0
  5. package/README.md +340 -334
  6. package/asyncStorageWrapper.d.ts +8 -0
  7. package/asyncStorageWrapper.js +1 -0
  8. package/bundle.js +1 -0
  9. package/combineRetrieverAsynchronously.d.ts +1 -0
  10. package/combineRetrieverAsynchronously.js +1 -0
  11. package/combineRetrieverEmitterAsynchronously.d.ts +1 -0
  12. package/combineRetrieverEmitterAsynchronously.js +1 -0
  13. package/createContext.d.ts +1 -0
  14. package/createContext.js +1 -0
  15. package/createCustomGlobalState.d.ts +22 -0
  16. package/createCustomGlobalState.js +1 -0
  17. package/createGlobalState.d.ts +20 -0
  18. package/createGlobalState.js +1 -0
  19. package/debounce.d.ts +1 -0
  20. package/debounce.js +1 -0
  21. package/getAsyncStorageItem.d.ts +2 -0
  22. package/getAsyncStorageItem.js +1 -0
  23. package/index.d.ts +23 -0
  24. package/isRecord.d.ts +1 -0
  25. package/isRecord.js +1 -0
  26. package/package.json +94 -8
  27. package/setAsyncStorageItem.d.ts +2 -0
  28. package/setAsyncStorageItem.js +1 -0
  29. package/shallowCompare.d.ts +1 -0
  30. package/shallowCompare.js +1 -0
  31. package/throwWrongKeyOnActionCollectionConfig.d.ts +1 -0
  32. package/throwWrongKeyOnActionCollectionConfig.js +1 -0
  33. package/types.d.ts +83 -0
  34. package/types.js +1 -0
  35. package/uniqueId.d.ts +1 -0
  36. package/uniqueId.js +1 -0
  37. package/uniqueSymbol.d.ts +1 -0
  38. package/uniqueSymbol.js +1 -0
  39. package/useConstantValueRef.d.ts +1 -0
  40. package/useConstantValueRef.js +1 -0
  41. package/webpack.config.js +89 -0
  42. package/lib/bundle.js +0 -2
  43. package/lib/bundle.js.LICENSE.txt +0 -1
  44. package/lib/src/GlobalStore.context.d.ts +0 -6
  45. package/lib/src/GlobalStore.d.ts +0 -28
  46. package/lib/src/GlobalStore.functionHooks.d.ts +0 -19
  47. package/lib/src/GlobalStore.types.d.ts +0 -124
  48. package/lib/src/GlobalStore.utils.d.ts +0 -16
  49. package/lib/src/GlobalStoreAbstract.d.ts +0 -16
  50. package/lib/src/index.d.ts +0 -12
@@ -1,6 +0,0 @@
1
- import { TMetadataResult } from "GlobalStore.types";
2
- import React from "react";
3
- import { ActionCollectionConfig, createStateConfig, StateHook, StateSetter, ActionCollectionResult, StateGetter } from "react-hooks-global-states";
4
- export declare const createStatefulContext: <TState, TMetadata = null, TActions extends ActionCollectionConfig<TState, TMetadata> = null>(initialValue: TState, parameters?: createStateConfig<TState, TMetadata, TActions>) => readonly [() => [hook: StateHook<TState, keyof TActions extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadataResult<TMetadata>, TActions>, TMetadataResult<TMetadata>>, stateRetriever: StateGetter<TState>, stateMutator: keyof TActions extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TActions>], React.FC<React.PropsWithChildren<{
5
- initialValue?: Partial<TState>;
6
- }>>];
@@ -1,28 +0,0 @@
1
- import * as GlobalStoreBase from "react-hooks-global-states";
2
- import { GlobalStoreConfig, StateChangesParam, TMetadataResult, ActionCollectionConfig, ActionCollectionResult } from "./GlobalStore.types";
3
- import { GlobalStoreAbstract, MetadataGetter, StateGetter, StateSetter } from "react-hooks-global-states";
4
- export declare class GlobalStore<TState, TMetadata = null, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> = StateSetter<TState>> extends GlobalStoreAbstract<TState, TMetadata, NonNullable<TStateSetter>> {
5
- protected config: GlobalStoreConfig<TState, TMetadata, TStateSetter>;
6
- constructor(state: TState, config?: GlobalStoreConfig<TState, TMetadata, TStateSetter>, actionsConfig?: TStateSetter | null);
7
- protected getMetadata: () => TMetadata | TMetadataResult<TMetadata>;
8
- /**
9
- * Returns an array with the a function to get the state, the state setter or the actions map, and a function to get the metadata
10
- * @returns {[() => TState, TStateSetter, () => TMetadata]} - The state getter, the state setter or the actions map, the metadata getter
11
- * */
12
- getHookDecoupled: () => [
13
- StateGetter<TState>,
14
- keyof TStateSetter extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TStateSetter>,
15
- MetadataGetter<TMetadataResult<TMetadata>>
16
- ];
17
- /**
18
- * Returns a custom hook that allows to handle a global state
19
- * @returns {[TState, TStateSetter, TMetadata]} - The state, the state setter or the actions map, the metadata
20
- * */
21
- getHook: () => () => [
22
- state: TState,
23
- stateMutator: keyof TStateSetter extends never ? StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TStateSetter>,
24
- metadata: MetadataGetter<TMetadataResult<TMetadata>>
25
- ];
26
- protected onInitialize: ({ setState, getState, setMetadata, }: GlobalStoreBase.StateConfigCallbackParam<TState, TMetadata>) => void;
27
- protected onChange: ({ getState, }: StateChangesParam<TState, TMetadata, TStateSetter>) => void;
28
- }
@@ -1,19 +0,0 @@
1
- import * as GlobalStoreBase from "react-hooks-global-states";
2
- import { createStateConfig, CustomGlobalHookParams, TMetadataResult, ActionCollectionConfig, ActionCollectionResult } from "./GlobalStore.types";
3
- import { AvoidNever, CustomGlobalHookBuilderParams } from "react-hooks-global-states";
4
- /**
5
- * Creates a global state with the given state and config.
6
- * @returns {} [HOOK, DECOUPLED_GETTER, DECOUPLED_SETTER] this is an array with the hook, the decoupled getState function and the decoupled setter of the state
7
- */
8
- export declare const createGlobalStateWithDecoupledFuncs: <TState, TMetadata = null, TActions extends ActionCollectionConfig<TState, TMetadata> = null>(state: TState, { actions, ...config }?: createStateConfig<TState, TMetadata, TActions>) => [hook: GlobalStoreBase.StateHook<TState, keyof TActions extends never ? GlobalStoreBase.StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TActions>, TMetadataResult<TMetadata>>, stateRetriever: GlobalStoreBase.StateGetter<TState>, stateMutator: keyof TActions extends never ? GlobalStoreBase.StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TActions>];
9
- /**
10
- * Creates a global hook that can be used to access the state and actions across the application
11
- * @returns {} - () => [TState, Setter, TMetadata] the hook that can be used to access the state and the setter of the state
12
- */
13
- export declare const createGlobalState: <TState, TMetadata = null, TActions extends ActionCollectionConfig<TState, TMetadata> = null>(state: TState, config?: createStateConfig<TState, TMetadata, TActions>) => GlobalStoreBase.StateHook<TState, keyof TActions extends never ? GlobalStoreBase.StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TActions>, TMetadataResult<TMetadata>>;
14
- /**
15
- * @description
16
- * Use this function to create a custom global store.
17
- * You can use this function to create a store with async storage.
18
- */
19
- export declare const createCustomGlobalStateWithDecoupledFuncs: <TInheritMetadata extends Record<string, unknown>, TCustomConfig extends Record<string, unknown>>({ onInitialize, onChange, }: GlobalStoreBase.CustomGlobalHookBuilderParams<GlobalStoreBase.AvoidNever<TInheritMetadata>, GlobalStoreBase.AvoidNever<TCustomConfig>>) => <TState, TMetadata = null, TActions extends ActionCollectionConfig<TState, TMetadata> = null>(state: TState, { config: customConfig, onInit, onStateChanged, ...parameters }?: GlobalStoreBase.CustomGlobalHookParams<TCustomConfig, TState, TMetadata, TActions>) => [hook: GlobalStoreBase.StateHook<TState, keyof TActions extends never ? GlobalStoreBase.StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TActions>, TMetadataResult<TMetadata>>, stateRetriever: GlobalStoreBase.StateGetter<TState>, stateMutator: keyof TActions extends never ? GlobalStoreBase.StateSetter<TState> : ActionCollectionResult<TState, TMetadata, TActions>];
@@ -1,124 +0,0 @@
1
- import * as GlobalStoreBase from "react-hooks-global-states";
2
- import { MetadataSetter, StateSetter } from "react-hooks-global-states";
3
- /**
4
- * Configuration of the async storage (optional)
5
- */
6
- export type AsyncStorageConfig = {
7
- /**
8
- * async storage configuration
9
- */
10
- asyncStorage?: {
11
- /**
12
- * The key of the async storage
13
- */
14
- key?: string;
15
- /**
16
- * The function used to encrypt the async storage, it can be a custom function or a boolean value (true = atob)
17
- */
18
- encrypt?: boolean | ((value: string) => string);
19
- /**
20
- * The function used to decrypt the async storage, it can be a custom function or a boolean value (true = atob)
21
- */
22
- decrypt?: boolean | ((value: string) => string);
23
- };
24
- };
25
- export type TMetadataResult<TMetadata = null> = {
26
- isAsyncStorageReady?: boolean;
27
- asyncStorageKey?: string;
28
- } & Omit<NonNullable<TMetadata>, "isAsyncStorageReady" | "asyncStorageKey">;
29
- /**
30
- * Callbacks to be passed to the configurations function of the store
31
- * @template {TState} TState - The state type
32
- * @template {TMetadata} TMetadata - The metadata type
33
- * @property {StateSetter<TState>} setMetadata - Set the metadata
34
- * @property {StateSetter<TState>} setState - Set the state
35
- * @property {() => TState} getState - Get the state
36
- * @property {() => TMetadata} getMetadata - Get the metadata
37
- * @property {ActionCollectionResult<TState, TMetadata>} actions - The actions collection if any
38
- **/
39
- export type StoreTools<TState = any, TMetadata = any, TActions = any> = Omit<GlobalStoreBase.StoreTools<TState, TMetadata, TActions>, "getMetadata" | "setMetadata"> & {
40
- getMetadata: () => TMetadataResult<TMetadata>;
41
- setMetadata: MetadataSetter<TMetadataResult<TMetadata>> | MetadataSetter<TMetadata>;
42
- };
43
- /**
44
- * Basic contract for the storeActionsConfig configuration
45
- * @template {TState} TState - The state type
46
- * @template {TMetadata} TMetadata - The metadata type
47
- * @property {string} key - The action name
48
- * @property {(...parameters: unknown[]) => (storeTools: { setMetadata: MetadataSetter<TMetadata>; setState: StateSetter<TState>; getState: () => TState; getMetadata: () => TMetadata; }) => unknown | void} value - The action function
49
- * @returns {ActionCollectionConfig<TState, TMetadata>} result - The action collection configuration
50
- */
51
- export interface ActionCollectionConfig<TState, TMetadata = null> {
52
- [key: string]: (...parameters: any[]) => (storeTools: any) => unknown | void;
53
- }
54
- /**
55
- * This is the actions object returned by the hook when you pass an storeActionsConfig configuration
56
- * if you pass an storeActionsConfig configuration, the hook will return an object with the actions
57
- * whatever data manipulation of the state should be executed through the custom actions with as access to the state and metadata
58
- * @template {TState} TState - The state type
59
- * @template {TMetadata} TMetadata - The metadata type
60
- * @template {TStateSetter} TStateSetter - The storeActionsConfig type (optional) - if you pass an storeActionsConfig the hook will return an object with the actions
61
- *
62
- * @example
63
- *
64
- * const store = new GlobalStore(0, {
65
- * increment: () => ({ setState }) => {
66
- * setState((state) => state + 1);
67
- * },
68
- * decrement: () => ({ setState }) => {
69
- * setState((state) => state - 1);
70
- * },
71
- * });
72
- *
73
- * const [state, actions] = store.getHook();
74
- *
75
- * actions.increment();
76
- * actions.decrement();
77
- *
78
- * console.log(state); // 0
79
- */
80
- export type ActionCollectionResult<TState, TMetadata, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> = StateSetter<TState>> = TStateSetter extends ActionCollectionConfig<TState, TMetadata> ? {
81
- [key in keyof TStateSetter]: (...params: Parameters<TStateSetter[key]>) => ReturnType<ReturnType<TStateSetter[key]>>;
82
- } : null;
83
- /**
84
- * Common parameters of the store configuration callback functions
85
- * @param {StateSetter<TState>} setState - add a new value to the state
86
- * @param {() => TState} getState - get the current state
87
- * @param {MetadataSetter<TMetadata>} setMetadata - add a new value to the metadata
88
- * @param {() => TMetadata} getMetadata - get the current metadata
89
- * @param {ActionCollectionResult<TState, ActionCollectionConfig<TState, TMetadata>> | null} actions - the actions object returned by the hook when you pass an storeActionsConfig configuration otherwise null
90
- * @template {TState} TState - The state type
91
- * @template {TMetadata} TMetadata - The metadata type
92
- * @template {TStateSetter} TStateSetter - The storeActionsConfig type (optional) - if you pass an storeActionsConfig the hook will return an object with the actions
93
- * @template {ActionCollectionResult<TState, TStateSetter>} TStateSetter - the result of the API (optional) - if you don't pass an API as a parameter, you can pass null
94
- * */
95
- export type StateConfigCallbackParam<TState = any, TMetadata = null, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | GlobalStoreBase.StateSetter<TState> = GlobalStoreBase.StateSetter<TState>> = {
96
- actions: ActionCollectionResult<TState, TMetadata, TStateSetter>;
97
- } & StoreTools<TState, TMetadata>;
98
- /**
99
- * Parameters of the onStateChanged callback function
100
- * @template {TState} TState - The state type
101
- * @template {TMetadata} TMetadata - The metadata type
102
- * @template {TStateSetter} TStateSetter - The storeActionsConfig type (optional) - if you pass an storeActionsConfig the hook will return an object with the actions
103
- */
104
- export type StateChangesParam<TState = any, TMetadata = null, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | GlobalStoreBase.StateSetter<TState> = GlobalStoreBase.StateSetter<TState>> = StateConfigCallbackParam<TState, TMetadata, TStateSetter> & GlobalStoreBase.StateChanges<TState>;
105
- /**
106
- * Configuration of the store (optional) - if you don't need to use the store configuration you don't need to pass this parameter
107
- * @param {StateConfigCallbackParam<TState, TMetadata> => void} onInit - callback function called when the store is initialized
108
- * @param {StateConfigCallbackParam<TState, TMetadata> => void} onSubscribed - callback function called every time a component is subscribed to the store
109
- * @param {StateChangesParam<TState, TMetadata> => boolean} computePreventStateChange - callback function called every time the state is changed and it allows you to prevent the state change
110
- * @param {StateChangesParam<TState, TMetadata> => void} onStateChanged - callback function called every time the state is changed
111
- * @template TState - the type of the state
112
- * @template TMetadata - the type of the metadata (optional) - if you don't pass an metadata as a parameter, you can pass null
113
- * @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
114
- * */
115
- export type GlobalStoreConfig<TState, TMetadata, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | GlobalStoreBase.StateSetter<TState> = GlobalStoreBase.StateSetter<TState>> = GlobalStoreBase.GlobalStoreConfig<TState, TMetadata, TStateSetter> & AsyncStorageConfig;
116
- /**
117
- * Configuration of the state (optional) - if you don't need to use the state configuration you don't need to pass this parameter
118
- */
119
- export type createStateConfig<TState, TMetadata, TActions extends ActionCollectionConfig<TState, TMetadata> | null = null> = GlobalStoreBase.createStateConfig<TState, TMetadata, TActions> & AsyncStorageConfig;
120
- /**
121
- * @description
122
- * Configuration of the custom global hook
123
- */
124
- export type CustomGlobalHookParams<TCustomConfig, TState, TMetadata, TActions extends ActionCollectionConfig<TState, TMetadata> | null> = GlobalStoreBase.CustomGlobalHookParams<TCustomConfig, TState, TMetadata, TActions> & AsyncStorageConfig;
@@ -1,16 +0,0 @@
1
- import { GlobalStoreConfig } from "./GlobalStore.types";
2
- /**
3
- * @description
4
- * Get an item from local storage using the config provided.
5
- */
6
- export declare const getAsyncStorageItem: <TState, TMetadata = null>({ config, }: {
7
- config: GlobalStoreConfig<TState, TMetadata>;
8
- }) => Promise<TState>;
9
- /**
10
- * @description
11
- * Set an item to local storage using the config provided.
12
- */
13
- export declare const setAsyncStorageItem: <TState, TMetadata = null>({ item, config, }: {
14
- config: GlobalStoreConfig<TState, TMetadata>;
15
- item: TState;
16
- }) => Promise<void>;
@@ -1,16 +0,0 @@
1
- import { StateSetter } from "react-hooks-global-states";
2
- import { GlobalStore } from "./GlobalStore";
3
- import { GlobalStoreConfig, StateConfigCallbackParam, StateChangesParam, ActionCollectionConfig } from "./GlobalStore.types";
4
- /**
5
- * @description
6
- * Use this class to extends the capabilities of the GlobalStore.
7
- * by implementing the abstract methods onInitialize and onChange.
8
- * You can use this class to create a store with async storage.
9
- */
10
- export declare abstract class GlobalStoreAbstract<TState, TMetadata = null, TStateSetter extends ActionCollectionConfig<TState, TMetadata> | StateSetter<TState> = StateSetter<TState>> extends GlobalStore<TState, TMetadata, TStateSetter> {
11
- constructor(state: TState, config?: GlobalStoreConfig<TState, TMetadata, TStateSetter>, actionsConfig?: TStateSetter | null);
12
- protected onInit: (parameters: StateConfigCallbackParam<TState, TMetadata, TStateSetter>) => void;
13
- protected onStateChanged: (parameters: StateChangesParam<TState, TMetadata, TStateSetter>) => void;
14
- protected abstract onInitialize: ({ setState, setMetadata, getMetadata, getState, actions, }: StateConfigCallbackParam<TState, TMetadata, TStateSetter>) => void;
15
- protected abstract onChange: ({ setState, setMetadata, getMetadata, getState, actions, }: StateChangesParam<TState, TMetadata, TStateSetter>) => void;
16
- }
@@ -1,12 +0,0 @@
1
- /**
2
- * The commented exports are the ones that are overridden by this library.
3
- * The intention is to extend the capabilities to specially web development.
4
- * The original library is intended to be used just with react,
5
- */
6
- export { clone, isNil, isNumber, isBoolean, isString, isDate, isRegex, isFunction, isPrimitive, formatFromStore, formatToStore, throwNoSubscribersWereAdded, createDerivate, createDerivateEmitter, shallowCompare, debounce, combineAsyncGettersEmitter, combineRetrieverEmitterAsynchronously, combineAsyncGetters, combineRetrieverAsynchronously, StateSetter, StateHook, AvoidNever, MetadataSetter, StateChanges, UseHookConfig, UnsubscribeCallback, SubscribeCallbackConfig, SubscribeCallback, SubscriberCallback, StateGetter, Subscribe, CustomGlobalHookBuilderParams, SelectorCallback, SubscribeToEmitter, SubscriberParameters, SubscriptionCallback, SetStateCallback, } from "react-hooks-global-states";
7
- export { AsyncStorageConfig, TMetadataResult, StoreTools, ActionCollectionConfig, ActionCollectionResult, StateConfigCallbackParam, StateChangesParam, GlobalStoreConfig, createStateConfig, CustomGlobalHookParams, } from "./GlobalStore.types";
8
- export { GlobalStore } from "./GlobalStore";
9
- export { GlobalStoreAbstract } from "./GlobalStoreAbstract";
10
- export { createGlobalStateWithDecoupledFuncs, createGlobalState, createCustomGlobalStateWithDecoupledFuncs, } from "./GlobalStore.functionHooks";
11
- export { getAsyncStorageItem, setAsyncStorageItem } from "./GlobalStore.utils";
12
- export * from "./GlobalStore.context";