From 834aa5c90d4ebdc306f46d7b08f9730ca117143d Mon Sep 17 00:00:00 2001 From: Robin Huang Date: Thu, 7 Nov 2024 16:20:02 -0800 Subject: [PATCH] Refactor: Renderer asks main for default install location. (#206) --- src/main.ts | 7 ++----- src/preload.ts | 7 ++----- src/renderer/index.tsx | 2 +- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/main.ts b/src/main.ts index 9a1681e5..45f3a625 100644 --- a/src/main.ts +++ b/src/main.ts @@ -390,11 +390,8 @@ export const createWindow = async (): Promise => { }); log.info('Loading renderer into main window'); - mainWindow.webContents.on('did-finish-load', () => { - if (mainWindow) { - mainWindow.webContents.send(IPC_CHANNELS.DEFAULT_INSTALL_LOCATION, app.getPath('documents')); - } - }); + + ipcMain.handle(IPC_CHANNELS.DEFAULT_INSTALL_LOCATION, () => app.getPath('documents')); await loadRendererIntoMainWindow(); log.info('Renderer loaded into main window'); diff --git a/src/preload.ts b/src/preload.ts index f0d3d824..b900f0c2 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -56,11 +56,8 @@ const electronAPI = { onFirstTimeSetupComplete: (callback: () => void) => { ipcRenderer.on(IPC_CHANNELS.FIRST_TIME_SETUP_COMPLETE, () => callback()); }, - onDefaultInstallLocation: (callback: (location: string) => void) => { - ipcRenderer.on(IPC_CHANNELS.DEFAULT_INSTALL_LOCATION, (_event, value) => { - console.log(`Received ${IPC_CHANNELS.DEFAULT_INSTALL_LOCATION} event`, value); - callback(value); - }); + getDefaultInstallLocation: (): Promise => { + return ipcRenderer.invoke(IPC_CHANNELS.DEFAULT_INSTALL_LOCATION); }, /** * Various paths that are useful to the renderer. diff --git a/src/renderer/index.tsx b/src/renderer/index.tsx index 4bbd49e8..d91acc7c 100644 --- a/src/renderer/index.tsx +++ b/src/renderer/index.tsx @@ -62,7 +62,7 @@ const Home: React.FC = () => { }, [updateProgress, addLogMessage]); useEffect(() => { - electronAPI.onDefaultInstallLocation((location: string) => { + electronAPI.getDefaultInstallLocation().then((location: string) => { setDefaultInstallLocation(location); }); }, []);