mirror of
https://github.com/zulip/zulip-desktop.git
synced 2025-11-21 23:18:14 +00:00
linux-update-util: Strongly type update items.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
@@ -3,6 +3,7 @@ import fs from "fs";
|
|||||||
import path from "path";
|
import path from "path";
|
||||||
|
|
||||||
import {JsonDB} from "node-json-db";
|
import {JsonDB} from "node-json-db";
|
||||||
|
import {DataError} from "node-json-db/dist/lib/Errors";
|
||||||
|
|
||||||
import Logger from "../common/logger-util";
|
import Logger from "../common/logger-util";
|
||||||
|
|
||||||
@@ -14,10 +15,19 @@ let db: JsonDB;
|
|||||||
|
|
||||||
reloadDB();
|
reloadDB();
|
||||||
|
|
||||||
export function getUpdateItem(key: string, defaultValue: unknown = null): any {
|
export function getUpdateItem(
|
||||||
|
key: string,
|
||||||
|
defaultValue: true | null = null,
|
||||||
|
): true | null {
|
||||||
reloadDB();
|
reloadDB();
|
||||||
const value = db.getData("/")[key];
|
let value: unknown;
|
||||||
if (value === undefined) {
|
try {
|
||||||
|
value = db.getObject<unknown>(`/${key}`);
|
||||||
|
} catch (error: unknown) {
|
||||||
|
if (!(error instanceof DataError)) throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value !== true && value !== null) {
|
||||||
setUpdateItem(key, defaultValue);
|
setUpdateItem(key, defaultValue);
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
@@ -25,7 +35,7 @@ export function getUpdateItem(key: string, defaultValue: unknown = null): any {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setUpdateItem(key: string, value: unknown): void {
|
export function setUpdateItem(key: string, value: true | null): void {
|
||||||
db.push(`/${key}`, value, true);
|
db.push(`/${key}`, value, true);
|
||||||
reloadDB();
|
reloadDB();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user