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() })