-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseConfigStore.ts
39 lines (33 loc) · 1.02 KB
/
useConfigStore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { create } from 'zustand';
import { get, merge, noop } from 'lodash';
export interface ConfigState {
config: SdkConfig;
setConfig: (config: SdkConfig) => void;
};
const useConfigStore = create<ConfigState>((set, get) => ({
config: {
onMessage: noop,
logger: noop
},
setConfig: config => {
const newConfig = merge({}, get().config, config);
set({
config: newConfig
});
},
onMessage: noop
}));
export function getConfig(): SdkConfig;
export function getConfig<K extends keyof SdkConfig>(key: K): SdkConfig[K];
export function getConfig<K extends keyof SdkConfig>(key?: K): SdkConfig | SdkConfig[K];
export function getConfig<K extends keyof SdkConfig>(key?: K): SdkConfig | SdkConfig[K] {
const config = useConfigStore.getState().config;
if (key) {
return get(config, key);
}
return config;
};
export const setConfig = (config: SdkConfig) => {
return useConfigStore.getState().setConfig(config);
};
export default useConfigStore;