Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
IPC helper for the renderer process of Electron
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
You can’t perform that action at this time.