diff --git a/src/common/cluster-store.ts b/src/common/cluster-store.ts index d1ccec355e..da8ab87a3a 100644 --- a/src/common/cluster-store.ts +++ b/src/common/cluster-store.ts @@ -11,7 +11,7 @@ import { appEventBus } from "./event-bus"; import { dumpConfigYaml } from "./kube-helpers"; import { saveToAppFiles } from "./utils/saveToAppFiles"; import { KubeConfig } from "@kubernetes/client-node"; -import { subscribeToBroadcast, unsubscribeAllFromBroadcast } from "./ipc"; +import { handleRequest, requestMain, subscribeToBroadcast, unsubscribeAllFromBroadcast } from "./ipc"; import _ from "lodash"; import move from "array-move"; import type { WorkspaceId } from "./workspace-store"; @@ -89,6 +89,8 @@ export class ClusterStore extends BaseStore { @observable removedClusters = observable.map(); @observable clusters = observable.map(); + private stateRequestChannel = "cluster:states"; + private constructor() { super({ configName: "lens-cluster-store", @@ -102,8 +104,40 @@ export class ClusterStore extends BaseStore { this.pushStateToViewsAutomatically(); } + async load() { + await super.load(); + type clusterStateSync = { + id: string; + state: ClusterState; + }; + if (ipcRenderer) { + logger.info("[CLUSTER-STORE] requesting initial state sync"); + const clusterStates: clusterStateSync[] = await requestMain(this.stateRequestChannel); + clusterStates.forEach((clusterState) => { + const cluster = this.getById(clusterState.id); + if (cluster) { + cluster.setState(clusterState.state); + } + }); + } else { + handleRequest(this.stateRequestChannel, (): clusterStateSync[] => { + const states: clusterStateSync[] = []; + this.clustersList.forEach((cluster) => { + states.push({ + state: cluster.getState(), + id: cluster.id + }); + }); + return states; + }); + } + } + protected pushStateToViewsAutomatically() { if (!ipcRenderer) { + reaction(() => this.enabledClustersList, () => { + this.pushState(); + }); reaction(() => this.connectedClustersList, () => { this.pushState(); });