mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-26 14:58:55 +08:00
fix(electron): use dynamic load for exposed meta (#5251)
There is high possibilities of circular dependencies when importing `exposed-meta` module. Change it to dynamic import to mitigate the issue..
This commit is contained in:
@@ -5,10 +5,9 @@ import electronWindowState from 'electron-window-state';
|
||||
import { join } from 'path';
|
||||
|
||||
import { isMacOS, isWindows } from '../shared/utils';
|
||||
import { getExposedMeta } from './exposed';
|
||||
import { ensureHelperProcess } from './helper-process';
|
||||
import { logger } from './logger';
|
||||
import { uiSubjects } from './ui';
|
||||
import { uiSubjects } from './ui/subject';
|
||||
import { parseCookie } from './utils';
|
||||
|
||||
const IS_DEV: boolean =
|
||||
@@ -18,7 +17,19 @@ const DEV_TOOL = process.env.DEV_TOOL === 'true';
|
||||
|
||||
export const mainWindowOrigin = process.env.DEV_SERVER_URL || 'file://.';
|
||||
|
||||
async function createWindow() {
|
||||
// todo: not all window need all of the exposed meta
|
||||
const getWindowAdditionalArguments = async () => {
|
||||
const { getExposedMeta } = await import('./exposed');
|
||||
const mainExposedMeta = getExposedMeta();
|
||||
const helperProcessManager = await ensureHelperProcess();
|
||||
const helperExposedMeta = await helperProcessManager.rpc?.getMeta();
|
||||
return [
|
||||
`--main-exposed-meta=` + JSON.stringify(mainExposedMeta),
|
||||
`--helper-exposed-meta=` + JSON.stringify(helperExposedMeta),
|
||||
];
|
||||
};
|
||||
|
||||
async function createWindow(additionalArguments: string[]) {
|
||||
logger.info('create window');
|
||||
const mainWindowState = electronWindowState({
|
||||
defaultWidth: 1000,
|
||||
@@ -30,8 +41,6 @@ async function createWindow() {
|
||||
|
||||
assert(helperExposedMeta, 'helperExposedMeta should be defined');
|
||||
|
||||
const mainExposedMeta = getExposedMeta();
|
||||
|
||||
const browserWindow = new BrowserWindow({
|
||||
titleBarStyle: isMacOS()
|
||||
? 'hiddenInset'
|
||||
@@ -56,10 +65,7 @@ async function createWindow() {
|
||||
spellcheck: false, // FIXME: enable?
|
||||
preload: join(__dirname, './preload.js'),
|
||||
// serialize exposed meta that to be used in preload
|
||||
additionalArguments: [
|
||||
`--main-exposed-meta=` + JSON.stringify(mainExposedMeta),
|
||||
`--helper-exposed-meta=` + JSON.stringify(helperExposedMeta),
|
||||
],
|
||||
additionalArguments: additionalArguments,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -146,16 +152,24 @@ async function createWindow() {
|
||||
let browserWindow$: Promise<BrowserWindow> | undefined;
|
||||
|
||||
/**
|
||||
* Restore existing BrowserWindow or Create new BrowserWindow
|
||||
* Init main BrowserWindow. Will create a new window if it's not created yet.
|
||||
*/
|
||||
export async function getOrCreateWindow() {
|
||||
export async function initMainWindow() {
|
||||
if (!browserWindow$ || (await browserWindow$.then(w => w.isDestroyed()))) {
|
||||
browserWindow$ = createWindow();
|
||||
const additionalArguments = await getWindowAdditionalArguments();
|
||||
browserWindow$ = createWindow(additionalArguments);
|
||||
}
|
||||
const mainWindow = await browserWindow$;
|
||||
return mainWindow;
|
||||
}
|
||||
|
||||
export async function getMainWindow() {
|
||||
if (!browserWindow$) return;
|
||||
const window = await browserWindow$;
|
||||
if (window.isDestroyed()) return;
|
||||
return window;
|
||||
}
|
||||
|
||||
export async function handleOpenUrlInHiddenWindow(url: string) {
|
||||
const win = new BrowserWindow({
|
||||
width: 1200,
|
||||
|
||||
Reference in New Issue
Block a user