2019-02-14 22:29:20 +00:00
|
|
|
import { ipcMainInternal } from '@electron/internal/browser/ipc-main-internal'
|
|
|
|
import * as errorUtils from '@electron/internal/common/error-utils'
|
2019-02-01 18:56:46 +00:00
|
|
|
|
2019-02-14 22:29:20 +00:00
|
|
|
type IPCHandler = (...args: any[]) => any
|
2019-02-01 18:56:46 +00:00
|
|
|
|
2019-02-19 09:24:19 +00:00
|
|
|
const callHandler = async function (handler: IPCHandler, event: ElectronInternal.IpcMainInternalEvent, args: any[], reply: (args: any[]) => void) {
|
2019-02-01 18:56:46 +00:00
|
|
|
try {
|
|
|
|
const result = await handler(event, ...args)
|
|
|
|
reply([null, result])
|
|
|
|
} catch (error) {
|
|
|
|
reply([errorUtils.serialize(error)])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:29:20 +00:00
|
|
|
export const handle = function <T extends IPCHandler> (channel: string, handler: T) {
|
2019-02-04 22:49:53 +00:00
|
|
|
ipcMainInternal.on(channel, (event, requestId, ...args) => {
|
2019-02-01 18:56:46 +00:00
|
|
|
callHandler(handler, event, args, responseArgs => {
|
2019-02-26 23:48:26 +00:00
|
|
|
if (requestId) {
|
|
|
|
event._replyInternal(`${channel}_RESPONSE_${requestId}`, ...responseArgs)
|
|
|
|
} else {
|
|
|
|
event.returnValue = responseArgs
|
|
|
|
}
|
2019-02-01 18:56:46 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|