From 00c944e1b52ebf876909088f6d4808034806ddad Mon Sep 17 00:00:00 2001 From: imbytecat Date: Mon, 9 Feb 2026 01:13:08 +0800 Subject: [PATCH] =?UTF-8?q?refactor(desktop):=20=E7=B2=BE=E7=AE=80?= =?UTF-8?q?=E4=B8=BB=E8=BF=9B=E7=A8=8B=E5=90=AF=E5=8A=A8=E4=B8=8E=E9=80=80?= =?UTF-8?q?=E5=87=BA=E9=80=BB=E8=BE=91=E5=B9=B6=E5=87=8F=E5=B0=91=E6=89=93?= =?UTF-8?q?=E5=8C=85=E6=80=81=E6=97=A5=E5=BF=97=E5=99=AA=E9=9F=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/desktop/src/main/index.ts | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/apps/desktop/src/main/index.ts b/apps/desktop/src/main/index.ts index 2f03156..2647e64 100644 --- a/apps/desktop/src/main/index.ts +++ b/apps/desktop/src/main/index.ts @@ -10,6 +10,15 @@ let mainWindow: BrowserWindow | null = null let serverProcess: ReturnType | null = null let isQuitting = false +const shouldAbortWindowLoad = (): boolean => + isQuitting || !mainWindow || mainWindow.isDestroyed() + +const logLifecycle = (message: string) => { + if (!app.isPackaged) { + console.log(message) + } +} + const getAvailablePort = (): Promise => new Promise((resolve, reject) => { const server = createServer() @@ -132,14 +141,14 @@ const createWindow = async () => { mainWindow.show() const serverUrl = await getServerUrl() - if (!serverUrl || isQuitting || !mainWindow || mainWindow.isDestroyed()) { + if (!serverUrl || shouldAbortWindowLoad()) { stopServerProcess() return } - console.log(`Waiting for server at ${serverUrl}...`) + logLifecycle(`Waiting for server at ${serverUrl}...`) const ready = await waitForServer(serverUrl) - if (isQuitting || !mainWindow || mainWindow.isDestroyed()) { + if (shouldAbortWindowLoad()) { stopServerProcess() return } @@ -154,19 +163,23 @@ const createWindow = async () => { return } - console.log(`Loading ${serverUrl}`) - if (!mainWindow || mainWindow.isDestroyed()) { + logLifecycle(`Loading ${serverUrl}`) + if (shouldAbortWindowLoad()) { return } mainWindow.loadURL(serverUrl) } +const beginQuit = () => { + isQuitting = true + stopServerProcess() +} + app.whenReady().then(createWindow) app.on('window-all-closed', () => { if (process.platform !== 'darwin') { - isQuitting = true - stopServerProcess() + beginQuit() app.quit() } }) @@ -178,6 +191,5 @@ app.on('activate', () => { }) app.on('before-quit', () => { - isQuitting = true - stopServerProcess() + beginQuit() })