2023-06-21 10:38:39 +00:00
|
|
|
import { ConfigMap } from 'fyo/core/types';
|
2023-07-10 09:12:20 +00:00
|
|
|
import type { IPC } from 'main/preload';
|
2022-04-18 11:29:20 +00:00
|
|
|
|
|
|
|
export class Config {
|
2023-07-10 09:12:20 +00:00
|
|
|
config: Map<string, unknown> | IPC['store'];
|
2022-04-18 11:29:20 +00:00
|
|
|
constructor(isElectron: boolean) {
|
2023-06-14 08:40:03 +00:00
|
|
|
this.config = new Map();
|
|
|
|
if (isElectron) {
|
2023-07-10 09:12:20 +00:00
|
|
|
this.config = ipc.store;
|
2022-04-18 11:29:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 10:38:39 +00:00
|
|
|
get<K extends keyof ConfigMap>(
|
|
|
|
key: K,
|
|
|
|
defaultValue?: ConfigMap[K]
|
|
|
|
): ConfigMap[K] | undefined {
|
|
|
|
const value = this.config.get(key) as ConfigMap[K] | undefined;
|
|
|
|
return value ?? defaultValue;
|
2022-04-18 11:29:20 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 10:38:39 +00:00
|
|
|
set<K extends keyof ConfigMap>(key: K, value: ConfigMap[K]) {
|
2023-06-14 08:40:03 +00:00
|
|
|
this.config.set(key, value);
|
2022-04-18 11:29:20 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 10:38:39 +00:00
|
|
|
delete(key: keyof ConfigMap) {
|
2023-06-14 08:40:03 +00:00
|
|
|
this.config.delete(key);
|
2022-04-18 11:29:20 +00:00
|
|
|
}
|
|
|
|
}
|