react-hooks-global-states 4.0.7 → 5.0.1
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 +51 -68
- package/lib/bundle.js +1 -1
- package/lib/src/GlobalStore.combiners.d.ts +10 -24
- package/lib/src/GlobalStore.context.d.ts +33 -170
- package/lib/src/GlobalStore.d.ts +62 -80
- package/lib/src/GlobalStore.functionHooks.d.ts +33 -120
- package/lib/src/GlobalStore.types.d.ts +62 -85
- package/lib/src/GlobalStore.utils.d.ts +8 -12
- package/lib/src/GlobalStoreAbstract.d.ts +2 -3
- package/package.json +3 -3
|
@@ -1,21 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* This parameter indicate whether we should force the re-render of the subscribers even if the state is the same,
|
|
4
|
-
* Do
|
|
5
|
-
*/
|
|
6
|
-
{ forceUpdate, }?: {
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
export type StateSetter<State> = (setter: State | ((state: State) => State), meta?: {
|
|
7
3
|
/**
|
|
8
4
|
* @description you can add an identifier to the state call
|
|
9
5
|
* this will show up in the devtools to help you identify from where the state change was called
|
|
10
6
|
*/
|
|
11
|
-
identifier?: string;
|
|
7
|
+
identifier?: string | undefined;
|
|
12
8
|
/**
|
|
13
9
|
* @deprecated forceUpdate normally should not be used inside components
|
|
14
10
|
* Use this flag just in custom implementations of the global store
|
|
15
11
|
*/
|
|
16
|
-
forceUpdate?: boolean;
|
|
12
|
+
forceUpdate?: boolean | undefined;
|
|
17
13
|
}) => void;
|
|
18
|
-
export type
|
|
14
|
+
export type HookExtensions<State, StateMutator, Metadata extends BaseMetadata | unknown> = {
|
|
19
15
|
/**
|
|
20
16
|
* @description Return the state controls of the hook
|
|
21
17
|
* This selectors includes:
|
|
@@ -34,20 +30,41 @@ export type StateHook<State, StateMutator, Metadata extends BaseMetadata> = (<De
|
|
|
34
30
|
* The selector hook will be evaluated only if the result of the selector changes and the equality function returns false
|
|
35
31
|
* you can customize the equality function by passing the isEqualRoot and isEqual parameters
|
|
36
32
|
*/
|
|
37
|
-
createSelectorHook: <
|
|
33
|
+
createSelectorHook: <Derivate>(this: StateHook<State, StateMutator, Metadata>, selector: (state: State) => Derivate, args?: Omit<UseHookConfig<Derivate, State>, 'dependencies'> & {
|
|
34
|
+
name?: string;
|
|
35
|
+
}) => StateHook<Derivate, StateMutator, Metadata>;
|
|
36
|
+
createObservable: <Fragment>(this: StateHook<State, StateMutator, Metadata>, mainSelector: (state: State) => Fragment, args?: {
|
|
37
|
+
isEqual?: (current: Fragment, next: Fragment) => boolean;
|
|
38
|
+
isEqualRoot?: (current: State, next: State) => boolean;
|
|
38
39
|
name?: string;
|
|
39
|
-
}) =>
|
|
40
|
+
}) => ObservableFragment<Fragment>;
|
|
40
41
|
};
|
|
41
|
-
export type
|
|
42
|
+
export type ObservableFragment<State> = StateGetter<State> & {
|
|
43
|
+
createObservable: <Fragment>(this: ObservableFragment<State>, mainSelector: (state: State) => Fragment, args?: {
|
|
44
|
+
isEqual?: (current: Fragment, next: Fragment) => boolean;
|
|
45
|
+
isEqualRoot?: (current: State, next: State) => boolean;
|
|
46
|
+
name?: string;
|
|
47
|
+
}) => ObservableFragment<Fragment>;
|
|
48
|
+
_name: string | undefined;
|
|
49
|
+
};
|
|
50
|
+
export interface StateHook<State, StateMutator, Metadata extends BaseMetadata | unknown> extends HookExtensions<State, StateMutator, Metadata> {
|
|
51
|
+
(): Readonly<[state: State, stateMutator: StateMutator, metadata: Metadata]> & HookExtensions<State, StateMutator, Metadata>;
|
|
52
|
+
<Derivate>(selector: (state: State) => Derivate, config?: UseHookConfig<Derivate, State>): Readonly<[
|
|
53
|
+
state: Derivate,
|
|
54
|
+
stateMutator: StateMutator,
|
|
55
|
+
metadata: Metadata
|
|
56
|
+
]> & HookExtensions<Derivate, StateMutator, Metadata>;
|
|
57
|
+
}
|
|
58
|
+
export type MetadataSetter<Metadata extends BaseMetadata | unknown> = (setter: Metadata | ((metadata: Metadata) => Metadata)) => void;
|
|
42
59
|
export type StateChanges<State> = {
|
|
43
60
|
state: State;
|
|
44
|
-
previousState
|
|
45
|
-
identifier
|
|
61
|
+
previousState: State | undefined;
|
|
62
|
+
identifier: string | undefined;
|
|
46
63
|
};
|
|
47
64
|
/**
|
|
48
65
|
* API for the actions of the global states
|
|
49
66
|
**/
|
|
50
|
-
export type StoreTools<State, Metadata extends BaseMetadata = BaseMetadata, Actions
|
|
67
|
+
export type StoreTools<State, Metadata extends BaseMetadata | unknown = BaseMetadata, Actions extends undefined | unknown | Record<string, (...args: any[]) => any> = unknown> = {
|
|
51
68
|
setMetadata: MetadataSetter<Metadata>;
|
|
52
69
|
setState: StateSetter<State>;
|
|
53
70
|
getState: StateGetter<State>;
|
|
@@ -57,27 +74,23 @@ export type StoreTools<State, Metadata extends BaseMetadata = BaseMetadata, Acti
|
|
|
57
74
|
/**
|
|
58
75
|
* contract for the storeActionsConfig configuration
|
|
59
76
|
*/
|
|
60
|
-
export interface ActionCollectionConfig<State, Metadata extends BaseMetadata
|
|
61
|
-
readonly [key: string]:
|
|
77
|
+
export interface ActionCollectionConfig<State, Metadata extends BaseMetadata | unknown, ThisAPI = Record<string, (...parameters: any[]) => unknown>> {
|
|
78
|
+
readonly [key: string]: {
|
|
79
|
+
(this: ThisAPI, ...parameters: any[]): (this: ThisAPI, storeTools: StoreTools<State, Metadata, Record<string, (...parameters: any[]) => unknown | void>>) => unknown | void;
|
|
80
|
+
};
|
|
62
81
|
}
|
|
63
|
-
export type ActionCollectionResult<State, Metadata extends BaseMetadata, ActionsConfig extends ActionCollectionConfig<State, Metadata>> = {
|
|
64
|
-
[key in keyof ActionsConfig]:
|
|
82
|
+
export type ActionCollectionResult<State, Metadata extends BaseMetadata | unknown, ActionsConfig extends ActionCollectionConfig<State, Metadata>> = {
|
|
83
|
+
[key in keyof ActionsConfig]: {
|
|
84
|
+
(...params: Parameters<ActionsConfig[key]>): ReturnType<ReturnType<ActionsConfig[key]>>;
|
|
85
|
+
};
|
|
65
86
|
};
|
|
66
|
-
export type
|
|
67
|
-
/**
|
|
68
|
-
* non reactive information that you want to store in the store
|
|
69
|
-
* */
|
|
70
|
-
metadata?: Metadata;
|
|
87
|
+
export type GlobalStoreCallbacks<State, Metadata extends BaseMetadata | unknown> = {
|
|
71
88
|
onInit?: (args: StoreTools<State, Metadata>) => void;
|
|
72
89
|
onStateChanged?: (args: StoreTools<State, Metadata> & StateChanges<State>) => void;
|
|
73
90
|
onSubscribed?: (args: StoreTools<State, Metadata>) => void;
|
|
74
91
|
computePreventStateChange?: (args: StoreTools<State, Metadata> & StateChanges<State>) => boolean;
|
|
75
92
|
};
|
|
76
|
-
export type UseHookConfig<State, TRoot =
|
|
77
|
-
/**
|
|
78
|
-
* The callback to execute when the state is changed to check if the same really changed
|
|
79
|
-
* If the function is not provided the derived state will perform a shallow comparison
|
|
80
|
-
*/
|
|
93
|
+
export type UseHookConfig<State, TRoot = unknown> = {
|
|
81
94
|
isEqual?: (current: State, next: State) => boolean;
|
|
82
95
|
isEqualRoot?: (current: TRoot, next: TRoot) => boolean;
|
|
83
96
|
dependencies?: unknown[];
|
|
@@ -94,63 +107,29 @@ export type SubscribeCallbackConfig<State> = UseHookConfig<State> & {
|
|
|
94
107
|
*/
|
|
95
108
|
export type SubscribeCallback<State> = (state: State) => void;
|
|
96
109
|
/**
|
|
97
|
-
*
|
|
98
|
-
*/
|
|
99
|
-
export type SubscriberCallback<State> = (subscribe: SubscribeToEmitter<State>) => void;
|
|
100
|
-
/**
|
|
101
|
-
* Callback function to get the current state of the store or to subscribe to the store changes
|
|
102
|
-
* @template State - the type of the state
|
|
103
|
-
* @param {SubscriberCallback<State> | null} callback - the callback function to subscribe to the store changes (optional)
|
|
104
|
-
* use the methods subscribe and subscribeSelect to subscribe to the store changes
|
|
105
|
-
* if you don't pass a callback function the hook will return the current state of the store
|
|
106
|
-
* @returns {UnsubscribeCallback | State} result - the state or the unsubscribe callback if you pass a callback function
|
|
107
|
-
*/
|
|
108
|
-
export type StateGetter<State> = <Subscription extends Subscribe | false = false>(
|
|
109
|
-
/**
|
|
110
|
-
* @param {SubscriberCallback<State> | null} callback - the callback function to subscribe to the store changes (optional)
|
|
111
|
-
* use the methods subscribe and subscribeSelect to subscribe to the store changes
|
|
112
|
-
*/
|
|
113
|
-
callback?: Subscription extends Subscribe ? SubscriberCallback<State> : null) => Subscription extends Subscribe ? UnsubscribeCallback : State;
|
|
114
|
-
export type BaseMetadata = {
|
|
115
|
-
name?: string;
|
|
116
|
-
} & Record<string, any>;
|
|
117
|
-
export type MetadataGetter<Metadata extends BaseMetadata> = () => Metadata;
|
|
118
|
-
/**
|
|
119
|
-
* Constant value type to indicate that the getter is a subscription
|
|
110
|
+
* get the current state or subscribe to the state changes
|
|
120
111
|
*/
|
|
121
|
-
export type
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
112
|
+
export type StateGetter<State> = {
|
|
113
|
+
(): State;
|
|
114
|
+
(subscription: SubscribeCallback<State>, config?: SubscribeCallbackConfig<State>): UnsubscribeCallback;
|
|
115
|
+
<TDerivate>(selector: SelectorCallback<State, TDerivate>, subscription: SubscribeCallback<TDerivate>, config?: SubscribeCallbackConfig<TDerivate>): UnsubscribeCallback;
|
|
116
|
+
};
|
|
117
|
+
export type BaseMetadata = Record<string, unknown>;
|
|
118
|
+
export type MetadataGetter<Metadata extends BaseMetadata | unknown> = () => Metadata;
|
|
119
|
+
export type CustomGlobalHookBuilderParams<TCustomConfig extends BaseMetadata | unknown, Metadata extends BaseMetadata | unknown> = {
|
|
120
|
+
onInitialize?: (args: StoreTools<unknown, Metadata, unknown>, config: TCustomConfig | undefined) => void;
|
|
121
|
+
onChange?: (args: StoreTools<unknown, Metadata, unknown> & StateChanges<unknown>, config: TCustomConfig | undefined) => void;
|
|
125
122
|
};
|
|
126
123
|
export type SelectorCallback<State, TDerivate> = (state: State) => TDerivate;
|
|
127
|
-
/**
|
|
128
|
-
* @description
|
|
129
|
-
* Function to subscribe to the store changes
|
|
130
|
-
* @returns {UnsubscribeCallback} result - Function to unsubscribe from the store
|
|
131
|
-
*/
|
|
132
|
-
export type SubscribeToEmitter<State> = <TParam1 extends SubscribeCallback<State> | SelectorCallback<State, unknown>, TResult = ReturnType<TParam1>, TConfig = TResult extends void | null | undefined | never ? SubscribeCallbackConfig<State> : SubscribeCallbackConfig<TResult>, TParam2 extends SubscribeCallbackConfig<State> | SubscribeCallback<TResult> = TResult extends void | null | undefined | never ? TConfig : SubscribeCallback<TResult>, TParam3 = TResult extends void | null | undefined | never ? never : TConfig>(
|
|
133
|
-
/**
|
|
134
|
-
* @description
|
|
135
|
-
* The callback function to subscribe to the store changes or a selector function to derive the state
|
|
136
|
-
*/
|
|
137
|
-
param1: TParam1,
|
|
138
|
-
/**
|
|
139
|
-
* @description
|
|
140
|
-
* The configuration object or the callback function to subscribe to the store changes
|
|
141
|
-
*/
|
|
142
|
-
param2?: TParam2,
|
|
143
|
-
/**
|
|
144
|
-
* @description
|
|
145
|
-
* The configuration object
|
|
146
|
-
*/
|
|
147
|
-
param3?: TParam3) => UnsubscribeCallback;
|
|
148
124
|
export type SubscriberParameters = {
|
|
149
125
|
subscriptionId: string;
|
|
150
|
-
selector: SelectorCallback<
|
|
151
|
-
config: UseHookConfig<
|
|
126
|
+
selector: SelectorCallback<unknown, unknown> | undefined;
|
|
127
|
+
config: UseHookConfig<unknown> | SubscribeCallbackConfig<unknown> | undefined;
|
|
152
128
|
currentState: unknown;
|
|
153
|
-
callback: SubscriptionCallback
|
|
129
|
+
callback: SubscriptionCallback | React.Dispatch<React.SetStateAction<{
|
|
130
|
+
state: unknown;
|
|
131
|
+
}>>;
|
|
132
|
+
isSetStateCallback: boolean;
|
|
154
133
|
};
|
|
155
134
|
/**
|
|
156
135
|
* @description
|
|
@@ -159,10 +138,8 @@ export type SubscriberParameters = {
|
|
|
159
138
|
* @param {unknown} params.state - The new state
|
|
160
139
|
* @param {string} params.identifier - Optional identifier for the setState call
|
|
161
140
|
*/
|
|
162
|
-
export type SubscriptionCallback = (params: {
|
|
163
|
-
state:
|
|
141
|
+
export type SubscriptionCallback<State = unknown> = (params: {
|
|
142
|
+
state: State;
|
|
143
|
+
}, args: {
|
|
164
144
|
identifier?: string;
|
|
165
145
|
}) => void;
|
|
166
|
-
export type SetStateCallback = (parameters: {
|
|
167
|
-
state: unknown;
|
|
168
|
-
}) => void;
|
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
* Shallow compare two values and return true if they are equal.
|
|
3
|
-
* This function just compare the first level of the values.
|
|
4
|
-
* @param value1
|
|
5
|
-
* @param value2
|
|
6
|
-
* @returns {boolean} true if the values are equal, false otherwise
|
|
7
|
-
*/
|
|
1
|
+
/// <reference types="react" />
|
|
8
2
|
export declare const shallowCompare: <T>(value1: T, value2: T) => boolean;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export declare const
|
|
13
|
-
export declare const
|
|
3
|
+
export declare const debounce: <T extends (...args: Parameters<T>) => void>(callback: T, delay?: number) => (...args: Parameters<T>) => void;
|
|
4
|
+
export declare const uniqueId: (prefix?: string) => string;
|
|
5
|
+
export declare const throwWrongKeyOnActionCollectionConfig: (action_key: string) => never;
|
|
6
|
+
export declare const isRecord: (value: unknown) => value is Record<string, unknown>;
|
|
7
|
+
export declare const uniqueSymbol: unique symbol;
|
|
8
|
+
export type UniqueSymbol = typeof uniqueSymbol;
|
|
9
|
+
export declare const useConstantValueRef: <T>(initializer: () => T) => import("react").RefObject<T>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ActionCollectionConfig,
|
|
1
|
+
import { ActionCollectionConfig, StoreTools, BaseMetadata, StateChanges } from './GlobalStore.types';
|
|
2
2
|
import { GlobalStore } from './GlobalStore';
|
|
3
3
|
/**
|
|
4
4
|
* @description
|
|
@@ -6,8 +6,7 @@ import { GlobalStore } from './GlobalStore';
|
|
|
6
6
|
* by implementing the abstract methods onInitialize and onChange.
|
|
7
7
|
* You can use this class to create a store with async storage.
|
|
8
8
|
*/
|
|
9
|
-
export declare abstract class GlobalStoreAbstract<State, Metadata extends BaseMetadata, ActionsConfig extends ActionCollectionConfig<State, Metadata> |
|
|
10
|
-
constructor(state: State, config: GlobalStoreConfig<State, Metadata>, actionsConfig: ActionsConfig);
|
|
9
|
+
export declare abstract class GlobalStoreAbstract<State, Metadata extends BaseMetadata | unknown, ActionsConfig extends ActionCollectionConfig<State, Metadata> | unknown> extends GlobalStore<State, Metadata, ActionsConfig> {
|
|
11
10
|
protected onInit: (args: StoreTools<State, Metadata>) => void;
|
|
12
11
|
protected onStateChanged: (args: StoreTools<State, Metadata> & StateChanges<State>) => void;
|
|
13
12
|
protected abstract onInitialize: (args: StoreTools<State, Metadata>) => void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-hooks-global-states",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.1",
|
|
4
4
|
"description": "This is a package to easily handling global-state across your react-components No-redux",
|
|
5
5
|
"main": "lib/bundle.js",
|
|
6
6
|
"types": "lib/src/index.d.ts",
|
|
@@ -53,8 +53,8 @@
|
|
|
53
53
|
"@typescript-eslint/eslint-plugin": "^4.9.1",
|
|
54
54
|
"@typescript-eslint/parser": "^4.9.1",
|
|
55
55
|
"babel-loader": "^9.1.2",
|
|
56
|
-
"cancelable-promise-jq": "^1.0.4",
|
|
57
56
|
"clean-webpack-plugin": "^4.0.0",
|
|
57
|
+
"easy-cancelable-promise": "^1.0.1",
|
|
58
58
|
"eslint": "^7.15.0",
|
|
59
59
|
"eslint-config-airbnb": "^18.2.1",
|
|
60
60
|
"eslint-plugin-import": "^2.22.1",
|
|
@@ -80,6 +80,6 @@
|
|
|
80
80
|
}
|
|
81
81
|
},
|
|
82
82
|
"dependencies": {
|
|
83
|
-
"json-storage-formatter": "^1.
|
|
83
|
+
"json-storage-formatter": "^1.1.2"
|
|
84
84
|
}
|
|
85
85
|
}
|