|
1 | 1 | import { ipcRenderer, contextBridge } from 'electron' |
2 | 2 |
|
3 | 3 | // --------- Expose some API to the Renderer process --------- |
4 | | -contextBridge.exposeInMainWorld('app', { |
5 | | - onEvent(cb) { |
6 | | - const channel = 'main-process-message' |
7 | | - const channel2 = 'other-ipc-channel' |
8 | | - |
9 | | - ipcRenderer.on(channel, (_e, ...args) => cb(channel, ...args)) |
10 | | - ipcRenderer.on(channel2, (_e, ...args) => cb(channel2, ...args)) |
| 4 | +contextBridge.exposeInMainWorld('ipcRenderer', { |
| 5 | + on(...args: Parameters<typeof ipcRenderer.on>) { |
| 6 | + const [channel, listener] = args |
| 7 | + ipcRenderer.on(channel, (event, ...args) => listener(event, ...args)) |
| 8 | + }, |
| 9 | + off(...args: Parameters<typeof ipcRenderer.off>) { |
| 10 | + const [channel, ...omit] = args |
| 11 | + ipcRenderer.off(channel, ...omit) |
| 12 | + }, |
| 13 | + send(...args: Parameters<typeof ipcRenderer.send>) { |
| 14 | + const [channel, ...omit] = args |
| 15 | + ipcRenderer.send(channel, ...omit) |
11 | 16 | }, |
| 17 | + invoke(...args: Parameters<typeof ipcRenderer.invoke>) { |
| 18 | + const [channel, ...omit] = args |
| 19 | + ipcRenderer.invoke(channel, ...omit) |
| 20 | + }, |
| 21 | + |
| 22 | + // You can expose other APTs you need here. |
| 23 | + // ... |
12 | 24 | }) |
13 | 25 |
|
14 | 26 | // --------- Preload scripts loading --------- |
|
0 commit comments