Skip to content

Commit 018f314

Browse files
authored
fix(electron): deliver promised _nodeElectronHandle (#6348)
1 parent de21a94 commit 018f314

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/dispatchers/electronDispatcher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ export class ElectronApplicationDispatcher extends Dispatcher<ElectronApplicatio
5050
}
5151

5252
async evaluateExpression(params: channels.ElectronApplicationEvaluateExpressionParams): Promise<channels.ElectronApplicationEvaluateExpressionResult> {
53-
const handle = this._object._nodeElectronHandle!;
53+
const handle = await this._object._nodeElectronHandlePromised;
5454
return { value: serializeResult(await handle.evaluateExpressionAndWaitForSignals(params.expression, params.isFunction, true /* returnByValue */, parseArgument(params.arg))) };
5555
}
5656

5757
async evaluateExpressionHandle(params: channels.ElectronApplicationEvaluateExpressionHandleParams): Promise<channels.ElectronApplicationEvaluateExpressionHandleResult> {
58-
const handle = this._object._nodeElectronHandle!;
58+
const handle = await this._object._nodeElectronHandlePromised;
5959
const result = await handle.evaluateExpressionAndWaitForSignals(params.expression, params.isFunction, false /* returnByValue */, parseArgument(params.arg));
6060
return { handle: ElementHandleDispatcher.fromJSHandle(this._scope, result) };
6161
}

src/server/electron/electron.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ export class ElectronApplication extends SdkObject {
5858
private _nodeConnection: CRConnection;
5959
private _nodeSession: CRSession;
6060
private _nodeExecutionContext: js.ExecutionContext | undefined;
61-
_nodeElectronHandle: js.JSHandle<any> | undefined;
61+
_nodeElectronHandlePromised: Promise<js.JSHandle<any>>;
62+
private _resolveNodeElectronHandle!: (handle: js.JSHandle<any>) => void;
6263
private _windows = new Set<ElectronPage>();
6364
private _lastWindowId = 0;
6465
readonly _timeoutSettings = new TimeoutSettings();
@@ -73,6 +74,7 @@ export class ElectronApplication extends SdkObject {
7374
this._browserContext.on(BrowserContext.Events.Page, event => this._onPage(event));
7475
this._nodeConnection = nodeConnection;
7576
this._nodeSession = nodeConnection.rootSession;
77+
this._nodeElectronHandlePromised = new Promise(resolve => this._resolveNodeElectronHandle = resolve);
7678
}
7779

7880
private async _onPage(page: ElectronPage) {
@@ -87,7 +89,7 @@ export class ElectronApplication extends SdkObject {
8789
this._windows.add(page);
8890

8991
// Below is async.
90-
const handle = await this._nodeElectronHandle!.evaluateHandle(({ BrowserWindow }, windowId) => BrowserWindow.fromId(windowId), windowId).catch(e => {});
92+
const handle = await (await this._nodeElectronHandlePromised).evaluateHandle(({ BrowserWindow }, windowId) => BrowserWindow.fromId(windowId), windowId).catch(e => {});
9193
if (!handle)
9294
return;
9395
page.browserWindow = handle;
@@ -103,7 +105,7 @@ export class ElectronApplication extends SdkObject {
103105
async close() {
104106
const progressController = new ProgressController(internalCallMetadata(), this);
105107
const closed = progressController.run(progress => helper.waitForEvent(progress, this, ElectronApplication.Events.Close).promise, this._timeoutSettings.timeout({}));
106-
await this._nodeElectronHandle!.evaluate(({ app }) => app.quit());
108+
await (await this._nodeElectronHandlePromised).evaluate(({ app }) => app.quit());
107109
this._nodeConnection.close();
108110
await closed;
109111
}
@@ -114,7 +116,8 @@ export class ElectronApplication extends SdkObject {
114116
this._nodeExecutionContext = new js.ExecutionContext(this, new CRExecutionContext(this._nodeSession, event.context));
115117
});
116118
await this._nodeSession.send('Runtime.enable', {}).catch(e => {});
117-
this._nodeElectronHandle = await js.evaluate(this._nodeExecutionContext!, false /* returnByValue */, `process.mainModule.require('electron')`);
119+
js.evaluate(this._nodeExecutionContext!, false /* returnByValue */, `process.mainModule.require('electron')`)
120+
.then(this._resolveNodeElectronHandle);
118121
}
119122
}
120123

0 commit comments

Comments
 (0)