refactor: add ipcMainUtils.invokeInWebContents / ipcRendererUtils.handle helpers (#17313)

This commit is contained in:
Milan Burda 2019-03-13 20:03:17 +01:00 committed by Shelley Vohr
parent df7dc9396e
commit faabd0cc8b
5 changed files with 44 additions and 5 deletions

View file

@ -1,6 +1,21 @@
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'
import * as errorUtils from '@electron/internal/common/error-utils'
type IPCHandler = (event: Electron.IpcRendererEvent, ...args: any[]) => any
export const handle = function <T extends IPCHandler> (channel: string, handler: T) {
ipcRendererInternal.on(channel, (event, requestId, ...args) => {
new Promise(resolve => resolve(handler(event, ...args))
).then(result => {
return [null, result]
}, error => {
return [errorUtils.serialize(error)]
}).then(responseArgs => {
event.sender.send(`${channel}_RESPONSE_${requestId}`, ...responseArgs)
})
})
}
let nextId = 0
export function invoke<T> (command: string, ...args: any[]) {