From 43e361727f54d1c97471229c4a9502c52345712d Mon Sep 17 00:00:00 2001 From: Jari Kolehmainen Date: Thu, 19 Nov 2020 09:12:19 +0200 Subject: [PATCH] refactor/remove Cluster#frameId Signed-off-by: Jari Kolehmainen --- src/common/cluster-ipc.ts | 12 +++++++++--- src/common/ipc.ts | 10 ++++------ src/main/cluster.ts | 1 - src/main/window-manager.ts | 4 ++-- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/common/cluster-ipc.ts b/src/common/cluster-ipc.ts index 15f4fef584..d7ff78fd37 100644 --- a/src/common/cluster-ipc.ts +++ b/src/common/cluster-ipc.ts @@ -3,12 +3,14 @@ import { ClusterId, clusterStore } from "./cluster-store"; import { appEventBus } from "./event-bus" import { ResourceApplier } from "../main/resource-applier"; import { ipcMain } from "electron"; +import { observable } from "mobx"; export const clusterActivateHandler = "cluster:activate" export const clusterSetFrameIdHandler = "cluster:set-frame-id" export const clusterRefreshHandler = "cluster:refresh" export const clusterDisconnectHandler = "cluster:disconnect" export const clusterKubectlApplyAllHandler = "cluster:kubectl-apply-all" +export const clusterFrameMap = observable.map(); if (ipcMain) { handleRequest(clusterActivateHandler, (event, clusterId: ClusterId, force = false) => { @@ -18,10 +20,10 @@ if (ipcMain) { } }) - handleRequest(clusterSetFrameIdHandler, (event, clusterId: ClusterId, frameId?: number) => { + handleRequest(clusterSetFrameIdHandler, (event, clusterId: ClusterId, frameId: number) => { const cluster = clusterStore.getById(clusterId); if (cluster) { - if (frameId) cluster.frameId = frameId; // save cluster's webFrame.routingId to be able to send push-updates + clusterFrameMap.set(cluster.id, frameId) return cluster.pushState(); } }) @@ -33,7 +35,11 @@ if (ipcMain) { handleRequest(clusterDisconnectHandler, (event, clusterId: ClusterId) => { appEventBus.emit({name: "cluster", action: "stop"}); - return clusterStore.getById(clusterId)?.disconnect(); + const cluster = clusterStore.getById(clusterId); + if (cluster) { + cluster.disconnect(); + clusterFrameMap.delete(cluster.id) + } }) handleRequest(clusterKubectlApplyAllHandler, (event, clusterId: ClusterId, resources: string[]) => { diff --git a/src/common/ipc.ts b/src/common/ipc.ts index 6e2a76c5ed..7878882343 100644 --- a/src/common/ipc.ts +++ b/src/common/ipc.ts @@ -2,8 +2,9 @@ // https://www.electronjs.org/docs/api/ipc-main // https://www.electronjs.org/docs/api/ipc-renderer -import { ipcMain, ipcRenderer, webContents, remote } from "electron" +import { ipcMain, ipcRenderer, webContents, remote } from "electron"; import logger from "../main/logger"; +import { clusterFrameMap } from "./cluster-ipc"; export function handleRequest(channel: string, listener: (...args: any[]) => any) { ipcMain.handle(channel, listener) @@ -15,11 +16,8 @@ export async function requestMain(channel: string, ...args: any[]) { async function getSubFrames(): Promise { const subFrames: number[] = []; - const { clusterStore } = await import("./cluster-store"); - clusterStore.clustersList.forEach(cluster => { - if (cluster.frameId) { - subFrames.push(cluster.frameId) - } + clusterFrameMap.forEach((frameId, _) => { + subFrames.push(frameId) }); return subFrames; } diff --git a/src/main/cluster.ts b/src/main/cluster.ts index 28dce65f0b..ecb6e6db9c 100644 --- a/src/main/cluster.ts +++ b/src/main/cluster.ts @@ -50,7 +50,6 @@ export interface ClusterState { export class Cluster implements ClusterModel, ClusterState { public id: ClusterId; - public frameId: number; public kubeCtl: Kubectl public contextHandler: ContextHandler; public ownerRef: string; diff --git a/src/main/window-manager.ts b/src/main/window-manager.ts index 33a943b552..5a536e6946 100644 --- a/src/main/window-manager.ts +++ b/src/main/window-manager.ts @@ -1,5 +1,4 @@ import type { ClusterId } from "../common/cluster-store"; -import { clusterStore } from "../common/cluster-store"; import { observable } from "mobx"; import { app, BrowserWindow, dialog, shell, webContents } from "electron" import windowStateKeeper from "electron-window-state" @@ -8,6 +7,7 @@ import { subscribeToBroadcast } from "../common/ipc" import { initMenu } from "./menu"; import { initTray } from "./tray"; import { Singleton } from "../common/utils"; +import { clusterFrameMap } from "../common/cluster-ipc"; export class WindowManager extends Singleton { protected mainWindow: BrowserWindow; @@ -130,7 +130,7 @@ export class WindowManager extends Singleton { } reload() { - const frameId = clusterStore.getById(this.activeClusterId)?.frameId; + const frameId = clusterFrameMap.get(this.activeClusterId) if (frameId) { this.sendToView({ channel: "renderer:reload", frameId }); } else {