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,187 +1,50 @@
|
|
|
1
|
-
import { ActionCollectionConfig, StateHook, StateSetter, ActionCollectionResult, StateGetter,
|
|
1
|
+
import { ActionCollectionConfig, StateHook, StateSetter, ActionCollectionResult, StateGetter, BaseMetadata, MetadataSetter, GlobalStoreCallbacks } from './GlobalStore.types';
|
|
2
2
|
import React, { PropsWithChildren } from 'react';
|
|
3
|
-
export type
|
|
3
|
+
export type ContextProviderAPI<Value, Metadata extends BaseMetadata | unknown> = {
|
|
4
4
|
setMetadata: MetadataSetter<Metadata>;
|
|
5
5
|
setState: StateSetter<Value>;
|
|
6
6
|
getState: StateGetter<Value>;
|
|
7
7
|
getMetadata: () => Metadata;
|
|
8
8
|
actions: Record<string, (...args: any[]) => void>;
|
|
9
9
|
};
|
|
10
|
-
type
|
|
10
|
+
export type ContextProvider<Value, Metadata extends BaseMetadata | unknown> = React.FC<PropsWithChildren<{
|
|
11
11
|
value?: Value | ((initialValue: Value) => Value);
|
|
12
|
-
ref?: React.
|
|
12
|
+
ref?: React.RefObject<ContextProviderAPI<Value, Metadata>>;
|
|
13
13
|
}>>;
|
|
14
|
-
type
|
|
15
|
-
|
|
16
|
-
* Allows you to create a selector hooks
|
|
17
|
-
* This hooks only works when contained in the scope of the provider
|
|
18
|
-
*/
|
|
19
|
-
createSelectorHook: <RootState, RootSelectorResult, RootDerivate = RootSelectorResult extends never ? RootState : RootSelectorResult>(this: Context<RootState, PublicStateMutator, Metadata>, mainSelector?: (state: Value) => RootSelectorResult, args?: {
|
|
14
|
+
export type ContextHook<Value, PublicStateMutator, Metadata extends BaseMetadata | unknown> = (() => StateHook<Value, PublicStateMutator, Metadata>) & {
|
|
15
|
+
createSelectorHook: <RootState, RootSelectorResult, RootDerivate = RootSelectorResult extends never ? RootState : RootSelectorResult>(this: ContextHook<RootState, PublicStateMutator, Metadata>, mainSelector?: (state: Value) => RootSelectorResult, args?: {
|
|
20
16
|
isEqual?: (current: RootDerivate, next: RootDerivate) => boolean;
|
|
21
17
|
isEqualRoot?: (current: RootState, next: RootState) => boolean;
|
|
22
18
|
name?: string;
|
|
23
19
|
}) => StateHook<RootDerivate, PublicStateMutator, Metadata>;
|
|
24
20
|
};
|
|
25
21
|
export interface CreateContext {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
];
|
|
30
|
-
createContext<Value, Metadata extends BaseMetadata, ActionsConfig extends ActionCollectionConfig<Value, Metadata> | {} | null = null, StoreAPI = {
|
|
31
|
-
setMetadata: MetadataSetter<Metadata>;
|
|
32
|
-
setState: StateSetter<Value>;
|
|
33
|
-
getState: StateGetter<Value>;
|
|
34
|
-
getMetadata: () => Metadata;
|
|
35
|
-
actions: ActionsConfig extends null ? null : Record<string, (...args: any[]) => void>;
|
|
36
|
-
}>(state: Value, config: Readonly<{
|
|
37
|
-
/**
|
|
38
|
-
* @deprecated We needed to move the actions parameter as a third argument to fix several issues with the type inference of the actions
|
|
39
|
-
*/
|
|
40
|
-
actions?: ActionsConfig;
|
|
41
|
-
/**
|
|
42
|
-
* Non reactive information about the state
|
|
43
|
-
*/
|
|
44
|
-
metadata?: Metadata;
|
|
45
|
-
/**
|
|
46
|
-
* executes immediately after the store is created
|
|
47
|
-
* */
|
|
48
|
-
onInit?: (args: StoreAPI) => void;
|
|
49
|
-
onStateChanged?: (args: StoreAPI & StateChanges<Value>) => void;
|
|
50
|
-
onSubscribed?: (args: StoreAPI) => void;
|
|
51
|
-
/**
|
|
52
|
-
* callback function called every time the state is about to change and it allows you to prevent the state change
|
|
53
|
-
*/
|
|
54
|
-
computePreventStateChange?: (args: StoreAPI & StateChanges<Value>) => boolean;
|
|
55
|
-
onUnMount?: () => void;
|
|
56
|
-
}>): readonly [
|
|
57
|
-
Context<Value, ActionsConfig extends null ? StateSetter<Value> : ActionCollectionResult<Value, Metadata, ActionsConfig>, Metadata>,
|
|
58
|
-
Provider<Context<Value, ActionsConfig extends null ? StateSetter<Value> : ActionCollectionResult<Value, Metadata, ActionsConfig>, Metadata>, Metadata>
|
|
59
|
-
];
|
|
60
|
-
createContext<Value, Metadata extends BaseMetadata, ActionsConfig extends ActionCollectionConfig<Value, Metadata>, StoreAPI = {
|
|
61
|
-
setMetadata: MetadataSetter<Metadata>;
|
|
62
|
-
setState: StateSetter<Value>;
|
|
63
|
-
getState: StateGetter<Value>;
|
|
64
|
-
getMetadata: () => Metadata;
|
|
65
|
-
actions: Record<string, (...args: any[]) => void>;
|
|
66
|
-
}>(state: Value, config: Readonly<{
|
|
67
|
-
/**
|
|
68
|
-
* Non reactive information about the state
|
|
69
|
-
*/
|
|
70
|
-
metadata?: Metadata;
|
|
71
|
-
/**
|
|
72
|
-
* executes immediately after the store is created
|
|
73
|
-
* */
|
|
74
|
-
onInit?: (args: StoreAPI) => void;
|
|
75
|
-
onStateChanged?: (args: StoreAPI & StateChanges<Value>) => void;
|
|
76
|
-
onSubscribed?: (args: StoreAPI) => void;
|
|
77
|
-
/**
|
|
78
|
-
* callback function called every time the state is about to change and it allows you to prevent the state change
|
|
79
|
-
*/
|
|
80
|
-
computePreventStateChange?: (args: StoreAPI & StateChanges<Value>) => boolean;
|
|
81
|
-
onUnMount?: () => void;
|
|
82
|
-
}>, actions: ActionsConfig): readonly [
|
|
83
|
-
Context<Value, ActionCollectionResult<Value, Metadata, ActionsConfig>, Metadata>,
|
|
84
|
-
Provider<Context<Value, ActionCollectionResult<Value, Metadata, ActionsConfig>, Metadata>>
|
|
85
|
-
];
|
|
86
|
-
createContext<Value, Metadata extends BaseMetadata, ActionsConfig extends ActionCollectionConfig<Value, Metadata>, StoreAPI = {
|
|
87
|
-
setMetadata: MetadataSetter<Metadata>;
|
|
88
|
-
setState: StateSetter<Value>;
|
|
89
|
-
getState: StateGetter<Value>;
|
|
90
|
-
getMetadata: () => Metadata;
|
|
91
|
-
}>(state: Value, builder: () => ActionsConfig, config?: Readonly<{
|
|
92
|
-
/**
|
|
93
|
-
* Non reactive information about the state
|
|
94
|
-
*/
|
|
95
|
-
metadata?: Metadata;
|
|
96
|
-
/**
|
|
97
|
-
* executes immediately after the store is created
|
|
98
|
-
* */
|
|
99
|
-
onInit?: (args: StoreAPI) => void;
|
|
100
|
-
onStateChanged?: (args: StoreAPI & StateChanges<Value>) => void;
|
|
101
|
-
onSubscribed?: (args: StoreAPI) => void;
|
|
102
|
-
/**
|
|
103
|
-
* callback function called every time the state is about to change and it allows you to prevent the state change
|
|
104
|
-
*/
|
|
105
|
-
computePreventStateChange?: (args: StoreAPI & StateChanges<Value>) => boolean;
|
|
106
|
-
onUnMount?: () => void;
|
|
107
|
-
}>): readonly [
|
|
108
|
-
Context<Value, ActionCollectionResult<Value, Metadata, ActionsConfig>, Metadata>,
|
|
109
|
-
Provider<Context<Value, ActionCollectionResult<Value, Metadata, ActionsConfig>, Metadata>>
|
|
22
|
+
<Value, Hook = ContextHook<Value, StateSetter<Value>, BaseMetadata>>(builder: () => Value): readonly [
|
|
23
|
+
Hook,
|
|
24
|
+
ContextProvider<Hook, BaseMetadata>
|
|
110
25
|
];
|
|
26
|
+
<Value, Metadata extends BaseMetadata | unknown, Hook = ContextHook<Value, StateSetter<Value>, Metadata>>(builder: () => Value, args: {
|
|
27
|
+
name?: string;
|
|
28
|
+
metadata?: unknown;
|
|
29
|
+
callbacks?: GlobalStoreCallbacks<Value, Metadata> & {
|
|
30
|
+
onUnMount?: () => void;
|
|
31
|
+
};
|
|
32
|
+
}): readonly [Hook, ContextProvider<Hook, Metadata>];
|
|
33
|
+
<Value, Metadata extends BaseMetadata | unknown, ActionsConfig extends ActionCollectionConfig<Value, Metadata>, PublicStateMutator = keyof ActionsConfig extends never | undefined ? StateSetter<Value> : ActionCollectionResult<Value, Metadata, NonNullable<ActionsConfig>>, Hook = ContextHook<Value, PublicStateMutator, Metadata>>(builder: () => Value, args: {
|
|
34
|
+
name?: string;
|
|
35
|
+
metadata?: unknown;
|
|
36
|
+
callbacks?: GlobalStoreCallbacks<Value, Metadata> & {
|
|
37
|
+
onUnMount?: () => void;
|
|
38
|
+
};
|
|
39
|
+
actions?: ActionCollectionConfig<Value, Metadata>;
|
|
40
|
+
}): readonly [Hook, ContextProvider<Hook, Metadata>];
|
|
41
|
+
<Value, Metadata extends BaseMetadata | unknown, ActionsConfig extends ActionCollectionConfig<Value, Metadata>, Hook = ContextHook<Value, ActionCollectionResult<Value, Metadata, ActionsConfig>, Metadata>>(builder: () => Value, args: {
|
|
42
|
+
name?: string;
|
|
43
|
+
metadata?: unknown;
|
|
44
|
+
callbacks?: GlobalStoreCallbacks<Value, Metadata> & {
|
|
45
|
+
onUnMount?: () => void;
|
|
46
|
+
};
|
|
47
|
+
actions: ActionCollectionConfig<Value, Metadata>;
|
|
48
|
+
}): readonly [Hook, ContextProvider<Hook, Metadata>];
|
|
111
49
|
}
|
|
112
|
-
export declare const createContext:
|
|
113
|
-
<Value>(state: Value): readonly [Context<Value, StateSetter<Value>, BaseMetadata>, Provider<Context<Value, StateSetter<Value>, BaseMetadata>, BaseMetadata>];
|
|
114
|
-
<Value_1, Metadata extends BaseMetadata, ActionsConfig extends {} | ActionCollectionConfig<Value_1, Metadata> = null, StoreAPI = {
|
|
115
|
-
setMetadata: MetadataSetter<Metadata>;
|
|
116
|
-
setState: StateSetter<Value_1>;
|
|
117
|
-
getState: StateGetter<Value_1>;
|
|
118
|
-
getMetadata: () => Metadata;
|
|
119
|
-
actions: ActionsConfig extends null ? null : Record<string, (...args: any[]) => void>;
|
|
120
|
-
}>(state: Value_1, config: Readonly<{
|
|
121
|
-
/**
|
|
122
|
-
* @deprecated We needed to move the actions parameter as a third argument to fix several issues with the type inference of the actions
|
|
123
|
-
*/
|
|
124
|
-
actions?: ActionsConfig;
|
|
125
|
-
/**
|
|
126
|
-
* Non reactive information about the state
|
|
127
|
-
*/
|
|
128
|
-
metadata?: Metadata;
|
|
129
|
-
/**
|
|
130
|
-
* executes immediately after the store is created
|
|
131
|
-
* */
|
|
132
|
-
onInit?: (args: StoreAPI) => void;
|
|
133
|
-
onStateChanged?: (args: StoreAPI & StateChanges<Value_1>) => void;
|
|
134
|
-
onSubscribed?: (args: StoreAPI) => void;
|
|
135
|
-
/**
|
|
136
|
-
* callback function called every time the state is about to change and it allows you to prevent the state change
|
|
137
|
-
*/
|
|
138
|
-
computePreventStateChange?: (args: StoreAPI & StateChanges<Value_1>) => boolean;
|
|
139
|
-
onUnMount?: () => void;
|
|
140
|
-
}>): readonly [Context<Value_1, ActionsConfig extends null ? StateSetter<Value_1> : ActionCollectionResult<Value_1, Metadata, ActionsConfig>, Metadata>, Provider<Context<Value_1, ActionsConfig extends null ? StateSetter<Value_1> : ActionCollectionResult<Value_1, Metadata, ActionsConfig>, Metadata>, Metadata>];
|
|
141
|
-
<Value_2, Metadata_1 extends BaseMetadata, ActionsConfig_1 extends ActionCollectionConfig<Value_2, Metadata_1>, StoreAPI_1 = {
|
|
142
|
-
setMetadata: MetadataSetter<Metadata_1>;
|
|
143
|
-
setState: StateSetter<Value_2>;
|
|
144
|
-
getState: StateGetter<Value_2>;
|
|
145
|
-
getMetadata: () => Metadata_1;
|
|
146
|
-
actions: Record<string, (...args: any[]) => void>;
|
|
147
|
-
}>(state: Value_2, config: Readonly<{
|
|
148
|
-
/**
|
|
149
|
-
* Non reactive information about the state
|
|
150
|
-
*/
|
|
151
|
-
metadata?: Metadata_1;
|
|
152
|
-
/**
|
|
153
|
-
* executes immediately after the store is created
|
|
154
|
-
* */
|
|
155
|
-
onInit?: (args: StoreAPI_1) => void;
|
|
156
|
-
onStateChanged?: (args: StoreAPI_1 & StateChanges<Value_2>) => void;
|
|
157
|
-
onSubscribed?: (args: StoreAPI_1) => void;
|
|
158
|
-
/**
|
|
159
|
-
* callback function called every time the state is about to change and it allows you to prevent the state change
|
|
160
|
-
*/
|
|
161
|
-
computePreventStateChange?: (args: StoreAPI_1 & StateChanges<Value_2>) => boolean;
|
|
162
|
-
onUnMount?: () => void;
|
|
163
|
-
}>, actions: ActionsConfig_1): readonly [Context<Value_2, ActionCollectionResult<Value_2, Metadata_1, ActionsConfig_1>, Metadata_1>, Provider<Context<Value_2, ActionCollectionResult<Value_2, Metadata_1, ActionsConfig_1>, Metadata_1>, BaseMetadata>];
|
|
164
|
-
<Value_3, Metadata_2 extends BaseMetadata, ActionsConfig_2 extends ActionCollectionConfig<Value_3, Metadata_2>, StoreAPI_2 = {
|
|
165
|
-
setMetadata: MetadataSetter<Metadata_2>;
|
|
166
|
-
setState: StateSetter<Value_3>;
|
|
167
|
-
getState: StateGetter<Value_3>;
|
|
168
|
-
getMetadata: () => Metadata_2;
|
|
169
|
-
}>(state: Value_3, builder: () => ActionsConfig_2, config?: Readonly<{
|
|
170
|
-
/**
|
|
171
|
-
* Non reactive information about the state
|
|
172
|
-
*/
|
|
173
|
-
metadata?: Metadata_2;
|
|
174
|
-
/**
|
|
175
|
-
* executes immediately after the store is created
|
|
176
|
-
* */
|
|
177
|
-
onInit?: (args: StoreAPI_2) => void;
|
|
178
|
-
onStateChanged?: (args: StoreAPI_2 & StateChanges<Value_3>) => void;
|
|
179
|
-
onSubscribed?: (args: StoreAPI_2) => void;
|
|
180
|
-
/**
|
|
181
|
-
* callback function called every time the state is about to change and it allows you to prevent the state change
|
|
182
|
-
*/
|
|
183
|
-
computePreventStateChange?: (args: StoreAPI_2 & StateChanges<Value_3>) => boolean;
|
|
184
|
-
onUnMount?: () => void;
|
|
185
|
-
}>): readonly [Context<Value_3, ActionCollectionResult<Value_3, Metadata_2, ActionsConfig_2>, Metadata_2>, Provider<Context<Value_3, ActionCollectionResult<Value_3, Metadata_2, ActionsConfig_2>, Metadata_2>, BaseMetadata>];
|
|
186
|
-
};
|
|
187
|
-
export {};
|
|
50
|
+
export declare const createContext: CreateContext;
|
package/lib/src/GlobalStore.d.ts
CHANGED
|
@@ -1,64 +1,47 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { UniqueSymbol } from './GlobalStore.utils';
|
|
2
|
+
import { ActionCollectionConfig, StateSetter, GlobalStoreCallbacks, ActionCollectionResult, MetadataSetter, UseHookConfig, StateGetter, SelectorCallback, SubscriberParameters, MetadataGetter, StateHook, BaseMetadata, StateChanges, StoreTools, ObservableFragment } from './GlobalStore.types';
|
|
3
3
|
/**
|
|
4
4
|
* The GlobalStore class is the main class of the library and it is used to create a GlobalStore instances
|
|
5
5
|
* */
|
|
6
|
-
export declare class GlobalStore<State, Metadata extends BaseMetadata, ActionsConfig extends ActionCollectionConfig<State, Metadata> |
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
actions:
|
|
12
|
-
}, PublicStateMutator = ActionsConfig extends null ? StateSetter<State> : ActionCollectionResult<State, Metadata, ActionsConfig>> {
|
|
13
|
-
protected actionsConfig: ActionsConfig;
|
|
14
|
-
/**
|
|
15
|
-
* list of all the subscribers setState functions
|
|
16
|
-
* @template {State} TState - The type of the state object
|
|
17
|
-
* */
|
|
6
|
+
export declare class GlobalStore<State, Metadata extends BaseMetadata | unknown, ActionsConfig extends ActionCollectionConfig<State, Metadata> | undefined | unknown, PublicStateMutator = keyof ActionsConfig extends never | undefined ? StateSetter<State> : ActionCollectionResult<State, Metadata, NonNullable<ActionsConfig>>> {
|
|
7
|
+
protected _name: string;
|
|
8
|
+
actionsConfig: ActionsConfig | null;
|
|
9
|
+
callbacks: GlobalStoreCallbacks<State, Metadata> | null;
|
|
10
|
+
metadata: Metadata;
|
|
11
|
+
actions: ActionCollectionResult<State, Metadata, NonNullable<ActionsConfig>> | null;
|
|
18
12
|
subscribers: Map<string, SubscriberParameters>;
|
|
19
|
-
|
|
20
|
-
* Actions of the store
|
|
21
|
-
*/
|
|
22
|
-
actions?: ActionsConfig extends null ? null : ActionCollectionResult<State, Metadata, ActionsConfig>;
|
|
23
|
-
protected config: GlobalStoreConfig<State, Metadata>;
|
|
24
|
-
/**
|
|
25
|
-
* execute when the store is initialized
|
|
26
|
-
*/
|
|
27
|
-
protected onInit?: (args: StoreAPI) => void;
|
|
28
|
-
protected onStateChanged?: (args: StoreAPI & StateChanges<State>) => void;
|
|
29
|
-
protected onSubscribed?: (args: StoreAPI) => void;
|
|
30
|
-
/**
|
|
31
|
-
* Every time a state change is triggered and before the state is updated, it allows to prevent the state change by returning true
|
|
32
|
-
*/
|
|
33
|
-
protected computePreventStateChange?: (parameters: StoreAPI & StateChanges<State>) => boolean;
|
|
34
|
-
/**
|
|
35
|
-
* We use a wrapper in order to be able to force the state update when necessary even with primitive types
|
|
36
|
-
*/
|
|
37
|
-
protected stateWrapper: {
|
|
13
|
+
stateWrapper: {
|
|
38
14
|
state: State;
|
|
39
15
|
};
|
|
40
|
-
/**
|
|
41
|
-
* @deprecated direct modifications of the state could end up in unexpected behaviors
|
|
42
|
-
*/
|
|
43
|
-
protected get state(): State;
|
|
44
16
|
constructor(state: State);
|
|
45
|
-
constructor(state: State,
|
|
46
|
-
|
|
17
|
+
constructor(state: State, args: {
|
|
18
|
+
metadata?: Metadata;
|
|
19
|
+
callbacks?: GlobalStoreCallbacks<State, Metadata>;
|
|
20
|
+
actions?: ActionsConfig;
|
|
21
|
+
name?: string;
|
|
22
|
+
});
|
|
23
|
+
protected onInit?: (args: StoreTools<State, Metadata>) => void;
|
|
24
|
+
protected onStateChanged?: (args: StoreTools<State, Metadata> & StateChanges<State>) => void;
|
|
25
|
+
protected onSubscribed?: (args: StoreTools<State, Metadata>) => void;
|
|
26
|
+
protected computePreventStateChange?: (parameters: StoreTools<State, Metadata> & StateChanges<State>) => boolean;
|
|
47
27
|
protected initialize: () => Promise<void>;
|
|
48
|
-
protected executeSetStateForSubscriber: (subscription: SubscriberParameters,
|
|
49
|
-
forceUpdate: boolean;
|
|
28
|
+
protected executeSetStateForSubscriber: (subscription: SubscriberParameters, args: {
|
|
29
|
+
forceUpdate: boolean | undefined;
|
|
50
30
|
newRootState: State;
|
|
51
|
-
currentRootState: State;
|
|
52
|
-
identifier: string;
|
|
53
|
-
}) =>
|
|
31
|
+
currentRootState: State | UniqueSymbol;
|
|
32
|
+
identifier: string | undefined;
|
|
33
|
+
}) => {
|
|
34
|
+
didUpdate: boolean;
|
|
35
|
+
};
|
|
54
36
|
/**
|
|
55
37
|
* set the state and update all the subscribers
|
|
56
38
|
* @param {StateSetter<State>} setter - The setter function or the value to set
|
|
57
39
|
* */
|
|
58
|
-
protected setState: ({ state: newRootState,
|
|
40
|
+
protected setState: ({ state: newRootState, }: {
|
|
59
41
|
state: State;
|
|
60
|
-
|
|
61
|
-
|
|
42
|
+
}, { forceUpdate, identifier }: {
|
|
43
|
+
forceUpdate?: boolean | undefined;
|
|
44
|
+
identifier?: string | undefined;
|
|
62
45
|
}) => void;
|
|
63
46
|
/**
|
|
64
47
|
* Set the value of the metadata property, this is no reactive and will not trigger a re-render
|
|
@@ -66,56 +49,50 @@ export declare class GlobalStore<State, Metadata extends BaseMetadata, ActionsCo
|
|
|
66
49
|
* */
|
|
67
50
|
protected setMetadata: MetadataSetter<Metadata>;
|
|
68
51
|
protected getMetadata: () => Metadata;
|
|
69
|
-
|
|
70
|
-
selector?: SelectorCallback<unknown, unknown>;
|
|
71
|
-
callback: SubscribeCallback<unknown>;
|
|
72
|
-
config: SubscribeCallbackConfig<unknown>;
|
|
73
|
-
}) => {
|
|
74
|
-
stateWrapper: {
|
|
75
|
-
state: unknown;
|
|
76
|
-
};
|
|
77
|
-
subscriptionCallback: SubscriptionCallback;
|
|
78
|
-
};
|
|
79
|
-
protected getState: StateGetter<State>;
|
|
52
|
+
getState: StateGetter<State>;
|
|
80
53
|
/**
|
|
81
|
-
* get the parameters object to pass to the callback functions
|
|
54
|
+
* get the parameters object to pass to the callback functions:
|
|
55
|
+
* onInit, onStateChanged, onSubscribed, computePreventStateChange
|
|
82
56
|
* */
|
|
83
|
-
|
|
84
|
-
protected lastSubscriptionId: string;
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
protected
|
|
57
|
+
getConfigCallbackParam: () => StoreTools<State, Metadata>;
|
|
58
|
+
protected lastSubscriptionId: string | null;
|
|
59
|
+
protected setOrUpdateSubscription: (subscription: SubscriberParameters) => {
|
|
60
|
+
isNewSubscription?: boolean;
|
|
61
|
+
};
|
|
62
|
+
protected partialUpdateSubscription: (subscriptionId: string, values: Partial<SubscriberParameters>) => void;
|
|
89
63
|
protected executeOnSubscribed: () => void;
|
|
90
64
|
/**
|
|
91
65
|
* Returns a custom hook that allows to handle a global state
|
|
92
66
|
* @returns {[State, StateMutator, Metadata]} - The state, the state setter or the actions map, the metadata
|
|
93
67
|
* */
|
|
94
68
|
getHook: () => StateHook<State, PublicStateMutator, Metadata>;
|
|
69
|
+
protected computeSelectedState: ({ selector, subscriptionId, config, currentDependencies, computeChildState, stateWrapperRef, }: {
|
|
70
|
+
selector: SelectorCallback<unknown, unknown> | undefined;
|
|
71
|
+
subscriptionId: string;
|
|
72
|
+
config: UseHookConfig<unknown, unknown>;
|
|
73
|
+
currentDependencies: unknown[] | undefined;
|
|
74
|
+
computeChildState: () => {
|
|
75
|
+
state: unknown;
|
|
76
|
+
};
|
|
77
|
+
stateWrapperRef: {
|
|
78
|
+
state: unknown;
|
|
79
|
+
};
|
|
80
|
+
}) => unknown;
|
|
95
81
|
/**
|
|
96
82
|
* @description
|
|
97
83
|
* Use this function to create a custom global hook which contains a fragment of the state of another hook
|
|
98
84
|
*/
|
|
99
|
-
createSelectorHook: <RootState, StateMutator, Metadata_1 extends BaseMetadata, RootSelectorResult, RootDerivate = RootSelectorResult extends never ? RootState : RootSelectorResult>(mainSelector
|
|
100
|
-
isEqual?: (current: RootDerivate, next: RootDerivate) => boolean;
|
|
101
|
-
isEqualRoot?: (current: RootState, next: RootState) => boolean;
|
|
102
|
-
name?: string;
|
|
85
|
+
createSelectorHook: <RootState, StateMutator, Metadata_1 extends BaseMetadata, RootSelectorResult, RootDerivate = RootSelectorResult extends never ? RootState : RootSelectorResult>(mainSelector: (state: RootState) => RootSelectorResult, { isEqualRoot: mainIsEqualRoot, isEqual: mainIsEqualFun, name: selectorName, }?: {
|
|
86
|
+
isEqual?: ((current: RootDerivate, next: RootDerivate) => boolean) | undefined;
|
|
87
|
+
isEqualRoot?: ((current: RootState, next: RootState) => boolean) | undefined;
|
|
88
|
+
name?: string | undefined;
|
|
103
89
|
}) => StateHook<RootDerivate, StateMutator, Metadata_1>;
|
|
104
|
-
|
|
105
|
-
* 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
|
|
106
|
-
* @returns {[() => State, StateMutator, () => Metadata]} - The state getter, the state setter or the actions map, the metadata getter
|
|
107
|
-
* */
|
|
108
|
-
stateControls: () => [StateGetter<State>, PublicStateMutator, MetadataGetter<Metadata>];
|
|
90
|
+
stateControls: () => [retriever: StateGetter<State>, mutator: PublicStateMutator, metadata: MetadataGetter<Metadata>];
|
|
109
91
|
/**
|
|
110
92
|
* Returns the state setter or the actions map
|
|
111
93
|
* @returns {StateMutator} - The state setter or the actions map
|
|
112
94
|
* */
|
|
113
95
|
protected getStateOrchestrator: () => PublicStateMutator;
|
|
114
|
-
/**
|
|
115
|
-
* Calculate whenever or not we should compute the callback parameters on the state change
|
|
116
|
-
* @returns {boolean} - True if we should compute the callback parameters on the state change
|
|
117
|
-
* */
|
|
118
|
-
protected hasStateCallbacks: () => boolean;
|
|
119
96
|
/**
|
|
120
97
|
* This is responsible for defining whenever or not the state change should be allowed or prevented
|
|
121
98
|
* the function also execute the functions:
|
|
@@ -127,5 +104,10 @@ export declare class GlobalStore<State, Metadata extends BaseMetadata, ActionsCo
|
|
|
127
104
|
* This creates a map of actions that can be used to modify or interact with the state
|
|
128
105
|
* @returns {ActionCollectionResult<State, Metadata, StateMutator>} - The actions map result of the configuration object passed to the constructor
|
|
129
106
|
* */
|
|
130
|
-
|
|
107
|
+
getStoreActionsMap: () => null | ActionCollectionResult<State, Metadata, NonNullable<ActionsConfig>>;
|
|
108
|
+
createObservable<Fragment>(mainSelector: (state: State) => Fragment, { isEqualRoot: mainIsEqualRoot, isEqual: mainIsEqualFun, name: selectorName, }?: {
|
|
109
|
+
isEqual?: (current: Fragment, next: Fragment) => boolean;
|
|
110
|
+
isEqualRoot?: (current: State, next: State) => boolean;
|
|
111
|
+
name?: string;
|
|
112
|
+
}): ObservableFragment<Fragment>;
|
|
131
113
|
}
|
|
@@ -1,125 +1,38 @@
|
|
|
1
|
-
import { ActionCollectionConfig, StateSetter, ActionCollectionResult,
|
|
2
|
-
|
|
3
|
-
* Creates a global hook that can be used to access the state and actions across the application
|
|
4
|
-
* @returns {} - () => [TState, Setter, TMetadata] the hook that can be used to access the state and the setter of the state
|
|
5
|
-
*/
|
|
6
|
-
export declare const createGlobalState: {
|
|
1
|
+
import { ActionCollectionConfig, StateSetter, ActionCollectionResult, StateHook, CustomGlobalHookBuilderParams, BaseMetadata, GlobalStoreCallbacks } from './GlobalStore.types';
|
|
2
|
+
export interface CreateGlobalState {
|
|
7
3
|
<State>(state: State): StateHook<State, StateSetter<State>, BaseMetadata>;
|
|
8
|
-
<
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
getMetadata: () => Metadata;
|
|
13
|
-
actions: ActionsConfig extends null ? null : Record<string, (...args: any[]) => void>;
|
|
14
|
-
}>(state: State_1, config: Readonly<{
|
|
15
|
-
/**
|
|
16
|
-
* @deprecated We needed to move the actions parameter as a third argument to fix several issues with the type inference of the actions
|
|
17
|
-
*/
|
|
4
|
+
<State, Metadata extends BaseMetadata | unknown, ActionsConfig extends ActionCollectionConfig<State, Metadata> | null | {}, PublicStateMutator = keyof ActionsConfig extends never | undefined ? StateSetter<State> : ActionCollectionResult<State, Metadata, NonNullable<ActionsConfig>>>(state: State, args: {
|
|
5
|
+
name?: string;
|
|
6
|
+
metadata?: Metadata;
|
|
7
|
+
callbacks?: GlobalStoreCallbacks<State, Metadata>;
|
|
18
8
|
actions?: ActionsConfig;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
9
|
+
}): StateHook<State, PublicStateMutator, Metadata>;
|
|
10
|
+
<State, Metadata extends BaseMetadata | unknown, ActionsConfig extends ActionCollectionConfig<State, Metadata>>(state: State, args: {
|
|
11
|
+
name?: string;
|
|
22
12
|
metadata?: Metadata;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* executes immediately after the store is created
|
|
47
|
-
* */
|
|
48
|
-
onInit?: (args: StoreAPI_1) => void;
|
|
49
|
-
onStateChanged?: (args: StoreAPI_1 & StateChanges<State_2>) => void;
|
|
50
|
-
onSubscribed?: (args: StoreAPI_1) => void;
|
|
51
|
-
/**
|
|
52
|
-
* callback function called every time the state is about to change and it allows you to prevent the state change
|
|
53
|
-
*/
|
|
54
|
-
computePreventStateChange?: (args: StoreAPI_1 & StateChanges<State_2>) => boolean;
|
|
55
|
-
}>, actions: ActionsConfig_1): StateHook<State_2, ActionCollectionResult<State_2, Metadata_1, ActionsConfig_1>, Metadata_1>;
|
|
56
|
-
<State_3, Metadata_2 extends BaseMetadata, ActionsConfig_2 extends ActionCollectionConfig<State_3, Metadata_2>, StoreAPI_2 = {
|
|
57
|
-
setMetadata: MetadataSetter<Metadata_2>;
|
|
58
|
-
setState: StateSetter<State_3>;
|
|
59
|
-
getState: StateGetter<State_3>;
|
|
60
|
-
getMetadata: () => Metadata_2;
|
|
61
|
-
}>(state: State_3, builder: () => ActionsConfig_2, config?: Readonly<{
|
|
62
|
-
/**
|
|
63
|
-
* Non reactive information about the state
|
|
64
|
-
*/
|
|
65
|
-
metadata?: Metadata_2;
|
|
66
|
-
/**
|
|
67
|
-
* executes immediately after the store is created
|
|
68
|
-
* */
|
|
69
|
-
onInit?: (args: StoreAPI_2) => void;
|
|
70
|
-
onStateChanged?: (args: StoreAPI_2 & StateChanges<State_3>) => void;
|
|
71
|
-
onSubscribed?: (args: StoreAPI_2) => void;
|
|
72
|
-
/**
|
|
73
|
-
* callback function called every time the state is about to change and it allows you to prevent the state change
|
|
74
|
-
*/
|
|
75
|
-
computePreventStateChange?: (args: StoreAPI_2 & StateChanges<State_3>) => boolean;
|
|
76
|
-
}>): StateHook<State_3, ActionCollectionResult<State_3, Metadata_2, ActionsConfig_2>, Metadata_2>;
|
|
77
|
-
};
|
|
78
|
-
/**
|
|
79
|
-
* @description
|
|
80
|
-
* Use this function to create a custom global store.
|
|
81
|
-
* You can use this function to create a store with async storage.
|
|
82
|
-
*/
|
|
83
|
-
export declare const createCustomGlobalState: <InheritMetadata, TCustomConfig>({ onInitialize, onChange, }: CustomGlobalHookBuilderParams<InheritMetadata, TCustomConfig>) => <State, Metadata, ActionsConfig extends Readonly<ActionCollectionConfig<State, Metadata & InheritMetadata>> = null>(state: State, _config?: Readonly<{
|
|
84
|
-
metadata?: Metadata & InheritMetadata;
|
|
85
|
-
readonly actions?: Readonly<ActionsConfig>;
|
|
86
|
-
onInit?: (args: StoreTools<State, Metadata & InheritMetadata>) => void;
|
|
87
|
-
onStateChanged?: (args: StoreTools<State, Metadata & InheritMetadata> & StateChanges<State>) => void;
|
|
88
|
-
onSubscribed?: (parameters: StoreTools<State, Metadata & InheritMetadata>) => void;
|
|
89
|
-
computePreventStateChange?: (args: StoreTools<State, Metadata & InheritMetadata> & StateChanges<State>) => boolean;
|
|
90
|
-
/**
|
|
91
|
-
* @description
|
|
92
|
-
* Type of the configuration object that the custom hook will require or accept
|
|
93
|
-
*/
|
|
94
|
-
config?: TCustomConfig;
|
|
95
|
-
}>) => StateHook<State, ActionsConfig extends null ? StateSetter<State> : ActionCollectionResult<State, Metadata & InheritMetadata, ActionsConfig>, Metadata & InheritMetadata>;
|
|
96
|
-
/**
|
|
97
|
-
* @description
|
|
98
|
-
* Use this function to create a custom global store.
|
|
99
|
-
* You can use this function to create a store with async storage.
|
|
100
|
-
* @deprecated
|
|
101
|
-
*/
|
|
102
|
-
export declare const createCustomGlobalStateWithDecoupledFuncs: <InheritMetadata, TCustomConfig>({ onInitialize, onChange, }: CustomGlobalHookBuilderParams<InheritMetadata, TCustomConfig>) => <State, Metadata, ActionsConfig extends Readonly<ActionCollectionConfig<State, Metadata & InheritMetadata>> = null>(state: State, _config?: Readonly<{
|
|
103
|
-
metadata?: Metadata & InheritMetadata;
|
|
104
|
-
readonly actions?: Readonly<ActionsConfig>;
|
|
105
|
-
onInit?: (args: StoreTools<State, Metadata & InheritMetadata>) => void;
|
|
106
|
-
onStateChanged?: (args: StoreTools<State, Metadata & InheritMetadata> & StateChanges<State>) => void;
|
|
107
|
-
onSubscribed?: (parameters: StoreTools<State, Metadata & InheritMetadata>) => void;
|
|
108
|
-
computePreventStateChange?: (args: StoreTools<State, Metadata & InheritMetadata> & StateChanges<State>) => boolean;
|
|
109
|
-
/**
|
|
110
|
-
* @description
|
|
111
|
-
* Type of the configuration object that the custom hook will require or accept
|
|
112
|
-
*/
|
|
113
|
-
config?: TCustomConfig;
|
|
114
|
-
}>) => StateHook<State, ActionsConfig extends null ? StateSetter<State> : ActionCollectionResult<State, Metadata & InheritMetadata, ActionsConfig>, Metadata & InheritMetadata>;
|
|
115
|
-
/**
|
|
116
|
-
* @description
|
|
117
|
-
* Use this function to create a custom global hook which contains a fragment of the state of another hook
|
|
118
|
-
*/
|
|
119
|
-
export declare const createDerivate: <RootState, StateMutator, Metadata, RootSelectorResult, RootDerivate = RootSelectorResult extends never ? RootState : RootSelectorResult>(useHook: StateHook<RootState, StateMutator, Metadata>, mainSelector?: (state: RootState) => RootSelectorResult, args?: Omit<UseHookConfig<RootDerivate, RootState>, "dependencies">) => StateHook<RootDerivate, StateMutator, Metadata>;
|
|
13
|
+
callbacks?: GlobalStoreCallbacks<State, Metadata>;
|
|
14
|
+
actions: ActionsConfig;
|
|
15
|
+
}): StateHook<State, ActionCollectionResult<State, Metadata, ActionsConfig>, Metadata>;
|
|
16
|
+
}
|
|
17
|
+
export declare const createGlobalState: CreateGlobalState;
|
|
18
|
+
export interface CustomCreateGlobalState<TCustomConfig extends BaseMetadata | unknown, InheritMetadata extends BaseMetadata | unknown = BaseMetadata> {
|
|
19
|
+
<State>(state: State): StateHook<State, StateSetter<State>, BaseMetadata>;
|
|
20
|
+
<State, Metadata extends BaseMetadata | unknown, ActionsConfig extends ActionCollectionConfig<State, InheritMetadata & Metadata> | null | {}, PublicStateMutator = keyof ActionsConfig extends never | undefined ? StateSetter<State> : ActionCollectionResult<State, InheritMetadata & Metadata, NonNullable<ActionsConfig>>>(state: State, args: {
|
|
21
|
+
name?: string;
|
|
22
|
+
metadata?: Metadata;
|
|
23
|
+
callbacks?: GlobalStoreCallbacks<State, InheritMetadata & Metadata>;
|
|
24
|
+
actions?: ActionsConfig;
|
|
25
|
+
config?: TCustomConfig;
|
|
26
|
+
}): StateHook<State, PublicStateMutator, InheritMetadata & Metadata>;
|
|
27
|
+
<State, Metadata extends BaseMetadata | unknown, ActionsConfig extends Readonly<ActionCollectionConfig<State, InheritMetadata & Metadata>>>(state: State, args: {
|
|
28
|
+
name?: string;
|
|
29
|
+
metadata?: Metadata;
|
|
30
|
+
callbacks?: GlobalStoreCallbacks<State, InheritMetadata & Metadata>;
|
|
31
|
+
actions: ActionsConfig;
|
|
32
|
+
config?: TCustomConfig;
|
|
33
|
+
}): StateHook<State, ActionCollectionResult<State, InheritMetadata & Metadata, ActionsConfig>, InheritMetadata & Metadata>;
|
|
34
|
+
}
|
|
120
35
|
/**
|
|
121
|
-
* @description
|
|
122
|
-
* This function allows you to create a derivate emitter
|
|
123
|
-
* With this approach, you can subscribe to changes in a specific fragment or subset of the state.
|
|
36
|
+
* @description Simple custom global state hook builder
|
|
124
37
|
*/
|
|
125
|
-
export declare const
|
|
38
|
+
export declare const createCustomGlobalState: <TCustomConfig extends unknown, InheritMetadata extends unknown = BaseMetadata>({ onInitialize, onChange, }: CustomGlobalHookBuilderParams<TCustomConfig, InheritMetadata>) => CustomCreateGlobalState<TCustomConfig, InheritMetadata>;
|