Skip to content

Instantly share code, notes, and snippets.

@superRaytin
Created July 12, 2019 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superRaytin/f6c3e58c95782aaca487e922d37f752f to your computer and use it in GitHub Desktop.
Save superRaytin/f6c3e58c95782aaca487e922d37f752f to your computer and use it in GitHub Desktop.
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