From 018f3146a83ccf5794ebc46a0846ae76cc9cd90e Mon Sep 17 00:00:00 2001 From: Gnome Bard Date: Sat, 1 May 2021 09:46:27 +0800 Subject: [PATCH] fix(electron): deliver promised _nodeElectronHandle (#6348) --- src/dispatchers/electronDispatcher.ts | 4 ++-- src/server/electron/electron.ts | 11 +++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/dispatchers/electronDispatcher.ts b/src/dispatchers/electronDispatcher.ts index 010601c6cc696..72e8990b5ec5c 100644 --- a/src/dispatchers/electronDispatcher.ts +++ b/src/dispatchers/electronDispatcher.ts @@ -50,12 +50,12 @@ export class ElectronApplicationDispatcher extends Dispatcher { - const handle = this._object._nodeElectronHandle!; + const handle = await this._object._nodeElectronHandlePromised; return { value: serializeResult(await handle.evaluateExpressionAndWaitForSignals(params.expression, params.isFunction, true /* returnByValue */, parseArgument(params.arg))) }; } async evaluateExpressionHandle(params: channels.ElectronApplicationEvaluateExpressionHandleParams): Promise { - const handle = this._object._nodeElectronHandle!; + const handle = await this._object._nodeElectronHandlePromised; const result = await handle.evaluateExpressionAndWaitForSignals(params.expression, params.isFunction, false /* returnByValue */, parseArgument(params.arg)); return { handle: ElementHandleDispatcher.fromJSHandle(this._scope, result) }; } diff --git a/src/server/electron/electron.ts b/src/server/electron/electron.ts index 5a609f0c84bd5..42cdbf41dbb6e 100644 --- a/src/server/electron/electron.ts +++ b/src/server/electron/electron.ts @@ -58,7 +58,8 @@ export class ElectronApplication extends SdkObject { private _nodeConnection: CRConnection; private _nodeSession: CRSession; private _nodeExecutionContext: js.ExecutionContext | undefined; - _nodeElectronHandle: js.JSHandle | undefined; + _nodeElectronHandlePromised: Promise>; + private _resolveNodeElectronHandle!: (handle: js.JSHandle) => void; private _windows = new Set(); private _lastWindowId = 0; readonly _timeoutSettings = new TimeoutSettings(); @@ -73,6 +74,7 @@ export class ElectronApplication extends SdkObject { this._browserContext.on(BrowserContext.Events.Page, event => this._onPage(event)); this._nodeConnection = nodeConnection; this._nodeSession = nodeConnection.rootSession; + this._nodeElectronHandlePromised = new Promise(resolve => this._resolveNodeElectronHandle = resolve); } private async _onPage(page: ElectronPage) { @@ -87,7 +89,7 @@ export class ElectronApplication extends SdkObject { this._windows.add(page); // Below is async. - const handle = await this._nodeElectronHandle!.evaluateHandle(({ BrowserWindow }, windowId) => BrowserWindow.fromId(windowId), windowId).catch(e => {}); + const handle = await (await this._nodeElectronHandlePromised).evaluateHandle(({ BrowserWindow }, windowId) => BrowserWindow.fromId(windowId), windowId).catch(e => {}); if (!handle) return; page.browserWindow = handle; @@ -103,7 +105,7 @@ export class ElectronApplication extends SdkObject { async close() { const progressController = new ProgressController(internalCallMetadata(), this); const closed = progressController.run(progress => helper.waitForEvent(progress, this, ElectronApplication.Events.Close).promise, this._timeoutSettings.timeout({})); - await this._nodeElectronHandle!.evaluate(({ app }) => app.quit()); + await (await this._nodeElectronHandlePromised).evaluate(({ app }) => app.quit()); this._nodeConnection.close(); await closed; } @@ -114,7 +116,8 @@ export class ElectronApplication extends SdkObject { this._nodeExecutionContext = new js.ExecutionContext(this, new CRExecutionContext(this._nodeSession, event.context)); }); await this._nodeSession.send('Runtime.enable', {}).catch(e => {}); - this._nodeElectronHandle = await js.evaluate(this._nodeExecutionContext!, false /* returnByValue */, `process.mainModule.require('electron')`); + js.evaluate(this._nodeExecutionContext!, false /* returnByValue */, `process.mainModule.require('electron')`) + .then(this._resolveNodeElectronHandle); } }