Skip to content

Commit

Permalink
fix(electron): allow using pre-ready apis
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Dec 20, 2022
1 parent 412c11d commit e433ac1
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions packages/playwright-core/src/server/electron/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,30 @@ for (const arg of chromiumSwitches) {
app.commandLine.appendSwitch(match[1], match[2]);
}

// Defer ready event.
const originalWhenReady = app.whenReady();
const originalEmit = app.emit.bind(app);
let readyEventArgs: any[];
app.emit = (event: string | symbol, ...args: any[]): boolean => {
if (event === 'ready') {
readyEventArgs = args;
return app.listenerCount('ready') > 0;
}
return originalEmit(event, ...args);
};
app.getAppPath = () => path.dirname(appPath);
let isReady = false;
let whenReadyCallback: (event: any) => any;
const whenReadyPromise = new Promise<void>(f => whenReadyCallback = f);
app.isReady = () => isReady;
app.whenReady = () => whenReadyPromise;

let launchInfoEventPayload: any;
app.on('ready', launchInfo => launchInfoEventPayload = launchInfo);
require(appPath);

(globalThis as any).__playwright_run = async () => {
// Wait for app to be ready to avoid browser initialization races.
await app.whenReady();

// Override isReady pipeline.
let isReady = false;
let whenReadyCallback: () => void;
const whenReadyPromise = new Promise<void>(f => whenReadyCallback = f);
app.isReady = () => isReady;
app.whenReady = () => whenReadyPromise;

require(appPath);

// Trigger isReady.
const event = await originalWhenReady;
isReady = true;
whenReadyCallback!();
app.emit('will-finish-launching');
app.emit('ready', launchInfoEventPayload);
whenReadyCallback(event);
originalEmit('ready', ...readyEventArgs);
};

0 comments on commit e433ac1

Please sign in to comment.