chore: Split setupRunnablesBeforeClosingOfApplication

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton committed 2023-04-12 15:15:36 -04:00
1 parent 802cbdb096
commit c0eb4c6dea
4 files changed
+80 -74

No files matched your search

@@ -0,0 +1,37 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { beforeElectronIsReadyInjectionToken } from "@k8slens/application-for-electron-main";
import { runManySyncFor } from "@k8slens/run-many";
import { getInjectable } from "@ogre-tools/injectable";
import isIntegrationTestingInjectable from "../../../common/vars/is-integration-testing.injectable";
import { afterQuitOfFrontEndInjectionToken } from "../../start-main-application/runnable-tokens/phases";
import electronAppInjectable from "../electron-app.injectable";
import isAutoUpdatingInjectable from "../features/is-auto-updating.injectable";
const setupBehaviourOnCloseOfLastWindowInjectable = getInjectable({
id: "setup-behaviour-on-close-of-last-window",
instantiate: (di) => ({
run: () => {
const runManySync = runManySyncFor(di);
const runAfterQuitOfFrontEnd = runManySync(afterQuitOfFrontEndInjectionToken);
const app = di.inject(electronAppInjectable);
const isIntegrationTesting = di.inject(isIntegrationTestingInjectable);
const isAutoUpdating = di.inject(isAutoUpdatingInjectable);
app.on("window-all-closed", () => {
runAfterQuitOfFrontEnd();
if (isIntegrationTesting || isAutoUpdating.get()) {
app.quit();
}
});
return undefined;
},
}),
injectionToken: beforeElectronIsReadyInjectionToken,
});
export default setupBehaviourOnCloseOfLastWindowInjectable;
@@ -0,0 +1,43 @@
/**
* 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 { beforeElectronIsReadyInjectionToken } from "@k8slens/application-for-electron-main";
import { onQuitOfBackEndInjectionToken } from "../../start-main-application/runnable-tokens/phases";
import electronAppInjectable from "../electron-app.injectable";
import { runManyFor } from "@k8slens/run-many";
import { once } from "lodash";
const setupCleanupOfBackendOnQuitInjectable = getInjectable({
id: "setup-cleanup-of-backend-on-quit",
instantiate: (di) => ({
run: () => {
const runMany = runManyFor(di);
const runOnQuitOfBackEnd = runMany(onQuitOfBackEndInjectionToken);
const app = di.inject(electronAppInjectable);
const doAsyncQuit = once(() => void (async () => {
try {
await runOnQuitOfBackEnd();
app.exit(0);
} catch (error) {
console.error("A beforeQuitOfBackEnd failed!!!!", error);
app.exit(1);
}
})());
app.on("will-quit", (event) => {
event.preventDefault();
doAsyncQuit();
});
return undefined;
},
}),
injectionToken: beforeElectronIsReadyInjectionToken,
});
export default setupCleanupOfBackendOnQuitInjectable;
@@ -1,72 +0,0 @@
/**
* 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 { beforeElectronIsReadyInjectionToken } from "@k8slens/application-for-electron-main";
import { afterQuitOfFrontEndInjectionToken, onQuitOfBackEndInjectionToken } from "../../start-main-application/runnable-tokens/phases";
import electronAppInjectable from "../electron-app.injectable";
import isIntegrationTestingInjectable from "../../../common/vars/is-integration-testing.injectable";
import { runManySyncFor, runManyFor } from "@k8slens/run-many";
import isAutoUpdatingInjectable from "../features/is-auto-updating.injectable";
const setupRunnablesBeforeClosingOfApplicationInjectable = getInjectable({
id: "setup-closing-of-application",
instantiate: (di) => ({
run: () => {
const runManySync = runManySyncFor(di);
const runMany = runManyFor(di);
const runAfterQuitOfFrontEnd = runManySync(afterQuitOfFrontEndInjectionToken);
const runOnQuitOfBackEnd = runMany(onQuitOfBackEndInjectionToken);
const app = di.inject(electronAppInjectable);
const isIntegrationTesting = di.inject(isIntegrationTestingInjectable);
const isAutoUpdating = di.inject(isAutoUpdatingInjectable);
app.on("will-quit", () => {
runAfterQuitOfFrontEnd();
let isAsyncQuitting = false;
const doAsyncQuit = (event: Electron.Event, exitCode = 0) => {
if (isAsyncQuitting) {
return;
}
isAsyncQuitting = true;
void (async () => {
try {
await runOnQuitOfBackEnd();
} catch (error) {
console.error("A beforeQuitOfBackEnd failed!!!!", error);
exitCode = 1;
}
app.exit(exitCode);
})();
};
app.on("will-quit", (event) => {
runAfterQuitOfFrontEnd();
event.preventDefault();
if (isIntegrationTesting || isAutoUpdating.get()) {
doAsyncQuit(event);
}
});
app.on("quit", (event, exitCode) => {
event.preventDefault();
doAsyncQuit(event, exitCode);
});
});
return undefined;
},
}),
injectionToken: beforeElectronIsReadyInjectionToken,
});
export default setupRunnablesBeforeClosingOfApplicationInjectable;