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
|
@ -9,6 +9,7 @@ const { app, ipcMain, session, deprecate } = electron
|
||||||
|
|
||||||
const NavigationController = require('@electron/internal/browser/navigation-controller')
|
const NavigationController = require('@electron/internal/browser/navigation-controller')
|
||||||
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal')
|
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal')
|
||||||
|
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils')
|
||||||
const errorUtils = require('@electron/internal/common/error-utils')
|
const errorUtils = require('@electron/internal/common/error-utils')
|
||||||
|
|
||||||
// session is not used here, the purpose is to make sure session is initalized
|
// session is not used here, the purpose is to make sure session is initalized
|
||||||
|
@ -193,7 +194,7 @@ const asyncWebFrameMethods = function (requestId, method, callback, ...args) {
|
||||||
|
|
||||||
for (const method of webFrameMethods) {
|
for (const method of webFrameMethods) {
|
||||||
WebContents.prototype[method] = function (...args) {
|
WebContents.prototype[method] = function (...args) {
|
||||||
this._sendInternal('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', method, args)
|
ipcMainUtils.invokeInWebContents(this, 'ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', method, ...args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { ipcMainInternal } from '@electron/internal/browser/ipc-main-internal'
|
import { ipcMainInternal } from '@electron/internal/browser/ipc-main-internal'
|
||||||
import * as errorUtils from '@electron/internal/common/error-utils'
|
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) {
|
const callHandler = async function (handler: IPCHandler, event: ElectronInternal.IpcMainInternalEvent, args: any[], reply: (args: any[]) => void) {
|
||||||
try {
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,21 @@
|
||||||
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'
|
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'
|
||||||
import * as errorUtils from '@electron/internal/common/error-utils'
|
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
|
let nextId = 0
|
||||||
|
|
||||||
export function invoke<T> (command: string, ...args: any[]) {
|
export function invoke<T> (command: string, ...args: any[]) {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { webFrame, WebFrame } from 'electron'
|
import { webFrame, WebFrame } from 'electron'
|
||||||
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'
|
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'
|
||||||
|
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils'
|
||||||
import * as errorUtils from '@electron/internal/common/error-utils'
|
import * as errorUtils from '@electron/internal/common/error-utils'
|
||||||
|
|
||||||
// All keys of WebFrame that extend Function
|
// All keys of WebFrame that extend Function
|
||||||
|
@ -10,13 +11,13 @@ type WebFrameMethod = {
|
||||||
|
|
||||||
export const webFrameInit = () => {
|
export const webFrameInit = () => {
|
||||||
// Call webFrame method
|
// Call webFrame method
|
||||||
ipcRendererInternal.on('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', (
|
ipcRendererUtils.handle('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', (
|
||||||
_event, method: keyof WebFrameMethod, args: any[]
|
event, method: keyof WebFrameMethod, ...args: any[]
|
||||||
) => {
|
) => {
|
||||||
// The TypeScript compiler cannot handle the sheer number of
|
// The TypeScript compiler cannot handle the sheer number of
|
||||||
// call signatures here and simply gives up. Incorrect invocations
|
// call signatures here and simply gives up. Incorrect invocations
|
||||||
// will be caught by "keyof WebFrameMethod" though.
|
// will be caught by "keyof WebFrameMethod" though.
|
||||||
(webFrame[method] as any)(...args)
|
return (webFrame[method] as any)(...args)
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcRendererInternal.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', (
|
ipcRendererInternal.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', (
|
||||||
|
|
4
typings/internal-electron.d.ts
vendored
4
typings/internal-electron.d.ts
vendored
|
@ -55,6 +55,10 @@ declare namespace Electron {
|
||||||
sendToAll(webContentsId: number, channel: string, ...args: any[]): void
|
sendToAll(webContentsId: number, channel: string, ...args: any[]): void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface WebContentsInternal extends Electron.WebContents {
|
||||||
|
_sendInternal(channel: string, ...args: any[]): void;
|
||||||
|
}
|
||||||
|
|
||||||
const deprecate: ElectronInternal.DeprecationUtil;
|
const deprecate: ElectronInternal.DeprecationUtil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue