From 78deb0ebad40e4746796994345143120e3566b6b Mon Sep 17 00:00:00 2001 From: Vendicated Date: Thu, 1 Sep 2022 21:40:26 +0200 Subject: [PATCH] Fix settings corrupting --- src/ipcMain.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/ipcMain.ts b/src/ipcMain.ts index 38a16ab..80bff55 100644 --- a/src/ipcMain.ts +++ b/src/ipcMain.ts @@ -23,12 +23,18 @@ function readSettings() { ipcMain.handle(IpcEvents.GET_SETTINGS_DIR, () => SETTINGS_DIR); ipcMain.handle(IpcEvents.GET_QUICK_CSS, () => readCss()); -// .on because we need Settings synchronously (ipcRenderer.sendSync) -ipcMain.on(IpcEvents.GET_SETTINGS, (e) => e.returnValue = readSettings()); -ipcMain.handle(IpcEvents.SET_SETTINGS, (_, s) => void writeFile(SETTINGS_FILE, s)); ipcMain.handle(IpcEvents.OPEN_PATH, (_, path) => shell.openPath(path)); ipcMain.handle(IpcEvents.OPEN_EXTERNAL, (_, url) => shell.openExternal(url)); +// .on because we need Settings synchronously (ipcRenderer.sendSync) +ipcMain.on(IpcEvents.GET_SETTINGS, (e) => e.returnValue = readSettings()); + +// This is required because otherwise calling SET_SETTINGS in quick succession may lead to concurrent writes +let settingsWriteQueue = Promise.resolve(); +ipcMain.handle(IpcEvents.SET_SETTINGS, (_, s) => { + settingsWriteQueue = settingsWriteQueue.then(() => writeFile(SETTINGS_FILE, s)); +}); + export function initIpc(mainWindow: BrowserWindow) { open(QUICKCSS_PATH, "a+").then(fd => { fd.close();