1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Move initClusterFrame around

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2023-02-17 09:23:40 -05:00
parent ac6e3e18e2
commit 1058113e90
6 changed files with 82 additions and 121 deletions

View File

@ -3,7 +3,7 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import webFrameInjectable from "./web-frame/web-frame.injectable";
import webFrameInjectable from "./web-frame.injectable";
const frameRoutingIdInjectable = getInjectable({
id: "frame-routing-id",

View File

@ -12,7 +12,7 @@ import { DiContextProvider } from "@ogre-tools/injectable-react";
import type { DiContainer } from "@ogre-tools/injectable";
import extensionInstallationStateStoreInjectable from "../extensions/extension-installation-state-store/extension-installation-state-store.injectable";
import initRootFrameInjectable from "./frames/root-frame/init-root-frame.injectable";
import initClusterFrameInjectable from "./frames/cluster-frame/init-cluster-frame/init-cluster-frame.injectable";
import initClusterFrameInjectable from "./frames/cluster-frame/init-cluster-frame.injectable";
import { Router } from "react-router";
import historyInjectable from "./navigation/history.injectable";
import assert from "assert";

View File

@ -0,0 +1,80 @@
/**
* 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 catalogEntityRegistryInjectable from "../../api/catalog/entity/registry.injectable";
import frameRoutingIdInjectable from "../../../features/electron/renderer/frame-routing-id.injectable";
import hostedClusterInjectable from "../../cluster-frame-context/hosted-cluster.injectable";
import assert from "assert";
import emitAppEventInjectable from "../../../common/app-event-bus/emit-event.injectable";
import showErrorNotificationInjectable from "../../components/notifications/show-error-notification.injectable";
import autoInitExtensionsInjectable from "../../../features/extensions/loader/common/auto-init-extensions.injectable";
import prefixedLoggerInjectable from "../../../common/logger/prefixed-logger.injectable";
import { when } from "mobx";
import { requestSetClusterFrameId } from "../../ipc";
const initClusterFrameInjectable = getInjectable({
id: "init-cluster-frame",
instantiate: (di) => {
const hostedCluster = di.inject(hostedClusterInjectable);
assert(hostedCluster, "This can only be injected within a cluster frame");
const loadExtensions = di.inject(autoInitExtensionsInjectable);
const catalogEntityRegistry = di.inject(catalogEntityRegistryInjectable);
const frameRoutingId = di.inject(frameRoutingIdInjectable);
const emitAppEvent = di.inject(emitAppEventInjectable);
const logger = di.inject(prefixedLoggerInjectable, "CLUSTER-FRAME");
const showErrorNotification = di.inject(showErrorNotificationInjectable);
return async (unmountRoot: () => void) => {
// TODO: Make catalogEntityRegistry already initialized when passed as dependency
catalogEntityRegistry.init();
logger.info(`Init dashboard, clusterId=${hostedCluster.id}, frameId=${frameRoutingId}`);
await requestSetClusterFrameId(hostedCluster.id);
await hostedCluster.whenReady; // cluster.activate() is done at this point
catalogEntityRegistry.activeEntity = hostedCluster.id;
// Only load the extensions once the catalog has been populated.
// Note that the Catalog might still have unprocessed entities until the extensions are fully loaded.
when(
() => catalogEntityRegistry.items.get().length > 0,
() =>
loadExtensions(),
{
timeout: 15_000,
onError: (error) => {
logger.warn(
"[CLUSTER-FRAME]: error from activeEntity when()",
error,
);
showErrorNotification("Failed to get KubernetesCluster for this view. Extensions will not be loaded.");
},
},
);
setTimeout(() => {
emitAppEvent({
name: "cluster",
action: "open",
params: {
clusterId: hostedCluster.id,
},
});
});
window.addEventListener("beforeunload", () => {
logger.info(`Unload dashboard, clusterId=${(hostedCluster.id)}, frameId=${frameRoutingId}`);
unmountRoot();
});
};
},
});
export default initClusterFrameInjectable;

View File

@ -1,36 +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 { initClusterFrame } from "./init-cluster-frame";
import catalogEntityRegistryInjectable from "../../../api/catalog/entity/registry.injectable";
import frameRoutingIdInjectable from "./frame-routing-id/frame-routing-id.injectable";
import hostedClusterInjectable from "../../../cluster-frame-context/hosted-cluster.injectable";
import assert from "assert";
import emitAppEventInjectable from "../../../../common/app-event-bus/emit-event.injectable";
import loggerInjectable from "../../../../common/logger.injectable";
import showErrorNotificationInjectable from "../../../components/notifications/show-error-notification.injectable";
import autoInitExtensionsInjectable from "../../../../features/extensions/loader/common/auto-init-extensions.injectable";
const initClusterFrameInjectable = getInjectable({
id: "init-cluster-frame",
instantiate: (di) => {
const hostedCluster = di.inject(hostedClusterInjectable);
assert(hostedCluster, "This can only be injected within a cluster frame");
return initClusterFrame({
hostedCluster,
loadExtensions: di.inject(autoInitExtensionsInjectable),
catalogEntityRegistry: di.inject(catalogEntityRegistryInjectable),
frameRoutingId: di.inject(frameRoutingIdInjectable),
emitAppEvent: di.inject(emitAppEventInjectable),
logger: di.inject(loggerInjectable),
showErrorNotification: di.inject(showErrorNotificationInjectable),
});
},
});
export default initClusterFrameInjectable;

View File

@ -1,83 +0,0 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { Cluster } from "../../../../common/cluster/cluster";
import type { CatalogEntityRegistry } from "../../../api/catalog/entity/registry";
import type { ShowNotification } from "../../../components/notifications";
import { when } from "mobx";
import { requestSetClusterFrameId } from "../../../ipc";
import type { EmitAppEvent } from "../../../../common/app-event-bus/emit-event.injectable";
import type { Logger } from "../../../../common/logger";
interface Dependencies {
hostedCluster: Cluster;
loadExtensions: () => void;
catalogEntityRegistry: CatalogEntityRegistry;
frameRoutingId: number;
emitAppEvent: EmitAppEvent;
logger: Logger;
showErrorNotification: ShowNotification;
}
const logPrefix = "[CLUSTER-FRAME]:";
export const initClusterFrame = ({
hostedCluster,
loadExtensions,
catalogEntityRegistry,
frameRoutingId,
emitAppEvent,
logger,
showErrorNotification,
}: 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}`,
);
await requestSetClusterFrameId(hostedCluster.id);
await hostedCluster.whenReady; // cluster.activate() is done at this point
catalogEntityRegistry.activeEntity = hostedCluster.id;
// Only load the extensions once the catalog has been populated.
// Note that the Catalog might still have unprocessed entities until the extensions are fully loaded.
when(
() => catalogEntityRegistry.items.get().length > 0,
() =>
loadExtensions(),
{
timeout: 15_000,
onError: (error) => {
logger.warn(
"[CLUSTER-FRAME]: error from activeEntity when()",
error,
);
showErrorNotification("Failed to get KubernetesCluster for this view. Extensions will not be loaded.");
},
},
);
setTimeout(() => {
emitAppEvent({
name: "cluster",
action: "open",
params: {
clusterId: hostedCluster.id,
},
});
});
window.onbeforeunload = () => {
logger.info(
`${logPrefix} Unload dashboard, clusterId=${(hostedCluster.id)}, frameId=${frameRoutingId}`,
);
unmountRoot();
};
};