diff --git a/electron/preload/index.ts b/electron/preload/index.ts index 3316868a..396b771a 100644 --- a/electron/preload/index.ts +++ b/electron/preload/index.ts @@ -1,14 +1,26 @@ import { ipcRenderer, contextBridge } from 'electron' // --------- Expose some API to the Renderer process --------- -contextBridge.exposeInMainWorld('app', { - onEvent(cb) { - const channel = 'main-process-message' - const channel2 = 'other-ipc-channel' - - ipcRenderer.on(channel, (_e, ...args) => cb(channel, ...args)) - ipcRenderer.on(channel2, (_e, ...args) => cb(channel2, ...args)) +contextBridge.exposeInMainWorld('ipcRenderer', { + on(...args: Parameters) { + const [channel, listener] = args + ipcRenderer.on(channel, (event, ...args) => listener(event, ...args)) + }, + off(...args: Parameters) { + const [channel, ...omit] = args + ipcRenderer.off(channel, ...omit) + }, + send(...args: Parameters) { + const [channel, ...omit] = args + ipcRenderer.send(channel, ...omit) }, + invoke(...args: Parameters) { + const [channel, ...omit] = args + ipcRenderer.invoke(channel, ...omit) + }, + + // You can expose other APTs you need here. + // ... }) // --------- Preload scripts loading --------- diff --git a/src/demos/ipc.ts b/src/demos/ipc.ts index c5697a1f..ba4daa0c 100644 --- a/src/demos/ipc.ts +++ b/src/demos/ipc.ts @@ -1,4 +1,4 @@ -window.app.onEvent((channel, ...args) => { - console.log(channel, ...args) +window.ipcRenderer.on('main-process-message', (_event, ...args) => { + console.log('[Receive Main-process message]:', ...args) }) diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index 5679df78..788532e7 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -7,8 +7,6 @@ declare module '*.vue' { } interface Window { - app: { - // Expose in the `electron/preload/index.ts` - onEvent: (cb: (channel: string, ...args: any[]) => void) => void - } + // expose in the `electron/preload/index.ts` + ipcRenderer: import('electron').IpcRenderer }