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 = Promise.resolve(); const readFile = async (): Promise> => { try { const content = await fs.readFile(filePath, "utf-8"); const parsed = parseJsonWithByteOrderMark(content); return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? (parsed as Record) : {}; } catch { return {}; } }; return { async read(): Promise> { await operationQueue; return readFile(); }, async update(patch: RecordingPreferencesPatch): Promise { 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; }, }; }