1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/main/electron-app/before-application-is-ready/setup-runnables-before-closing-of-application.injectable.ts
Janne Savolainen b3dab6b781
Fix application quit by calling Electron's event.preventDefault in a very specific way and preventing async
Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
2022-05-11 08:25:36 +03:00

61 lines
2.3 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
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 { 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 = runManySyncFor(di);
const runRunnablesBeforeQuitOfFrontEnd = runMany(
beforeQuitOfFrontEndInjectionToken,
);
const runRunnablesBeforeQuitOfBackEnd = runMany(
beforeQuitOfBackEndInjectionToken,
);
return {
run: () => {
const app = di.inject(electronAppInjectable);
const isIntegrationTesting = di.inject(isIntegrationTestingInjectable);
const autoUpdater = di.inject(autoUpdaterInjectable);
let isAutoUpdating = false;
autoUpdater.on("before-quit-for-update", () => {
isAutoUpdating = true;
});
app.on("will-quit", (event) => {
runRunnablesBeforeQuitOfFrontEnd();
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();
}
});
},
};
},
injectionToken: beforeApplicationIsReadyInjectionToken,
});
export default setupRunnablesBeforeClosingOfApplicationInjectable;