Skip to content

Commit 20b83ee

Browse files
authored
fix(electron): do not use --require to throttle startup (#4006)
1 parent 6dccd27 commit 20b83ee

File tree

2 files changed

+13
-89
lines changed

2 files changed

+13
-89
lines changed

src/server/electron/electron.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
import * as path from 'path';
1817
import * as os from 'os';
1918
import { CRBrowser, CRBrowserContext } from '../chromium/crBrowser';
2019
import { CRConnection, CRSession } from '../chromium/crConnection';
@@ -77,17 +76,18 @@ export class ElectronApplication extends EventEmitter {
7776
private async _onPage(page: ElectronPage) {
7877
// Needs to be sync.
7978
const windowId = ++this._lastWindowId;
80-
// Can be async.
81-
const handle = await this._nodeElectronHandle!.evaluateHandle(({ BrowserWindow }, windowId) => BrowserWindow.fromId(windowId), windowId).catch(e => {});
82-
if (!handle)
83-
return;
84-
page.browserWindow = handle;
85-
page._browserWindowId = windowId;
8679
page.on(Page.Events.Close, () => {
8780
page.browserWindow.dispose();
8881
this._windows.delete(page);
8982
});
83+
page._browserWindowId = windowId;
9084
this._windows.add(page);
85+
86+
// Below is async.
87+
const handle = await this._nodeElectronHandle!.evaluateHandle(({ BrowserWindow }, windowId) => BrowserWindow.fromId(windowId), windowId).catch(e => {});
88+
if (!handle)
89+
return;
90+
page.browserWindow = handle;
9191
await runAbortableTask(progress => page.mainFrame()._waitForLoadState(progress, 'domcontentloaded'), page._timeoutSettings.navigationTimeout({})).catch(e => {}); // can happen after detach
9292
this.emit(ElectronApplication.Events.Window, page);
9393
}
@@ -126,16 +126,12 @@ export class ElectronApplication extends EventEmitter {
126126
}
127127

128128
async _init() {
129-
this._nodeSession.once('Runtime.executionContextCreated', event => {
130-
this._nodeExecutionContext = new js.ExecutionContext(new CRExecutionContext(this._nodeSession, event.context));
129+
this._nodeSession.on('Runtime.executionContextCreated', (event: any) => {
130+
if (event.context.auxData && event.context.auxData.isDefault)
131+
this._nodeExecutionContext = new js.ExecutionContext(new CRExecutionContext(this._nodeSession, event.context));
131132
});
132133
await this._nodeSession.send('Runtime.enable', {}).catch(e => {});
133-
this._nodeElectronHandle = await js.evaluate(this._nodeExecutionContext!, false /* returnByValue */, () => {
134-
// Resolving the race between the debugger and the boot-time script.
135-
if ((global as any)._playwrightRun)
136-
return (global as any)._playwrightRun();
137-
return new Promise(f => (global as any)._playwrightRunCallback = f);
138-
});
134+
this._nodeElectronHandle = await js.evaluate(this._nodeExecutionContext!, false /* returnByValue */, `process.mainModule.require('electron')`);
139135
}
140136
}
141137

@@ -151,7 +147,7 @@ export class Electron {
151147
controller.setLogName('browser');
152148
return controller.run(async progress => {
153149
let app: ElectronApplication | undefined = undefined;
154-
const electronArguments = ['--inspect=0', '--remote-debugging-port=0', '--require', path.join(__dirname, 'electronLoader.js'), ...args];
150+
const electronArguments = ['--inspect=0', '--remote-debugging-port=0', ...args];
155151

156152
if (os.platform() === 'linux') {
157153
const runningAsRoot = process.geteuid && process.geteuid() === 0;
@@ -171,7 +167,7 @@ export class Electron {
171167
cwd: options.cwd,
172168
tempDirectories: [],
173169
attemptToGracefullyClose: () => app!.close(),
174-
onExit: (exitCode, signal) => {},
170+
onExit: () => {},
175171
});
176172

177173
const nodeMatch = await waitForLine(progress, launchedProcess, launchedProcess.stderr, /^Debugger listening on (ws:\/\/.*)$/);

src/server/electron/electronLoader.ts

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)