fix(electron): allow using pre-ready apis (#19599)
This commit is contained in:
parent
d7e7cab44a
commit
971e30482a
|
|
@ -31,27 +31,30 @@ for (const arg of chromiumSwitches) {
|
||||||
app.commandLine.appendSwitch(match[1], match[2]);
|
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);
|
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;
|
require(appPath);
|
||||||
app.on('ready', launchInfo => launchInfoEventPayload = launchInfo);
|
|
||||||
|
|
||||||
(globalThis as any).__playwright_run = async () => {
|
(globalThis as any).__playwright_run = async () => {
|
||||||
// Wait for app to be ready to avoid browser initialization races.
|
// Wait for app to be ready to avoid browser initialization races.
|
||||||
await app.whenReady();
|
const event = await originalWhenReady;
|
||||||
|
|
||||||
// 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.
|
|
||||||
isReady = true;
|
isReady = true;
|
||||||
whenReadyCallback!();
|
whenReadyCallback(event);
|
||||||
app.emit('will-finish-launching');
|
originalEmit('ready', ...readyEventArgs);
|
||||||
app.emit('ready', launchInfoEventPayload);
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
const { app, protocol } = require('electron');
|
const { app, protocol } = require('electron');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
|
// Test using pre-ready apis.
|
||||||
|
protocol.registerSchemesAsPrivileged([]);
|
||||||
|
|
||||||
app.on('window-all-closed', e => e.preventDefault());
|
app.on('window-all-closed', e => e.preventDefault());
|
||||||
|
|
||||||
app.whenReady().then(() => {
|
app.whenReady().then(() => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue