refactor(desktop): 精简主进程启动与退出逻辑并减少打包态日志噪音
This commit is contained in:
@@ -10,6 +10,15 @@ let mainWindow: BrowserWindow | null = null
|
|||||||
let serverProcess: ReturnType<typeof spawn> | null = null
|
let serverProcess: ReturnType<typeof spawn> | null = null
|
||||||
let isQuitting = false
|
let isQuitting = false
|
||||||
|
|
||||||
|
const shouldAbortWindowLoad = (): boolean =>
|
||||||
|
isQuitting || !mainWindow || mainWindow.isDestroyed()
|
||||||
|
|
||||||
|
const logLifecycle = (message: string) => {
|
||||||
|
if (!app.isPackaged) {
|
||||||
|
console.log(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const getAvailablePort = (): Promise<number> =>
|
const getAvailablePort = (): Promise<number> =>
|
||||||
new Promise((resolve, reject) => {
|
new Promise((resolve, reject) => {
|
||||||
const server = createServer()
|
const server = createServer()
|
||||||
@@ -132,14 +141,14 @@ const createWindow = async () => {
|
|||||||
mainWindow.show()
|
mainWindow.show()
|
||||||
|
|
||||||
const serverUrl = await getServerUrl()
|
const serverUrl = await getServerUrl()
|
||||||
if (!serverUrl || isQuitting || !mainWindow || mainWindow.isDestroyed()) {
|
if (!serverUrl || shouldAbortWindowLoad()) {
|
||||||
stopServerProcess()
|
stopServerProcess()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Waiting for server at ${serverUrl}...`)
|
logLifecycle(`Waiting for server at ${serverUrl}...`)
|
||||||
const ready = await waitForServer(serverUrl)
|
const ready = await waitForServer(serverUrl)
|
||||||
if (isQuitting || !mainWindow || mainWindow.isDestroyed()) {
|
if (shouldAbortWindowLoad()) {
|
||||||
stopServerProcess()
|
stopServerProcess()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -154,19 +163,23 @@ const createWindow = async () => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Loading ${serverUrl}`)
|
logLifecycle(`Loading ${serverUrl}`)
|
||||||
if (!mainWindow || mainWindow.isDestroyed()) {
|
if (shouldAbortWindowLoad()) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mainWindow.loadURL(serverUrl)
|
mainWindow.loadURL(serverUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const beginQuit = () => {
|
||||||
|
isQuitting = true
|
||||||
|
stopServerProcess()
|
||||||
|
}
|
||||||
|
|
||||||
app.whenReady().then(createWindow)
|
app.whenReady().then(createWindow)
|
||||||
|
|
||||||
app.on('window-all-closed', () => {
|
app.on('window-all-closed', () => {
|
||||||
if (process.platform !== 'darwin') {
|
if (process.platform !== 'darwin') {
|
||||||
isQuitting = true
|
beginQuit()
|
||||||
stopServerProcess()
|
|
||||||
app.quit()
|
app.quit()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -178,6 +191,5 @@ app.on('activate', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
app.on('before-quit', () => {
|
app.on('before-quit', () => {
|
||||||
isQuitting = true
|
beginQuit()
|
||||||
stopServerProcess()
|
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user