feat: make async webContents / <webview> methods return a Promise (#18792)
This commit is contained in:
parent
632bbf948d
commit
deebde66f9
6 changed files with 32 additions and 35 deletions
|
@ -180,7 +180,7 @@ const webFrameMethods = [
|
|||
|
||||
for (const method of webFrameMethods) {
|
||||
WebContents.prototype[method] = function (...args) {
|
||||
ipcMainUtils.invokeInWebContents(this, false, 'ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', method, ...args)
|
||||
return ipcMainUtils.invokeInWebContents(this, false, 'ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', method, ...args)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,11 +4,7 @@ const { webContents } = require('electron')
|
|||
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal')
|
||||
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils')
|
||||
const parseFeaturesString = require('@electron/internal/common/parse-features-string')
|
||||
const {
|
||||
syncMethods,
|
||||
asyncCallbackMethods,
|
||||
asyncPromiseMethods
|
||||
} = require('@electron/internal/common/web-view-methods')
|
||||
const { syncMethods, asyncMethods } = require('@electron/internal/common/web-view-methods')
|
||||
|
||||
// Doesn't exist in early initialization.
|
||||
let webViewManager = null
|
||||
|
@ -371,11 +367,7 @@ ipcMainInternal.on('ELECTRON_GUEST_VIEW_MANAGER_FOCUS_CHANGE', function (event,
|
|||
}
|
||||
})
|
||||
|
||||
const allMethods = new Set([
|
||||
...syncMethods,
|
||||
...asyncCallbackMethods,
|
||||
...asyncPromiseMethods
|
||||
])
|
||||
const allMethods = new Set([ ...syncMethods, ...asyncMethods ])
|
||||
|
||||
handleMessage('ELECTRON_GUEST_VIEW_MANAGER_CALL', function (event, guestInstanceId, method, args) {
|
||||
const guest = getGuestForWebContents(guestInstanceId, event.sender)
|
||||
|
|
|
@ -53,18 +53,15 @@ exports.syncMethods = new Set([
|
|||
'setZoomLevel'
|
||||
])
|
||||
|
||||
exports.asyncCallbackMethods = new Set([
|
||||
exports.asyncMethods = new Set([
|
||||
'capturePage',
|
||||
'executeJavaScript',
|
||||
'insertCSS',
|
||||
'insertText',
|
||||
'send',
|
||||
'sendInputEvent',
|
||||
'setLayoutZoomLevelLimits',
|
||||
'setVisualZoomLevelLimits',
|
||||
'print'
|
||||
])
|
||||
|
||||
exports.asyncPromiseMethods = new Set([
|
||||
'capturePage',
|
||||
'executeJavaScript',
|
||||
'print',
|
||||
'printToPDF'
|
||||
])
|
||||
|
|
|
@ -3,11 +3,7 @@ import { remote, webFrame } from 'electron'
|
|||
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils'
|
||||
import * as guestViewInternal from '@electron/internal/renderer/web-view/guest-view-internal'
|
||||
import { WEB_VIEW_CONSTANTS } from '@electron/internal/renderer/web-view/web-view-constants'
|
||||
import {
|
||||
syncMethods,
|
||||
asyncCallbackMethods,
|
||||
asyncPromiseMethods
|
||||
} from '@electron/internal/common/web-view-methods'
|
||||
import { syncMethods, asyncMethods } from '@electron/internal/common/web-view-methods'
|
||||
|
||||
const v8Util = process.electronBinding('v8_util')
|
||||
|
||||
|
@ -257,23 +253,13 @@ export const setupMethods = (WebViewElement: typeof ElectronInternal.WebViewElem
|
|||
}
|
||||
|
||||
const createNonBlockHandler = function (method: string) {
|
||||
return function (this: ElectronInternal.WebViewElement, ...args: Array<any>) {
|
||||
ipcRendererUtils.invoke('ELECTRON_GUEST_VIEW_MANAGER_CALL', this.getWebContentsId(), method, args)
|
||||
}
|
||||
}
|
||||
|
||||
for (const method of asyncCallbackMethods) {
|
||||
(WebViewElement.prototype as Record<string, any>)[method] = createNonBlockHandler(method)
|
||||
}
|
||||
|
||||
const createPromiseHandler = function (method: string) {
|
||||
return function (this: ElectronInternal.WebViewElement, ...args: Array<any>) {
|
||||
return ipcRendererUtils.invoke('ELECTRON_GUEST_VIEW_MANAGER_CALL', this.getWebContentsId(), method, args)
|
||||
}
|
||||
}
|
||||
|
||||
for (const method of asyncPromiseMethods) {
|
||||
(WebViewElement.prototype as Record<string, any>)[method] = createPromiseHandler(method)
|
||||
for (const method of asyncMethods) {
|
||||
(WebViewElement.prototype as Record<string, any>)[method] = createNonBlockHandler(method)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue