Skip to content

Commit e78bd24

Browse files
committed
cherry-pick(#19599): fix(electron): allow using pre-ready apis
1 parent d10cc91 commit e78bd24

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

packages/playwright-core/src/server/electron/loader.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,30 @@ for (const arg of chromiumSwitches) {
3131
app.commandLine.appendSwitch(match[1], match[2]);
3232
}
3333

34+
// Defer ready event.
35+
const originalWhenReady = app.whenReady();
36+
const originalEmit = app.emit.bind(app);
37+
let readyEventArgs: any[];
38+
app.emit = (event: string | symbol, ...args: any[]): boolean => {
39+
if (event === 'ready') {
40+
readyEventArgs = args;
41+
return app.listenerCount('ready') > 0;
42+
}
43+
return originalEmit(event, ...args);
44+
};
3445
app.getAppPath = () => path.dirname(appPath);
46+
let isReady = false;
47+
let whenReadyCallback: (event: any) => any;
48+
const whenReadyPromise = new Promise<void>(f => whenReadyCallback = f);
49+
app.isReady = () => isReady;
50+
app.whenReady = () => whenReadyPromise;
3551

36-
let launchInfoEventPayload: any;
37-
app.on('ready', launchInfo => launchInfoEventPayload = launchInfo);
52+
require(appPath);
3853

3954
(globalThis as any).__playwright_run = async () => {
4055
// Wait for app to be ready to avoid browser initialization races.
41-
await app.whenReady();
42-
43-
// Override isReady pipeline.
44-
let isReady = false;
45-
let whenReadyCallback: () => void;
46-
const whenReadyPromise = new Promise<void>(f => whenReadyCallback = f);
47-
app.isReady = () => isReady;
48-
app.whenReady = () => whenReadyPromise;
49-
50-
require(appPath);
51-
52-
// Trigger isReady.
56+
const event = await originalWhenReady;
5357
isReady = true;
54-
whenReadyCallback!();
55-
app.emit('will-finish-launching');
56-
app.emit('ready', launchInfoEventPayload);
58+
whenReadyCallback(event);
59+
originalEmit('ready', ...readyEventArgs);
5760
};

tests/electron/electron-app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const { app, protocol } = require('electron');
22
const path = require('path');
33

4+
// Test using pre-ready apis.
5+
protocol.registerSchemesAsPrivileged([]);
6+
47
app.on('window-all-closed', e => e.preventDefault());
58

69
app.whenReady().then(() => {

0 commit comments

Comments
 (0)