mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 22:42:25 -04:00
55 lines
1.8 KiB
TypeScript
55 lines
1.8 KiB
TypeScript
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
import {
|
|
getConfigJson,
|
|
getMetaJson,
|
|
getProfileConfigJson,
|
|
setConfigJson,
|
|
setMetaJson,
|
|
setProfileConfigJson,
|
|
} from "@generated/backend";
|
|
|
|
async function getSettingJson(key: string, backendGetter: (key: string) => Promise<any>): Promise<any> {
|
|
const decoder = new TextDecoder();
|
|
const json = decoder.decode((await backendGetter(key)).json);
|
|
return JSON.parse(json);
|
|
}
|
|
|
|
async function setSettingJson(
|
|
key: string,
|
|
value: any,
|
|
backendSetter: (key: string, value: any) => Promise<any>,
|
|
): Promise<void> {
|
|
const encoder = new TextEncoder();
|
|
const json = JSON.stringify(value);
|
|
await backendSetter(key, encoder.encode(json));
|
|
}
|
|
|
|
export async function getProfileConfig(key: string): Promise<any> {
|
|
return getSettingJson(key, async (k) => await getProfileConfigJson({ val: k }));
|
|
}
|
|
|
|
export async function setProfileConfig(key: string, value: any): Promise<void> {
|
|
return await setSettingJson(key, value, async (k, v) => await setProfileConfigJson({ key: k, valueJson: v }));
|
|
}
|
|
|
|
export async function getMeta(key: string): Promise<any> {
|
|
return getSettingJson(key, async (k) => await getMetaJson({ val: k }));
|
|
}
|
|
|
|
export async function setMeta(key: string, value: any): Promise<void> {
|
|
return await setSettingJson(key, value, async (k, v) => await setMetaJson({ key: k, valueJson: v }));
|
|
}
|
|
|
|
export async function getColConfig(key: string): Promise<any> {
|
|
return await getSettingJson(key, async (k) => await getConfigJson({ val: k }));
|
|
}
|
|
|
|
export async function setColConfig(key: string, value: any): Promise<void> {
|
|
return await setSettingJson(
|
|
key,
|
|
value,
|
|
async (k, v) => await setConfigJson({ key: k, valueJson: v, undoable: true }),
|
|
);
|
|
}
|