partey_workadventure/desktop/src/settings.ts

39 lines
863 B
TypeScript
Raw Normal View History

2022-02-17 18:09:57 +01:00
import ElectronLog from "electron-log";
import Settings from "electron-settings";
2022-02-18 10:26:21 +01:00
import type { Server } from "./sidebar/types";
2022-02-18 01:51:35 +01:00
2022-02-17 18:09:57 +01:00
type SettingsData = {
2022-02-18 23:03:05 +01:00
log_level: ElectronLog.LogLevel;
auto_launch_enabled: boolean;
servers: Server[];
2022-02-17 18:09:57 +01:00
};
let settings: SettingsData;
async function init() {
2022-02-18 23:03:05 +01:00
settings = (await Settings.get()) as SettingsData;
2022-02-17 18:09:57 +01:00
}
2022-02-18 23:03:05 +01:00
function get<T extends keyof SettingsData>(key: T, fallback?: SettingsData[T]): SettingsData[T] {
if (settings === null) {
throw new Error("Settings not initialized");
}
2022-02-17 18:09:57 +01:00
2022-02-18 23:03:05 +01:00
return settings?.[key];
2022-02-17 18:09:57 +01:00
}
2022-02-18 23:03:05 +01:00
export function set<T extends keyof SettingsData>(key: T, value: SettingsData[T]) {
if (settings === null) {
throw new Error("Settings not initialized");
}
2022-02-17 18:09:57 +01:00
2022-02-18 23:03:05 +01:00
settings[key] = value;
void Settings.set(settings);
2022-02-17 18:09:57 +01:00
}
export default {
2022-02-18 23:03:05 +01:00
init,
get,
set,
2022-02-17 18:09:57 +01:00
};