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

Fix cluster state sync

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-12-09 11:48:40 -05:00
parent 731aec1bb8
commit ec42d10f7b

View File

@ -3,33 +3,41 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { reaction } from "mobx";
import { isEqual } from "lodash";
import { autorun } from "mobx";
import clusterStoreInjectable from "../../../../common/cluster-store/cluster-store.injectable";
import type { ClusterId, ClusterState } from "../../../../common/cluster-types";
import { beforeApplicationIsLoadingInjectionToken } from "../../../../main/start-main-application/runnable-tokens/before-application-is-loading-injection-token";
import initClusterStoreInjectable from "../../store/main/init.injectable";
import emitClusterStateUpdateInjectable from "./emit-update.injectable";
const setupClusterStateBroadcastingInjectable = getInjectable({
id: "setup-cluster-state-broadcasting",
instantiate: (di) => {
const emitClusterStateUpdate = di.inject(emitClusterStateUpdateInjectable);
const clusterStore = di.inject(clusterStoreInjectable);
instantiate: (di) => ({
id: "setup-cluster-state-broadcasting",
run: () => {
const emitClusterStateUpdate = di.inject(emitClusterStateUpdateInjectable);
const clusterStore = di.inject(clusterStoreInjectable);
const prevStates = new Map<ClusterId, ClusterState>();
autorun(() => {
for (const cluster of clusterStore.clusters.values()) {
const prevState = prevStates.get(cluster.id);
const curState = cluster.getState();
if (!prevState || !isEqual(prevState, curState)) {
prevStates.set(cluster.id, curState);
return {
id: "setup-cluster-state-broadcasting",
run: () => {
reaction(() => clusterStore.connectedClustersList, () => {
for (const cluster of clusterStore.clusters.values()) {
emitClusterStateUpdate({
clusterId: cluster.id,
state: cluster.getState(),
});
}
});
},
runAfter: di.inject(initClusterStoreInjectable),
};
},
}
});
},
runAfter: di.inject(initClusterStoreInjectable),
}),
injectionToken: beforeApplicationIsLoadingInjectionToken,
});