forked from imbytecat/fullstack-starter
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 =>
|
const shouldAbortWindowLoad = (): boolean =>
|
||||||
isQuitting || !mainWindow || mainWindow.isDestroyed()
|
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()
|
||||||
@@ -99,7 +93,7 @@ const spawnServer = (port: number): string => {
|
|||||||
return `http://127.0.0.1:${port}`
|
return `http://127.0.0.1:${port}`
|
||||||
}
|
}
|
||||||
|
|
||||||
const getServerUrl = async (): Promise<string | null> => {
|
const resolveServerUrl = async (): Promise<string | null> => {
|
||||||
if (!app.isPackaged) {
|
if (!app.isPackaged) {
|
||||||
return DEV_SERVER_URL
|
return DEV_SERVER_URL
|
||||||
}
|
}
|
||||||
@@ -140,13 +134,13 @@ const createWindow = async () => {
|
|||||||
}
|
}
|
||||||
mainWindow.show()
|
mainWindow.show()
|
||||||
|
|
||||||
const serverUrl = await getServerUrl()
|
const serverUrl = await resolveServerUrl()
|
||||||
if (!serverUrl || shouldAbortWindowLoad()) {
|
if (!serverUrl || shouldAbortWindowLoad()) {
|
||||||
stopServerProcess()
|
stopServerProcess()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logLifecycle(`Waiting for server at ${serverUrl}...`)
|
if (!app.isPackaged) console.log(`Waiting for server at ${serverUrl}...`)
|
||||||
const ready = await waitForServer(serverUrl)
|
const ready = await waitForServer(serverUrl)
|
||||||
if (shouldAbortWindowLoad()) {
|
if (shouldAbortWindowLoad()) {
|
||||||
stopServerProcess()
|
stopServerProcess()
|
||||||
@@ -163,10 +157,6 @@ const createWindow = async () => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logLifecycle(`Loading ${serverUrl}`)
|
|
||||||
if (shouldAbortWindowLoad()) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
mainWindow.loadURL(serverUrl)
|
mainWindow.loadURL(serverUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,7 +165,13 @@ const beginQuit = () => {
|
|||||||
stopServerProcess()
|
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', () => {
|
app.on('window-all-closed', () => {
|
||||||
if (process.platform !== 'darwin') {
|
if (process.platform !== 'darwin') {
|
||||||
|
|||||||
Reference in New Issue
Block a user