Created
July 12, 2019 02:58
-
-
Save superRaytin/f6c3e58c95782aaca487e922d37f752f to your computer and use it in GitHub Desktop.
IPC helper for the renderer process of Electron
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ipcRenderer as ipc } from 'electron' | |
let ipcCount = 0 | |
// 向主进程发送消息,并等待主进程的回复 | |
export function send(channel, ...args) { | |
const localCount = ipcCount += 1 | |
return new Promise((resolve) => { | |
ipc.once(`${channel}-${ipcCount}`, (event, ...res) => { | |
resolve({ event, payload: res.length === 1 ? res[0] : res }) | |
}) | |
ipc.send(channel, localCount, ...args) | |
}) | |
} | |
// 向主进程发送消息,不需要返回 | |
export function sendWithoutReturn(channel, ...args) { | |
ipc.send(channel, ...args) | |
} | |
export function on(channel, callback) { | |
ipc.on(channel, callback) | |
} | |
export function once(channel, callback) { | |
ipc.on(channel, callback) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment