feat: promisify executeJavaScript (#17312)

This commit is contained in:
Milan Burda 2019-03-14 20:08:54 +01:00 committed by Shelley Vohr
parent 878538f2e8
commit 2e89348541
14 changed files with 164 additions and 100 deletions

View file

@ -1,7 +1,5 @@
import { webFrame, WebFrame } from 'electron'
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'
// All keys of WebFrame that extend Function
type WebFrameMethod = {
@ -19,21 +17,4 @@ export const webFrameInit = () => {
// will be caught by "keyof WebFrameMethod" though.
return (webFrame[method] as any)(...args)
})
ipcRendererInternal.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', (
event, requestId: number, method: keyof WebFrameMethod, args: any[]
) => {
new Promise(resolve =>
// The TypeScript compiler cannot handle the sheer number of
// call signatures here and simply gives up. Incorrect invocations
// will be caught by "keyof WebFrameMethod" though.
(webFrame[method] as any)(...args, resolve)
).then(result => {
return [null, result]
}, error => {
return [errorUtils.serialize(error)]
}).then(responseArgs => {
event.sender.send(`ELECTRON_INTERNAL_BROWSER_ASYNC_WEB_FRAME_RESPONSE_${requestId}`, ...responseArgs)
})
})
}