mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Extract beforeunload listener to injectable
Signed-off-by: Sami Tiilikainen <97873007+samitiilikainen@users.noreply.github.com>
This commit is contained in:
parent
91a40c6b62
commit
39057ca5c4
@ -0,0 +1,46 @@
|
|||||||
|
/**
|
||||||
|
* 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 currentlyInClusterFrameInjectable from "../../routes/currently-in-cluster-frame.injectable";
|
||||||
|
import { beforeFrameStartsSecondInjectionToken } from "../tokens";
|
||||||
|
import loggerInjectable from "../../../common/logger.injectable";
|
||||||
|
import hostedClusterInjectable from "../../cluster-frame-context/hosted-cluster.injectable";
|
||||||
|
import frameRoutingIdInjectable from "../../frames/cluster-frame/init-cluster-frame/frame-routing-id/frame-routing-id.injectable";
|
||||||
|
import closeRendererLogFileInjectable from "../../logger/close-renderer-log-file.injectable";
|
||||||
|
import { unmountComponentAtNode } from "react-dom";
|
||||||
|
|
||||||
|
const listenUnloadInjectable = getInjectable({
|
||||||
|
id: "listen-unload",
|
||||||
|
instantiate: (di) => ({
|
||||||
|
run: () => {
|
||||||
|
const closeRendererLogFile = di.inject(closeRendererLogFileInjectable);
|
||||||
|
const isClusterFrame = di.inject(currentlyInClusterFrameInjectable);
|
||||||
|
const logger = di.inject(loggerInjectable);
|
||||||
|
|
||||||
|
window.addEventListener("beforeunload", () => {
|
||||||
|
if (isClusterFrame) {
|
||||||
|
const hostedCluster = di.inject(hostedClusterInjectable);
|
||||||
|
const frameRoutingId = di.inject(frameRoutingIdInjectable);
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
`[CLUSTER-FRAME] Unload dashboard, clusterId=${hostedCluster?.id}, frameId=${frameRoutingId}`,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
logger.info("[ROOT-FRAME]: Unload app");
|
||||||
|
}
|
||||||
|
|
||||||
|
closeRendererLogFile();
|
||||||
|
const rootElem = document.getElementById("app");
|
||||||
|
|
||||||
|
if (rootElem) {
|
||||||
|
unmountComponentAtNode(rootElem);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
injectionToken: beforeFrameStartsSecondInjectionToken,
|
||||||
|
});
|
||||||
|
|
||||||
|
export default listenUnloadInjectable;
|
||||||
@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
import "./components/app.scss";
|
import "./components/app.scss";
|
||||||
|
|
||||||
import { unmountComponentAtNode } from "react-dom";
|
|
||||||
import type {
|
import type {
|
||||||
DiContainerForInjection,
|
DiContainerForInjection,
|
||||||
} from "@ogre-tools/injectable";
|
} from "@ogre-tools/injectable";
|
||||||
@ -35,7 +34,7 @@ export async function bootstrap(di: DiContainerForInjection) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await initializeApp(() => unmountComponentAtNode(rootElem));
|
await initializeApp();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`[BOOTSTRAP]: view initialization error: ${error}`, {
|
console.error(`[BOOTSTRAP]: view initialization error: ${error}`, {
|
||||||
origin: location.href,
|
origin: location.href,
|
||||||
|
|||||||
@ -12,7 +12,6 @@ import emitAppEventInjectable from "../../../../common/app-event-bus/emit-event.
|
|||||||
import loadExtensionsInjectable from "../../load-extensions.injectable";
|
import loadExtensionsInjectable from "../../load-extensions.injectable";
|
||||||
import loggerInjectable from "../../../../common/logger.injectable";
|
import loggerInjectable from "../../../../common/logger.injectable";
|
||||||
import showErrorNotificationInjectable from "../../../components/notifications/show-error-notification.injectable";
|
import showErrorNotificationInjectable from "../../../components/notifications/show-error-notification.injectable";
|
||||||
import closeRendererLogFileInjectable from "../../../logger/close-renderer-log-file.injectable";
|
|
||||||
|
|
||||||
const initClusterFrameInjectable = getInjectable({
|
const initClusterFrameInjectable = getInjectable({
|
||||||
id: "init-cluster-frame",
|
id: "init-cluster-frame",
|
||||||
@ -30,7 +29,6 @@ const initClusterFrameInjectable = getInjectable({
|
|||||||
emitAppEvent: di.inject(emitAppEventInjectable),
|
emitAppEvent: di.inject(emitAppEventInjectable),
|
||||||
logger: di.inject(loggerInjectable),
|
logger: di.inject(loggerInjectable),
|
||||||
showErrorNotification: di.inject(showErrorNotificationInjectable),
|
showErrorNotification: di.inject(showErrorNotificationInjectable),
|
||||||
closeFileLogging: di.inject(closeRendererLogFileInjectable),
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@ -18,7 +18,6 @@ interface Dependencies {
|
|||||||
emitAppEvent: EmitAppEvent;
|
emitAppEvent: EmitAppEvent;
|
||||||
logger: Logger;
|
logger: Logger;
|
||||||
showErrorNotification: ShowNotification;
|
showErrorNotification: ShowNotification;
|
||||||
closeFileLogging: () => void;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const logPrefix = "[CLUSTER-FRAME]:";
|
const logPrefix = "[CLUSTER-FRAME]:";
|
||||||
@ -32,9 +31,8 @@ export const initClusterFrame =
|
|||||||
emitAppEvent,
|
emitAppEvent,
|
||||||
logger,
|
logger,
|
||||||
showErrorNotification,
|
showErrorNotification,
|
||||||
closeFileLogging,
|
|
||||||
}: Dependencies) =>
|
}: Dependencies) =>
|
||||||
async (unmountRoot: () => void) => {
|
async () => {
|
||||||
// TODO: Make catalogEntityRegistry already initialized when passed as dependency
|
// TODO: Make catalogEntityRegistry already initialized when passed as dependency
|
||||||
catalogEntityRegistry.init();
|
catalogEntityRegistry.init();
|
||||||
|
|
||||||
@ -71,13 +69,4 @@ export const initClusterFrame =
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
window.onbeforeunload = () => {
|
|
||||||
logger.info(
|
|
||||||
`${logPrefix} Unload dashboard, clusterId=${hostedCluster.id}, frameId=${frameRoutingId}`,
|
|
||||||
);
|
|
||||||
|
|
||||||
unmountRoot();
|
|
||||||
closeFileLogging();
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -9,11 +9,9 @@ import lensProtocolRouterRendererInjectable from "../../protocol-handler/lens-pr
|
|||||||
import catalogEntityRegistryInjectable from "../../api/catalog/entity/registry.injectable";
|
import catalogEntityRegistryInjectable from "../../api/catalog/entity/registry.injectable";
|
||||||
import registerIpcListenersInjectable from "../../ipc/register-ipc-listeners.injectable";
|
import registerIpcListenersInjectable from "../../ipc/register-ipc-listeners.injectable";
|
||||||
import loadExtensionsInjectable from "../load-extensions.injectable";
|
import loadExtensionsInjectable from "../load-extensions.injectable";
|
||||||
import loggerInjectable from "../../../common/logger.injectable";
|
|
||||||
import { delay } from "@k8slens/utilities";
|
import { delay } from "@k8slens/utilities";
|
||||||
import { broadcastMessage } from "../../../common/ipc";
|
import { broadcastMessage } from "../../../common/ipc";
|
||||||
import { bundledExtensionsLoaded } from "../../../common/ipc/extension-handling";
|
import { bundledExtensionsLoaded } from "../../../common/ipc/extension-handling";
|
||||||
import closeRendererLogFileInjectable from "../../logger/close-renderer-log-file.injectable";
|
|
||||||
|
|
||||||
const initRootFrameInjectable = getInjectable({
|
const initRootFrameInjectable = getInjectable({
|
||||||
id: "init-root-frame",
|
id: "init-root-frame",
|
||||||
@ -24,10 +22,8 @@ const initRootFrameInjectable = getInjectable({
|
|||||||
const bindProtocolAddRouteHandlers = di.inject(bindProtocolAddRouteHandlersInjectable);
|
const bindProtocolAddRouteHandlers = di.inject(bindProtocolAddRouteHandlersInjectable);
|
||||||
const lensProtocolRouterRenderer = di.inject(lensProtocolRouterRendererInjectable);
|
const lensProtocolRouterRenderer = di.inject(lensProtocolRouterRendererInjectable);
|
||||||
const catalogEntityRegistry = di.inject(catalogEntityRegistryInjectable);
|
const catalogEntityRegistry = di.inject(catalogEntityRegistryInjectable);
|
||||||
const logger = di.inject(loggerInjectable);
|
|
||||||
const closeRendererLogFile = di.inject(closeRendererLogFileInjectable);
|
|
||||||
|
|
||||||
return async (unmountRoot: () => void) => {
|
return async () => {
|
||||||
catalogEntityRegistry.init();
|
catalogEntityRegistry.init();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -58,13 +54,6 @@ const initRootFrameInjectable = getInjectable({
|
|||||||
window.addEventListener("online", () => broadcastMessage("network:online"));
|
window.addEventListener("online", () => broadcastMessage("network:online"));
|
||||||
|
|
||||||
registerIpcListeners();
|
registerIpcListeners();
|
||||||
|
|
||||||
window.addEventListener("beforeunload", () => {
|
|
||||||
logger.info("[ROOT-FRAME]: Unload app");
|
|
||||||
|
|
||||||
closeRendererLogFile();
|
|
||||||
unmountRoot();
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user