diff --git a/packages/core/src/main/electron-app/runnables/setup-runnables-before-closing-of-application.injectable.ts b/packages/core/src/main/electron-app/runnables/setup-runnables-before-closing-of-application.injectable.ts index c0bc3ae620..d0dd124984 100644 --- a/packages/core/src/main/electron-app/runnables/setup-runnables-before-closing-of-application.injectable.ts +++ b/packages/core/src/main/electron-app/runnables/setup-runnables-before-closing-of-application.injectable.ts @@ -10,14 +10,16 @@ import electronAppInjectable from "../electron-app.injectable"; import isIntegrationTestingInjectable from "../../../common/vars/is-integration-testing.injectable"; import autoUpdaterInjectable from "../features/auto-updater.injectable"; import { runManySyncFor } from "../../../common/runnable/run-many-sync-for"; +import { runManyFor } from "../../../common/runnable/run-many-for"; const setupRunnablesBeforeClosingOfApplicationInjectable = getInjectable({ id: "setup-closing-of-application", instantiate: (di) => { const runManySync = runManySyncFor(di); + const runMany = runManyFor(di); const runRunnablesBeforeQuitOfFrontEnd = runManySync(beforeQuitOfFrontEndInjectionToken); - const runRunnablesBeforeQuitOfBackEnd = runManySync(beforeQuitOfBackEndInjectionToken); + const runRunnablesBeforeQuitOfBackEnd = runMany(beforeQuitOfBackEndInjectionToken); const app = di.inject(electronAppInjectable); const isIntegrationTesting = di.inject(isIntegrationTestingInjectable); const autoUpdater = di.inject(autoUpdaterInjectable); @@ -31,19 +33,41 @@ const setupRunnablesBeforeClosingOfApplicationInjectable = getInjectable({ isAutoUpdating = true; }); + let isAsyncQuitting = false; + + const doAsyncQuit = (event: Electron.Event, exitCode = 0) => { + if (isAsyncQuitting) { + return; + } + + isAsyncQuitting = true; + + void (async () => { + try { + await runRunnablesBeforeQuitOfBackEnd(); + } catch (error) { + console.error("A beforeQuitOfBackEnd failed!!!!", error); + exitCode = 1; + } + + app.exit(exitCode); + })(); + }; + app.on("will-quit", (event) => { runRunnablesBeforeQuitOfFrontEnd(); + event.preventDefault(); - const shouldQuitBackEnd = isIntegrationTesting || isAutoUpdating; - - if (shouldQuitBackEnd) { - runRunnablesBeforeQuitOfBackEnd(); - } else { - // IMPORTANT: This cannot be destructured as it would break binding of "this" for the Electron event - event.preventDefault(); + if (isIntegrationTesting || isAutoUpdating) { + doAsyncQuit(event); } }); + app.on("quit", (event, exitCode) => { + event.preventDefault(); + doAsyncQuit(event, exitCode); + }); + return undefined; }, }; diff --git a/packages/core/src/main/start-main-application/lens-window/application-window/create-lens-window.injectable.ts b/packages/core/src/main/start-main-application/lens-window/application-window/create-lens-window.injectable.ts index d2d8f344eb..e4c3f88538 100644 --- a/packages/core/src/main/start-main-application/lens-window/application-window/create-lens-window.injectable.ts +++ b/packages/core/src/main/start-main-application/lens-window/application-window/create-lens-window.injectable.ts @@ -4,7 +4,7 @@ */ import { getInjectable } from "@ogre-tools/injectable"; import type { ContentSource, ElectronWindowTitleBarStyle } from "./create-electron-window.injectable"; -import createElectronWindowForInjectable from "./create-electron-window.injectable"; +import createElectronWindowInjectable from "./create-electron-window.injectable"; import type { ClusterFrameInfo } from "../../../../common/cluster-frames"; export interface ElectronWindow { @@ -59,7 +59,7 @@ const createLensWindowInjectable = getInjectable({ id: "create-lens-window", instantiate: (di) => { - const createElectronWindow = di.inject(createElectronWindowForInjectable); + const createElectronWindow = di.inject(createElectronWindowInjectable); return (configuration: LensWindowConfiguration): LensWindow => { let browserWindow: ElectronWindow | undefined; diff --git a/packages/core/src/main/start-main-application/runnable-tokens/before-quit-of-back-end-injection-token.ts b/packages/core/src/main/start-main-application/runnable-tokens/before-quit-of-back-end-injection-token.ts index d11ecf57d2..f4b7d90a26 100644 --- a/packages/core/src/main/start-main-application/runnable-tokens/before-quit-of-back-end-injection-token.ts +++ b/packages/core/src/main/start-main-application/runnable-tokens/before-quit-of-back-end-injection-token.ts @@ -3,8 +3,8 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectionToken } from "@ogre-tools/injectable"; -import type { RunnableSync } from "../../../common/runnable/run-many-sync-for"; +import type { Runnable } from "../../../common/runnable/run-many-for"; -export const beforeQuitOfBackEndInjectionToken = getInjectionToken({ +export const beforeQuitOfBackEndInjectionToken = getInjectionToken({ id: "before-quit-of-back-end", }); diff --git a/packages/core/src/main/utils/resolve-system-proxy/resolve-system-proxy-window.injectable.ts b/packages/core/src/main/utils/resolve-system-proxy/resolve-system-proxy-window.injectable.ts index 88e4319fa0..c5dd1bb875 100644 --- a/packages/core/src/main/utils/resolve-system-proxy/resolve-system-proxy-window.injectable.ts +++ b/packages/core/src/main/utils/resolve-system-proxy/resolve-system-proxy-window.injectable.ts @@ -7,9 +7,9 @@ import { BrowserWindow } from "electron"; const resolveSystemProxyWindowInjectable = getInjectable({ id: "resolve-system-proxy-window", - instantiate: () => { - return new BrowserWindow({ show: false, paintWhenInitiallyHidden: false }); - }, + instantiate: () => new BrowserWindow({ + show: false, + }), causesSideEffects: true, });