refactor(desktop): 精简主进程启动与退出逻辑并减少打包态日志噪音

This commit is contained in:
2026-02-09 01:13:08 +08:00
parent f9edfd0058
commit 00c944e1b5

View File

@@ -10,6 +10,15 @@ let mainWindow: BrowserWindow | null = null
let serverProcess: ReturnType<typeof spawn> | 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<number> =>
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()
})