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

use finally for cleanup

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-03-24 15:51:57 -04:00
parent 5a36494bc3
commit 30e1c83afa

View File

@ -3,6 +3,7 @@ import { ClusterId, ClusterStore, getClusterFrameUrl } from "../../../common/clu
import { getMatchedClusterId } from "../../navigation"; import { getMatchedClusterId } from "../../navigation";
import logger from "../../../main/logger"; import logger from "../../../main/logger";
import AwaitLock from "await-lock"; import AwaitLock from "await-lock";
import { Cluster } from "../../../main/cluster";
export interface LensView { export interface LensView {
isLoaded?: boolean isLoaded?: boolean
@ -20,27 +21,42 @@ export function hasLoadedView(clusterId: ClusterId): boolean {
export async function initView(clusterId: ClusterId) { export async function initView(clusterId: ClusterId) {
await lensViewsLock.acquireAsync(); await lensViewsLock.acquireAsync();
if (!clusterId || lensViews.has(clusterId)) { try {
return; const cluster = ClusterStore.getInstance().getById(clusterId);
}
const cluster = ClusterStore.getInstance().getById(clusterId);
logger.info(`[LENS-VIEW]: init dashboard, clusterId=${clusterId}`); if (!cluster || lensViews.has(clusterId)) {
const parentElem = document.getElementById("lens-views"); return;
}
logger.info(`[LENS-VIEW]: init dashboard, clusterId=${clusterId}`);
const iframe = createFrameFor(cluster);
document
.getElementById("lens-views")
.appendChild(iframe);
logger.info(`[LENS-VIEW]: waiting cluster to be ready, clusterId=${clusterId}`);
cluster.whenReady
.then(() => autoCleanOnRemove(clusterId, iframe));
} finally {
lensViewsLock.release();
}
}
function createFrameFor(cluster: Cluster): HTMLIFrameElement {
const iframe = document.createElement("iframe"); const iframe = document.createElement("iframe");
iframe.name = cluster.contextName; iframe.name = cluster.contextName;
iframe.setAttribute("src", getClusterFrameUrl(clusterId)); iframe.setAttribute("src", getClusterFrameUrl(cluster.id));
iframe.addEventListener("load", () => { iframe.addEventListener("load", () => {
logger.info(`[LENS-VIEW]: loaded from ${iframe.src}`); logger.info(`[LENS-VIEW]: loaded from ${iframe.src}`);
lensViews.get(clusterId).isLoaded = true; lensViews.get(cluster.id).isLoaded = true;
}, { once: true }); }, {
lensViews.set(clusterId, { clusterId, view: iframe }); once: true
parentElem.appendChild(iframe); });
logger.info(`[LENS-VIEW]: waiting cluster to be ready, clusterId=${clusterId}`); lensViews.set(cluster.id, { clusterId: cluster.id, view: iframe });
await cluster.whenReady;
lensViewsLock.release(); return iframe;
await autoCleanOnRemove(clusterId, iframe);
} }
export async function autoCleanOnRemove(clusterId: ClusterId, iframe: HTMLIFrameElement) { export async function autoCleanOnRemove(clusterId: ClusterId, iframe: HTMLIFrameElement) {