1
0
mirror of https://github.com/upscayl/upscayl.git synced 2025-01-27 00:13:45 +01:00
upscayl/electron/utils/local-storage.ts

37 lines
1017 B
TypeScript
Raw Normal View History

2023-11-02 20:33:21 +05:30
import { getMainWindow } from "../main-window";
/**
* @description LocalStorage wrapper for the main window
* @example
* import { settings } from "./utils/settings";
*
* // Get a value
* const value = settings.get("key", true);
*
* // Set a value
* settings.set("key", value);
*/
2023-11-10 17:11:35 +05:30
export const localStorage = {
get: <T>(key: string, parse: boolean = false): T | null => {
2023-11-02 20:33:21 +05:30
const mainWindow = getMainWindow();
2023-11-04 15:32:44 +05:30
if (!mainWindow) return null;
2023-11-02 20:33:21 +05:30
let result = null;
mainWindow.webContents
.executeJavaScript(`localStorage.getItem("${key}");`, true)
.then((localStorageValue: any) => {
if (localStorageValue) {
result = parse ? JSON.parse(localStorageValue) : localStorageValue;
}
});
return result;
},
set: (key: string, value: any) => {
const mainWindow = getMainWindow();
if (!mainWindow) return;
mainWindow.webContents.executeJavaScript(
`localStorage.setItem("${key}", ${JSON.stringify(value)});`,
true
);
},
};