refactor(desktop): simplify main process logic and improve naming
- Remove logLifecycle wrapper, inline the conditional logging - Remove redundant shouldAbortWindowLoad check before final loadURL - Rename getServerUrl to resolveServerUrl to reflect side effects - Add .catch on createWindow to prevent silent async failures
This commit is contained in:
@@ -13,12 +13,6 @@ 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()
|
||||
@@ -99,7 +93,7 @@ const spawnServer = (port: number): string => {
|
||||
return `http://127.0.0.1:${port}`
|
||||
}
|
||||
|
||||
const getServerUrl = async (): Promise<string | null> => {
|
||||
const resolveServerUrl = async (): Promise<string | null> => {
|
||||
if (!app.isPackaged) {
|
||||
return DEV_SERVER_URL
|
||||
}
|
||||
@@ -140,13 +134,13 @@ const createWindow = async () => {
|
||||
}
|
||||
mainWindow.show()
|
||||
|
||||
const serverUrl = await getServerUrl()
|
||||
const serverUrl = await resolveServerUrl()
|
||||
if (!serverUrl || shouldAbortWindowLoad()) {
|
||||
stopServerProcess()
|
||||
return
|
||||
}
|
||||
|
||||
logLifecycle(`Waiting for server at ${serverUrl}...`)
|
||||
if (!app.isPackaged) console.log(`Waiting for server at ${serverUrl}...`)
|
||||
const ready = await waitForServer(serverUrl)
|
||||
if (shouldAbortWindowLoad()) {
|
||||
stopServerProcess()
|
||||
@@ -163,10 +157,6 @@ const createWindow = async () => {
|
||||
return
|
||||
}
|
||||
|
||||
logLifecycle(`Loading ${serverUrl}`)
|
||||
if (shouldAbortWindowLoad()) {
|
||||
return
|
||||
}
|
||||
mainWindow.loadURL(serverUrl)
|
||||
}
|
||||
|
||||
@@ -175,7 +165,13 @@ const beginQuit = () => {
|
||||
stopServerProcess()
|
||||
}
|
||||
|
||||
app.whenReady().then(createWindow)
|
||||
app
|
||||
.whenReady()
|
||||
.then(createWindow)
|
||||
.catch((e) => {
|
||||
console.error('Failed to create window:', e)
|
||||
app.quit()
|
||||
})
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
if (process.platform !== 'darwin') {
|
||||
|
||||
Reference in New Issue
Block a user