Make cluster state sync fully injectable

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton committed 2022-12-06 09:19:00 -05:00
1 parent 659b63d0cf
commit f7b5ae7b57
13 files changed
+237 -65

No files matched your search

@@ -0,0 +1,21 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { ClusterId, ClusterState } from "../../../../common/cluster-types";
import type { MessageChannel } from "../../../../common/utils/channel/message-channel-listener-injection-token";
import type { RequestChannel } from "../../../../common/utils/channel/request-channel-listener-injection-token";
export interface ClusterStateSync {
clusterId: ClusterId;
state: ClusterState;
}
export const clusterStateSyncChannel: MessageChannel<ClusterStateSync> = {
id: "cluster-state-sync",
};
export const initialClusterStatesChannel: RequestChannel<void, ClusterStateSync[]> = {
id: "initial-cluster-state-sync",
};
@@ -0,0 +1,21 @@
/**
* 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 type { MessageChannelHandler } from "../../../../common/utils/channel/message-channel-listener-injection-token";
import { sendMessageToChannelInjectionToken } from "../../../../common/utils/channel/message-to-channel-injection-token";
import { clusterStateSyncChannel } from "../common/channels";
export type EmitClusterStateUpdate = MessageChannelHandler<typeof clusterStateSyncChannel>;
const emitClusterStateUpdateInjectable = getInjectable({
id: "emit-cluster-state-update",
instantiate: (di): EmitClusterStateUpdate => {
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
return (message) => sendMessageToChannel(clusterStateSyncChannel, message);
},
});
export default emitClusterStateUpdateInjectable;
@@ -0,0 +1,21 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import clusterStoreInjectable from "../../../../common/cluster-store/cluster-store.injectable";
import { getRequestChannelListenerInjectable } from "../../../../main/utils/channel/channel-listeners/listener-tokens";
import { initialClusterStatesChannel } from "../common/channels";
const handleInitialClusterStateSyncInjectable = getRequestChannelListenerInjectable({
channel: initialClusterStatesChannel,
handler: (di) => {
const clusterStore = di.inject(clusterStoreInjectable);
return () => clusterStore.clustersList.map(cluster => ({
clusterId: cluster.id,
state: cluster.getState(),
}));
},
});
export default handleInitialClusterStateSyncInjectable;
@@ -0,0 +1,36 @@
/**
* 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 { reaction } from "mobx";
import clusterStoreInjectable from "../../../../common/cluster-store/cluster-store.injectable";
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);
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),
};
},
injectionToken: beforeApplicationIsLoadingInjectionToken,
});
export default setupClusterStateBroadcastingInjectable;
@@ -0,0 +1,19 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import getClusterByIdInjectable from "../../../../common/cluster-store/get-by-id.injectable";
import { getMessageChannelListenerInjectable } from "../../../../common/utils/channel/message-channel-listener-injection-token";
import { clusterStateSyncChannel } from "../common/channels";
const clusterStateListenerInjectable = getMessageChannelListenerInjectable({
channel: clusterStateSyncChannel,
id: "main",
handler: (di) => {
const getClusterById = di.inject(getClusterByIdInjectable);
return ({ clusterId, state }) => getClusterById(clusterId)?.setState(state);
},
});
export default clusterStateListenerInjectable;
@@ -0,0 +1,21 @@
/**
* 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 type { RequestChannelHandler } from "../../../../main/utils/channel/channel-listeners/listener-tokens";
import requestFromChannelInjectable from "../../../../renderer/utils/channel/request-from-channel.injectable";
import { initialClusterStatesChannel } from "../common/channels";
export type RequestInitialClusterStates = RequestChannelHandler<typeof initialClusterStatesChannel>;
const requestInitialClusterStatesInjectable = getInjectable({
id: "request-initial-cluster-states",
instantiate: (di): RequestInitialClusterStates => {
const requestFromChannel = di.inject(requestFromChannelInjectable);
return () => requestFromChannel(initialClusterStatesChannel);
},
});
export default requestInitialClusterStatesInjectable;
@@ -0,0 +1,32 @@
/**
* 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 getClusterByIdInjectable from "../../../../common/cluster-store/get-by-id.injectable";
import { beforeFrameStartsInjectionToken } from "../../../../renderer/before-frame-starts/tokens";
import initClusterStoreInjectable from "../../store/renderer/init.injectable";
import requestInitialClusterStatesInjectable from "./request-initial.injectable";
const setupClusterStateSyncInjectable = getInjectable({
id: "setup-cluster-state-sync",
instantiate: (di) => {
const requestInitialClusterStates = di.inject(requestInitialClusterStatesInjectable);
const getClusterById = di.inject(getClusterByIdInjectable);
return {
id: "setup-cluster-state-sync",
run: async () => {
const initalStates = await requestInitialClusterStates();
for (const { clusterId, state } of initalStates) {
getClusterById(clusterId)?.setState(state);
}
},
runAfter: di.inject(initClusterStoreInjectable),
};
},
injectionToken: beforeFrameStartsInjectionToken,
});
export default setupClusterStateSyncInjectable;
@@ -0,0 +1,26 @@
/**
* 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 clusterStoreInjectable from "../../../../common/cluster-store/cluster-store.injectable";
import { beforeApplicationIsLoadingInjectionToken } from "../../../../main/start-main-application/runnable-tokens/before-application-is-loading-injection-token";
import initUserStoreInjectable from "../../../../main/stores/init-user-store.injectable";
const initClusterStoreInjectable = getInjectable({
id: "init-cluster-store",
instantiate: (di) => {
const clusterStore = di.inject(clusterStoreInjectable);
return {
id: "init-cluster-store",
run: () => {
clusterStore.load();
},
runAfter: di.inject(initUserStoreInjectable),
};
},
injectionToken: beforeApplicationIsLoadingInjectionToken,
});
export default initClusterStoreInjectable;
@@ -0,0 +1,26 @@
/**
* 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 clusterStoreInjectable from "../../../../common/cluster-store/cluster-store.injectable";
import { beforeFrameStartsInjectionToken } from "../../../../renderer/before-frame-starts/tokens";
import initUserStoreInjectable from "../../../../renderer/stores/init-user-store.injectable";
const initClusterStoreInjectable = getInjectable({
id: "init-cluster-store",
instantiate: (di) => {
const clusterStore = di.inject(clusterStoreInjectable);
return {
id: "init-cluster-store",
run: () => {
clusterStore.load();
},
runAfter: di.inject(initUserStoreInjectable),
};
},
injectionToken: beforeFrameStartsInjectionToken,
});
export default initClusterStoreInjectable;