Skip to content

Commit

Permalink
fix(electron): deliver promised _nodeElectronHandle (#6348)
Browse files Browse the repository at this point in the history
  • Loading branch information
msyfls123 committed May 1, 2021
1 parent de21a94 commit 018f314
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/dispatchers/electronDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ export class ElectronApplicationDispatcher extends Dispatcher<ElectronApplicatio
}

async evaluateExpression(params: channels.ElectronApplicationEvaluateExpressionParams): Promise<channels.ElectronApplicationEvaluateExpressionResult> {
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<channels.ElectronApplicationEvaluateExpressionHandleResult> {
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) };
}
Expand Down
11 changes: 7 additions & 4 deletions src/server/electron/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export class ElectronApplication extends SdkObject {
private _nodeConnection: CRConnection;
private _nodeSession: CRSession;
private _nodeExecutionContext: js.ExecutionContext | undefined;
_nodeElectronHandle: js.JSHandle<any> | undefined;
_nodeElectronHandlePromised: Promise<js.JSHandle<any>>;
private _resolveNodeElectronHandle!: (handle: js.JSHandle<any>) => void;
private _windows = new Set<ElectronPage>();
private _lastWindowId = 0;
readonly _timeoutSettings = new TimeoutSettings();
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 018f314

Please sign in to comment.