partey_workadventure/desktop/electron/src/settings.ts

40 lines
945 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-19 01:08:33 +01:00
import type { Server } from "./preload-local-app/types";
2022-02-18 01:51:35 +01:00
2022-02-22 10:05:21 +01:00
export type SettingsData = {
2022-02-18 23:03:05 +01:00
log_level: ElectronLog.LogLevel;
auto_launch_enabled: boolean;
servers: Server[];
2022-02-22 10:05:21 +01:00
shortcuts: Record<"mute_toggle" | "camera_toggle", string | null>;
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-19 01:48:56 +01:00
function get<T extends keyof SettingsData>(key: T): SettingsData[T] | undefined {
if (settings === undefined) {
2022-02-18 23:03:05 +01:00
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]) {
2022-02-19 01:48:56 +01:00
if (settings === undefined) {
2022-02-18 23:03:05 +01:00
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
};