From a0a2f44807ab4e240f0c9616e01bba4d4733db13 Mon Sep 17 00:00:00 2001 From: Johannes Millan Date: Sat, 16 Dec 2023 12:53:07 +0100 Subject: [PATCH] refactor(electronSecurity): make typing work in preload for ipc events --- electron/ipc-handler.ts | 2 +- electron/preload.ts | 26 ++++++++++++------- .../shared-with-frontend/ipc-events.const.ts | 4 ++- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/electron/ipc-handler.ts b/electron/ipc-handler.ts index 485b0a7d2d..c288a9c1a6 100644 --- a/electron/ipc-handler.ts +++ b/electron/ipc-handler.ts @@ -35,7 +35,7 @@ if (process.platform === 'darwin') { // --------- ipcMain.on(IPC.SHUTDOWN_NOW, quitApp); ipcMain.on(IPC.EXIT, (ev, exitCode: number) => app.exit(exitCode)); -ipcMain.on(IPC.RELAUCNH, () => app.relaunch()); +ipcMain.on(IPC.RELAUNCH, () => app.relaunch()); ipcMain.on(IPC.OPEN_DEV_TOOLS, () => getWin().webContents.openDevTools()); ipcMain.on(IPC.RELOAD_MAIN_WIN, () => getWin().reload()); diff --git a/electron/preload.ts b/electron/preload.ts index d43ce4141c..738f08e8c8 100644 --- a/electron/preload.ts +++ b/electron/preload.ts @@ -8,6 +8,14 @@ import { webFrame, } from 'electron'; import { ElectronAPI } from './electronAPI.d'; +import { IPCEventValue } from './shared-with-frontend/ipc-events.const'; + +const send: (channel: IPCEventValue, ...args: any[]) => void = (channel, ...args) => + ipcRenderer.send(channel, ...args); +const invoke: (channel: IPCEventValue, ...args: any[]) => Promise = ( + channel, + ...args +) => ipcRenderer.invoke(channel, ...args); const electronAPI: Partial = { // TODO use full interface @@ -36,19 +44,17 @@ const electronAPI: Partial = { isSystemDarkMode: () => nativeTheme.shouldUseDarkColors, - getUserDataPath: () => ipcRenderer.invoke('GET_PATH', 'userData'), - relaunch: () => ipcRenderer.send('RELAUNCH'), - exit: () => ipcRenderer.send('EXIT'), - reloadMainWin: () => ipcRenderer.send('RELOAD_MAIN_WIN'), - openDevTools: () => ipcRenderer.send('OPEN_DEV_TOOLS'), + getUserDataPath: () => invoke('GET_PATH', 'userData') as Promise, + relaunch: () => send('RELAUNCH'), + exit: () => send('EXIT'), + reloadMainWin: () => send('RELOAD_MAIN_WIN'), + openDevTools: () => send('OPEN_DEV_TOOLS'), // ALL EVENTS - scheduleRegisterBeforeClose: (id) => ipcRenderer.send('REGISTER_BEFORE_CLOSE', { id }), - unscheduleRegisterBeforeClose: (id) => - ipcRenderer.send('UNREGISTER_BEFORE_CLOSE', { id }), - setDoneRegisterBeforeClose: (id) => ipcRenderer.send('BEFORE_CLOSE_DONE', { id }), + scheduleRegisterBeforeClose: (id) => send('REGISTER_BEFORE_CLOSE', { id }), + unscheduleRegisterBeforeClose: (id) => send('UNREGISTER_BEFORE_CLOSE', { id }), + setDoneRegisterBeforeClose: (id) => send('BEFORE_CLOSE_DONE', { id }), }; - contextBridge.exposeInMainWorld('electronAPI', electronAPI); // contextBridge.exposeInIsolatedWorld(); diff --git a/electron/shared-with-frontend/ipc-events.const.ts b/electron/shared-with-frontend/ipc-events.const.ts index 793b294542..3471f1140b 100644 --- a/electron/shared-with-frontend/ipc-events.const.ts +++ b/electron/shared-with-frontend/ipc-events.const.ts @@ -2,7 +2,7 @@ export enum IPC { SHUTDOWN_NOW = 'SHUTDOWN_NOW', EXIT = 'EXIT', - RELAUCNH = 'RELAUCNH', + RELAUNCH = 'RELAUNCH', JIRA_CB_EVENT = 'JIRA_RESPONSE', JIRA_MAKE_REQUEST_EVENT = 'JIRA', @@ -68,3 +68,5 @@ export enum IPC { // maybe_PROJECT_CHANGED = 'PROJECT_CHANGED', // maybe_COMPLETE_DATA_RELOAD = 'COMPLETE_DATA_RELOAD', } + +export type IPCEventValue = `${IPC}`;