From 42081df811b5fa00fd59605f8eb6346a293f6bdc Mon Sep 17 00:00:00 2001 From: Sami Tiilikainen <97873007+samitiilikainen@users.noreply.github.com> Date: Mon, 6 Mar 2023 16:53:51 +0200 Subject: [PATCH] Remove pagehide event listener as it may cause UI to freeze Pagehide was needed in cluster frame to better handle main frame close/reload situation. But even empty pagehide listener in cluster frame seems to freeze the UI at least on some situations (multiple clusters open). Beforeunload is not always executed in cluster frame when main frame is reloaded/closed, leaving log files open. To fix that, `stopIpcLoggingInjectable` is introduced to close all log files. Signed-off-by: Sami Tiilikainen <97873007+samitiilikainen@users.noreply.github.com> --- .../logger/stop-ipc-logging.injectable.ts | 28 ++++++++++++ .../init-cluster-frame/init-cluster-frame.ts | 45 +++++++++---------- 2 files changed, 48 insertions(+), 25 deletions(-) create mode 100644 packages/core/src/main/logger/stop-ipc-logging.injectable.ts diff --git a/packages/core/src/main/logger/stop-ipc-logging.injectable.ts b/packages/core/src/main/logger/stop-ipc-logging.injectable.ts new file mode 100644 index 0000000000..2f6206c8b8 --- /dev/null +++ b/packages/core/src/main/logger/stop-ipc-logging.injectable.ts @@ -0,0 +1,28 @@ +/** + * 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 { beforeQuitOfFrontEndInjectionToken } from "../start-main-application/runnable-tokens/before-quit-of-front-end-injection-token"; +import ipcFileLoggerInjectable from "./ipc-file-logger.injectable"; + +const stopIpcLoggingInjectable = getInjectable({ + id: "stop-ipc-logging", + + instantiate: (di) => { + const ipcFileLogger = di.inject(ipcFileLoggerInjectable); + + return { + id: "stop-ipc-logging", + run: () => { + ipcFileLogger.closeAll(); + + return undefined; + }, + }; + }, + + injectionToken: beforeQuitOfFrontEndInjectionToken, +}); + +export default stopIpcLoggingInjectable; diff --git a/packages/core/src/renderer/frames/cluster-frame/init-cluster-frame/init-cluster-frame.ts b/packages/core/src/renderer/frames/cluster-frame/init-cluster-frame/init-cluster-frame.ts index 38b92983a0..73e4222132 100644 --- a/packages/core/src/renderer/frames/cluster-frame/init-cluster-frame/init-cluster-frame.ts +++ b/packages/core/src/renderer/frames/cluster-frame/init-cluster-frame/init-cluster-frame.ts @@ -2,7 +2,6 @@ * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ -import { once } from "lodash"; import type { Cluster } from "../../../../common/cluster/cluster"; import type { CatalogEntityRegistry } from "../../../api/catalog/entity/registry"; import type { ShowNotification } from "../../../components/notifications"; @@ -24,22 +23,23 @@ interface Dependencies { const logPrefix = "[CLUSTER-FRAME]:"; -export const initClusterFrame = ({ - hostedCluster, - loadExtensions, - catalogEntityRegistry, - frameRoutingId, - emitAppEvent, - logger, - showErrorNotification, - closeFileLogging, -}: Dependencies) => +export const initClusterFrame = + ({ + hostedCluster, + loadExtensions, + catalogEntityRegistry, + frameRoutingId, + emitAppEvent, + logger, + showErrorNotification, + closeFileLogging, + }: Dependencies) => async (unmountRoot: () => void) => { // TODO: Make catalogEntityRegistry already initialized when passed as dependency catalogEntityRegistry.init(); logger.info( - `${logPrefix} Init dashboard, clusterId=${hostedCluster.id}, frameId=${frameRoutingId}`, + `${logPrefix} Init dashboard, clusterId=${hostedCluster.id}, frameId=${frameRoutingId}` ); await requestSetClusterFrameId(hostedCluster.id); @@ -51,19 +51,17 @@ export const initClusterFrame = ({ // Note that the Catalog might still have unprocessed entities until the extensions are fully loaded. when( () => catalogEntityRegistry.items.get().length > 0, - () => - loadExtensions(), + () => loadExtensions(), { timeout: 15_000, onError: (error) => { - logger.warn( - "[CLUSTER-FRAME]: error from activeEntity when()", - error, - ); + logger.warn("[CLUSTER-FRAME]: error from activeEntity when()", error); - showErrorNotification("Failed to get KubernetesCluster for this view. Extensions will not be loaded."); + showErrorNotification( + "Failed to get KubernetesCluster for this view. Extensions will not be loaded." + ); }, - }, + } ); setTimeout(() => { @@ -76,15 +74,12 @@ export const initClusterFrame = ({ }); }); - const onCloseFrame = once(() => { + window.addEventListener("beforeunload", () => { logger.info( - `${logPrefix} Unload dashboard, clusterId=${(hostedCluster.id)}, frameId=${frameRoutingId}`, + `${logPrefix} Unload dashboard, clusterId=${hostedCluster.id}, frameId=${frameRoutingId}` ); unmountRoot(); closeFileLogging(); }); - - window.addEventListener("beforeunload", onCloseFrame); - window.addEventListener("pagehide", onCloseFrame); };