2019-02-15 01:24:25 +00:00
|
|
|
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'
|
2019-02-14 07:05:49 +00:00
|
|
|
|
2019-03-13 19:03:17 +00:00
|
|
|
type IPCHandler = (event: Electron.IpcRendererEvent, ...args: any[]) => any
|
|
|
|
|
|
|
|
export const handle = function <T extends IPCHandler> (channel: string, handler: T) {
|
2019-08-23 22:45:50 +00:00
|
|
|
ipcRendererInternal.on(channel, async (event, requestId, ...args) => {
|
|
|
|
const replyChannel = `${channel}_RESPONSE_${requestId}`
|
|
|
|
try {
|
|
|
|
event.sender.send(replyChannel, null, await handler(event, ...args))
|
|
|
|
} catch (error) {
|
2019-10-10 13:59:08 +00:00
|
|
|
event.sender.send(replyChannel, error)
|
2019-08-23 22:45:50 +00:00
|
|
|
}
|
2019-02-14 07:05:49 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function invokeSync<T> (command: string, ...args: any[]): T {
|
2019-08-23 22:45:50 +00:00
|
|
|
const [ error, result ] = ipcRendererInternal.sendSync(command, ...args)
|
2019-02-14 07:05:49 +00:00
|
|
|
|
|
|
|
if (error) {
|
2019-10-10 13:59:08 +00:00
|
|
|
throw error
|
2019-02-14 07:05:49 +00:00
|
|
|
} else {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|