From b3dab6b781ed0b0c1d1f3e2a7b5e6ecf921f1d61 Mon Sep 17 00:00:00 2001 From: Janne Savolainen Date: Thu, 28 Apr 2022 14:00:14 +0300 Subject: [PATCH] Fix application quit by calling Electron's event.preventDefault in a very specific way and preventing async Co-authored-by: Mikko Aspiala Signed-off-by: Janne Savolainen --- ...bles-before-closing-of-application.injectable.ts | 13 +++++++------ .../before-quit-of-back-end-injection-token.ts | 4 ++-- .../before-quit-of-front-end-injection-token.ts | 4 ++-- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/electron-app/before-application-is-ready/setup-runnables-before-closing-of-application.injectable.ts b/src/main/electron-app/before-application-is-ready/setup-runnables-before-closing-of-application.injectable.ts index 3b2707fc06..750b9ecfab 100644 --- a/src/main/electron-app/before-application-is-ready/setup-runnables-before-closing-of-application.injectable.ts +++ b/src/main/electron-app/before-application-is-ready/setup-runnables-before-closing-of-application.injectable.ts @@ -5,17 +5,17 @@ import { getInjectable } from "@ogre-tools/injectable"; import { beforeApplicationIsReadyInjectionToken } from "../../start-main-application/before-application-is-ready/before-application-is-ready-injection-token"; import { beforeQuitOfFrontEndInjectionToken } from "../../start-main-application/before-quit-of-front-end/before-quit-of-front-end-injection-token"; -import { runManyFor } from "../../start-main-application/run-many-for"; import { beforeQuitOfBackEndInjectionToken } from "../../start-main-application/before-quit-of-back-end/before-quit-of-back-end-injection-token"; 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 "../../start-main-application/run-many-sync-for"; const setupRunnablesBeforeClosingOfApplicationInjectable = getInjectable({ id: "setup-closing-of-application", instantiate: (di) => { - const runMany = runManyFor(di); + const runMany = runManySyncFor(di); const runRunnablesBeforeQuitOfFrontEnd = runMany( beforeQuitOfFrontEndInjectionToken, @@ -38,15 +38,16 @@ const setupRunnablesBeforeClosingOfApplicationInjectable = getInjectable({ isAutoUpdating = true; }); - app.on("will-quit", async ({ preventDefault: cancelNativeQuit }) => { - await runRunnablesBeforeQuitOfFrontEnd(); + app.on("will-quit", (event) => { + runRunnablesBeforeQuitOfFrontEnd(); const shouldQuitBackEnd = isIntegrationTesting || isAutoUpdating; if (shouldQuitBackEnd) { - await runRunnablesBeforeQuitOfBackEnd(); + runRunnablesBeforeQuitOfBackEnd(); } else { - cancelNativeQuit(); + // IMPORTANT: This cannot be destructured as it would break binding of "this" for the Electron event + event.preventDefault(); } }); }, diff --git a/src/main/start-main-application/before-quit-of-back-end/before-quit-of-back-end-injection-token.ts b/src/main/start-main-application/before-quit-of-back-end/before-quit-of-back-end-injection-token.ts index ed7922153e..a38e30add3 100644 --- a/src/main/start-main-application/before-quit-of-back-end/before-quit-of-back-end-injection-token.ts +++ b/src/main/start-main-application/before-quit-of-back-end/before-quit-of-back-end-injection-token.ts @@ -3,9 +3,9 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectionToken } from "@ogre-tools/injectable"; -import type { Runnable } from "../run-many-for"; +import type { RunnableSync } from "../run-many-sync-for"; export const beforeQuitOfBackEndInjectionToken = - getInjectionToken({ + getInjectionToken({ id: "before-quit-of-back-end", }); diff --git a/src/main/start-main-application/before-quit-of-front-end/before-quit-of-front-end-injection-token.ts b/src/main/start-main-application/before-quit-of-front-end/before-quit-of-front-end-injection-token.ts index 55cde355b4..f53ce2c8c2 100644 --- a/src/main/start-main-application/before-quit-of-front-end/before-quit-of-front-end-injection-token.ts +++ b/src/main/start-main-application/before-quit-of-front-end/before-quit-of-front-end-injection-token.ts @@ -3,9 +3,9 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectionToken } from "@ogre-tools/injectable"; -import type { Runnable } from "../run-many-for"; +import type { RunnableSync } from "../run-many-sync-for"; export const beforeQuitOfFrontEndInjectionToken = - getInjectionToken({ + getInjectionToken({ id: "before-quit-of-front-end", });