feat: better ipcRenderer api expose

This commit is contained in:
Leo Wang(草鞋没号)
2024-03-11 16:06:01 +08:00
parent a9952f16c8
commit 11e9c4bffa
3 changed files with 23 additions and 13 deletions

View File

@@ -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<typeof ipcRenderer.on>) {
const [channel, listener] = args
ipcRenderer.on(channel, (event, ...args) => listener(event, ...args))
},
off(...args: Parameters<typeof ipcRenderer.off>) {
const [channel, ...omit] = args
ipcRenderer.off(channel, ...omit)
},
send(...args: Parameters<typeof ipcRenderer.send>) {
const [channel, ...omit] = args
ipcRenderer.send(channel, ...omit)
},
invoke(...args: Parameters<typeof ipcRenderer.invoke>) {
const [channel, ...omit] = args
ipcRenderer.invoke(channel, ...omit)
},
// You can expose other APTs you need here.
// ...
})
// --------- Preload scripts loading ---------