refactor: add ipcMainUtils.invokeInWebContents / ipcRendererUtils.handle helpers (#17313)
This commit is contained in:
parent
df7dc9396e
commit
faabd0cc8b
5 changed files with 44 additions and 5 deletions
|
@ -1,7 +1,7 @@
|
|||
import { ipcMainInternal } from '@electron/internal/browser/ipc-main-internal'
|
||||
import * as errorUtils from '@electron/internal/common/error-utils'
|
||||
|
||||
type IPCHandler = (...args: any[]) => any
|
||||
type IPCHandler = (event: ElectronInternal.IpcMainInternalEvent, ...args: any[]) => any
|
||||
|
||||
const callHandler = async function (handler: IPCHandler, event: ElectronInternal.IpcMainInternalEvent, args: any[], reply: (args: any[]) => void) {
|
||||
try {
|
||||
|
@ -23,3 +23,21 @@ export const handle = function <T extends IPCHandler> (channel: string, handler:
|
|||
})
|
||||
})
|
||||
}
|
||||
|
||||
let nextId = 0
|
||||
|
||||
export function invokeInWebContents<T> (sender: Electron.WebContentsInternal, command: string, ...args: any[]) {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
const requestId = ++nextId
|
||||
ipcMainInternal.once(`${command}_RESPONSE_${requestId}`, (
|
||||
_event, error: Electron.SerializedError, result: any
|
||||
) => {
|
||||
if (error) {
|
||||
reject(errorUtils.deserialize(error))
|
||||
} else {
|
||||
resolve(result)
|
||||
}
|
||||
})
|
||||
sender._sendInternal(command, requestId, ...args)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue