Notes/src/services/options.ts

109 lines
2.5 KiB
TypeScript
Raw Normal View History

import becca from "../becca/becca.js";
import BOption from "../becca/entities/boption.js";
import { OptionRow } from '../becca/entities/rows.js';
import sql from "./sql.js";
2021-05-01 21:52:22 +02:00
/**
* A dictionary where the keys are the option keys (e.g. `theme`) and their corresponding values.
*/
export type OptionMap = Record<string | number, string>;
2024-02-17 01:24:37 +02:00
function getOptionOrNull(name: string): string | null {
2022-01-02 21:20:56 +01:00
let option;
if (becca.loaded) {
option = becca.getOption(name);
2022-01-12 19:32:23 +01:00
} else {
2022-01-02 21:20:56 +01:00
// e.g. in initial sync becca is not loaded because DB is not initialized
2024-02-17 01:24:37 +02:00
option = sql.getRow<OptionRow>("SELECT * FROM options WHERE name = ?", [name]);
2022-01-02 21:20:56 +01:00
}
2023-05-04 22:16:18 +02:00
2022-01-12 19:32:23 +01:00
return option ? option.value : null;
}
2024-04-03 23:28:26 +03:00
function getOption(name: string) {
2022-01-12 19:32:23 +01:00
const val = getOptionOrNull(name);
2017-11-02 20:48:02 -04:00
2022-01-12 19:32:23 +01:00
if (val === null) {
throw new Error(`Option '${name}' doesn't exist`);
2017-11-02 20:48:02 -04:00
}
2022-01-12 19:32:23 +01:00
return val;
2017-11-02 20:48:02 -04:00
}
2024-02-17 01:24:37 +02:00
function getOptionInt(name: string, defaultValue?: number): number {
2020-06-20 12:31:38 +02:00
const val = getOption(name);
const intVal = parseInt(val);
if (isNaN(intVal)) {
if (defaultValue === undefined) {
throw new Error(`Could not parse '${val}' into integer for option '${name}'`);
} else {
return defaultValue;
}
}
return intVal;
}
2024-02-17 01:24:37 +02:00
function getOptionBool(name: string): boolean {
2020-06-20 12:31:38 +02:00
const val = getOption(name);
2024-04-03 23:28:26 +03:00
if (typeof val !== "string" || !['true', 'false'].includes(val)) {
2023-05-04 22:16:18 +02:00
throw new Error(`Could not parse '${val}' into boolean for option '${name}'`);
}
return val === 'true';
}
2024-04-03 23:18:39 +03:00
function setOption(name: string, value: string | number | boolean) {
2024-04-03 23:28:26 +03:00
if (value === true || value === false || typeof value === "number") {
2020-07-24 23:14:31 +02:00
value = value.toString();
}
2022-01-02 21:20:56 +01:00
const option = becca.getOption(name);
if (option) {
option.value = value;
2020-06-20 12:31:38 +02:00
option.save();
}
else {
2020-06-20 12:31:38 +02:00
createOption(name, value, false);
}
}
function createOption(name: string, value: string, isSynced: boolean) {
new BOption({
2018-01-28 19:30:14 -05:00
name: name,
value: value,
isSynced: isSynced
}).save();
2017-11-02 20:48:02 -04:00
}
2020-06-20 12:31:38 +02:00
function getOptions() {
2021-05-01 21:52:22 +02:00
return Object.values(becca.options);
2018-09-06 11:54:04 +02:00
}
2023-06-29 22:10:13 +02:00
function getOptionMap() {
const map: OptionMap = {};
2021-05-01 21:52:22 +02:00
for (const option of Object.values(becca.options)) {
map[option.name] = option.value;
}
return map;
2018-09-06 11:54:04 +02:00
}
export default {
2017-11-02 20:48:02 -04:00
getOption,
getOptionInt,
getOptionBool,
2017-11-02 20:48:02 -04:00
setOption,
2018-09-06 11:54:04 +02:00
createOption,
getOptions,
2023-06-29 22:10:13 +02:00
getOptionMap,
2022-01-12 19:32:23 +01:00
getOptionOrNull
2020-06-20 12:31:38 +02:00
};