mirror of
https://github.com/webadderallorg/Recordly.git
synced 2026-09-24 14:55:37 +00:00
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import fs from "node:fs/promises";
|
|
import { parseJsonWithByteOrderMark } from "../utils";
|
|
|
|
export interface RecordingPreferencesPatch {
|
|
microphoneEnabled?: boolean;
|
|
microphoneDeviceId?: string;
|
|
systemAudioEnabled?: boolean;
|
|
webcamEnabled?: boolean;
|
|
webcamDeviceId?: string;
|
|
}
|
|
|
|
export function createRecordingPreferencesStore(filePath: string) {
|
|
let operationQueue: Promise<void> = Promise.resolve();
|
|
|
|
const readFile = async (): Promise<Record<string, unknown>> => {
|
|
try {
|
|
const content = await fs.readFile(filePath, "utf-8");
|
|
const parsed = parseJsonWithByteOrderMark<unknown>(content);
|
|
return parsed && typeof parsed === "object" && !Array.isArray(parsed)
|
|
? (parsed as Record<string, unknown>)
|
|
: {};
|
|
} catch {
|
|
return {};
|
|
}
|
|
};
|
|
|
|
return {
|
|
async read(): Promise<Record<string, unknown>> {
|
|
await operationQueue;
|
|
return readFile();
|
|
},
|
|
async update(patch: RecordingPreferencesPatch): Promise<void> {
|
|
const operation = operationQueue.then(async () => {
|
|
const existing = await readFile();
|
|
await fs.writeFile(
|
|
filePath,
|
|
JSON.stringify({ ...existing, ...patch }, null, 2),
|
|
"utf-8",
|
|
);
|
|
});
|
|
operationQueue = operation.catch(() => undefined);
|
|
await operation;
|
|
},
|
|
};
|
|
}
|