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
|
|
|
import * as errorUtils from '@electron/internal/common/error-utils'
|
|
|
|
|
|
|
|
let nextId = 0
|
|
|
|
|
|
|
|
export function invoke<T> (command: string, ...args: any[]) {
|
|
|
|
return new Promise<T>((resolve, reject) => {
|
|
|
|
const requestId = ++nextId
|
2019-02-15 01:24:25 +00:00
|
|
|
ipcRendererInternal.once(`${command}_RESPONSE_${requestId}`, (
|
2019-02-19 09:24:19 +00:00
|
|
|
_event, error: Electron.SerializedError, result: any
|
2019-02-14 07:05:49 +00:00
|
|
|
) => {
|
|
|
|
if (error) {
|
|
|
|
reject(errorUtils.deserialize(error))
|
|
|
|
} else {
|
|
|
|
resolve(result)
|
|
|
|
}
|
|
|
|
})
|
2019-02-15 01:24:25 +00:00
|
|
|
ipcRendererInternal.send(command, requestId, ...args)
|
2019-02-14 07:05:49 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function invokeSync<T> (command: string, ...args: any[]): T {
|
2019-02-15 01:24:25 +00:00
|
|
|
const [ error, result ] = ipcRendererInternal.sendSync(command, ...args)
|
2019-02-14 07:05:49 +00:00
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw errorUtils.deserialize(error)
|
|
|
|
} else {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|