refactor: replace ipcRendererUtils.invoke() with ipcRendererInternal.invoke() (#19574)
This commit is contained in:
parent
698120daf0
commit
81e9dab52f
29 changed files with 195 additions and 164 deletions
|
@ -4,38 +4,18 @@ 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[]) {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
const requestId = ++nextId
|
||||
ipcRendererInternal.once(`${command}_RESPONSE_${requestId}`, (
|
||||
_event, error: Electron.SerializedError, result: any
|
||||
) => {
|
||||
if (error) {
|
||||
reject(errorUtils.deserialize(error))
|
||||
} else {
|
||||
resolve(result)
|
||||
}
|
||||
})
|
||||
ipcRendererInternal.send(command, requestId, ...args)
|
||||
ipcRendererInternal.on(channel, async (event, requestId, ...args) => {
|
||||
const replyChannel = `${channel}_RESPONSE_${requestId}`
|
||||
try {
|
||||
event.sender.send(replyChannel, null, await handler(event, ...args))
|
||||
} catch (error) {
|
||||
event.sender.send(replyChannel, errorUtils.serialize(error))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function invokeSync<T> (command: string, ...args: any[]): T {
|
||||
const [ error, result ] = ipcRendererInternal.sendSync(command, null, ...args)
|
||||
const [ error, result ] = ipcRendererInternal.sendSync(command, ...args)
|
||||
|
||||
if (error) {
|
||||
throw errorUtils.deserialize(error)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue