partey_workadventure/desktop/src/settings.ts

50 lines
856 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 01:51:35 +01:00
type Server = {
_id: string;
name: string;
url: string;
};
2022-02-17 18:09:57 +01:00
type SettingsData = {
log_level: ElectronLog.LogLevel;
auto_launch_enabled: boolean;
2022-02-18 01:51:35 +01:00
servers: Server[];
2022-02-17 18:09:57 +01:00
};
let settings: SettingsData;
async function init() {
settings = (await Settings.get()) as SettingsData;
}
function get<T extends keyof SettingsData>(
key: T,
fallback?: SettingsData[T]
): SettingsData[T] {
if (settings === null) {
throw new Error("Settings not initialized");
}
return settings[key];
}
export function set<T extends keyof SettingsData>(
key: T,
value: SettingsData[T]
) {
if (settings === null) {
throw new Error("Settings not initialized");
}
settings[key] = value;
void Settings.set(settings);
}
export default {
init,
get,
set,
};