Move deactivating a cluster into injectable IPC (#7356)

* Move activating cluster into injectable IPC

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Move deactivating a cluster into injectable IPC

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix type error

Signed-off-by: Sebastian Malton <sebastian@malton.name>

---------

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton authored and GitHub committed 2023-03-15 08:31:38 -04:00
1 parent 497f63fde6
commit ae9a8c3c41
16 files changed
+111 -96

No files matched your search

@@ -8,10 +8,9 @@ import loggerInjectable from "../../../../common/logger.injectable";
import clusterStoreInjectable from "../../../../common/cluster-store/cluster-store.injectable";
import { onLoadOfApplicationInjectionToken } from "@k8slens/application";
import applicationMenuItemCompositeInjectable from "../../../../features/application-menu/main/application-menu-item-composite.injectable";
import emitAppEventInjectable from "../../../../common/app-event-bus/emit-event.injectable";
import getClusterByIdInjectable from "../../../../common/cluster-store/get-by-id.injectable";
import pushCatalogToRendererInjectable from "../../../catalog-sync-to-renderer/push-catalog-to-renderer.injectable";
import clusterConnectionInjectable from "../../../cluster/cluster-connection.injectable";
import clusterFramesInjectable from "../../../../common/cluster-frames.injectable";
const setupIpcMainHandlersInjectable = getInjectable({
id: "setup-ipc-main-handlers",
@@ -19,21 +18,15 @@ const setupIpcMainHandlersInjectable = getInjectable({
instantiate: (di) => ({
run: () => {
const logger = di.inject(loggerInjectable);
const applicationMenuItemComposite = di.inject(applicationMenuItemCompositeInjectable);
const pushCatalogToRenderer = di.inject(pushCatalogToRendererInjectable);
const clusterStore = di.inject(clusterStoreInjectable);
const emitAppEvent = di.inject(emitAppEventInjectable);
const getClusterById = di.inject(getClusterByIdInjectable);
logger.debug("[APP-MAIN] initializing ipc main handlers");
setupIpcMainHandlers({
applicationMenuItemComposite,
pushCatalogToRenderer,
clusterStore,
emitAppEvent,
getClusterById,
getClusterConnection: (cluster) => di.inject(clusterConnectionInjectable, cluster),
applicationMenuItemComposite: di.inject(applicationMenuItemCompositeInjectable),
pushCatalogToRenderer: di.inject(pushCatalogToRendererInjectable),
clusterStore: di.inject(clusterStoreInjectable),
getClusterById: di.inject(getClusterByIdInjectable),
clusterFrames: di.inject(clusterFramesInjectable),
});
},
}),
@@ -4,62 +4,43 @@
*/
import type { IpcMainInvokeEvent } from "electron";
import { BrowserWindow, Menu } from "electron";
import { clusterFrameMap } from "../../../../common/cluster-frames";
import { clusterSetFrameIdHandler, clusterDisconnectHandler, clusterStates } from "../../../../common/ipc/cluster";
import type { ClusterFrameInfo } from "../../../../common/cluster-frames.injectable";
import { clusterSetFrameIdHandler, clusterStates } from "../../../../common/ipc/cluster";
import type { ClusterId } from "../../../../common/cluster-types";
import type { ClusterStore } from "../../../../common/cluster-store/cluster-store";
import { broadcastMainChannel, broadcastMessage, ipcMainHandle, ipcMainOn } from "../../../../common/ipc";
import type { IComputedValue } from "mobx";
import type { IComputedValue, ObservableMap } from "mobx";
import { windowActionHandleChannel, windowLocationChangedChannel, windowOpenAppMenuAsContextMenuChannel } from "../../../../common/ipc/window";
import { handleWindowAction, onLocationChange } from "../../../ipc/window";
import type { ApplicationMenuItemTypes } from "../../../../features/application-menu/main/menu-items/application-menu-item-injection-token";
import type { Composite } from "../../../../common/utils/composite/get-composite/get-composite";
import { getApplicationMenuTemplate } from "../../../../features/application-menu/main/populate-application-menu.injectable";
import type { MenuItemRoot } from "../../../../features/application-menu/main/application-menu-item-composite.injectable";
import type { EmitAppEvent } from "../../../../common/app-event-bus/emit-event.injectable";
import type { GetClusterById } from "../../../../common/cluster-store/get-by-id.injectable";
import type { Cluster } from "../../../../common/cluster/cluster";
import type { ClusterConnection } from "../../../cluster/cluster-connection.injectable";
interface Dependencies {
applicationMenuItemComposite: IComputedValue<Composite<ApplicationMenuItemTypes | MenuItemRoot>>;
clusterStore: ClusterStore;
emitAppEvent: EmitAppEvent;
getClusterById: GetClusterById;
pushCatalogToRenderer: () => void;
getClusterConnection: (cluster: Cluster) => ClusterConnection;
clusterFrames: ObservableMap<string, ClusterFrameInfo>;
}
export const setupIpcMainHandlers = ({
applicationMenuItemComposite,
clusterStore,
emitAppEvent,
getClusterById,
pushCatalogToRenderer,
getClusterConnection,
clusterFrames,
}: Dependencies) => {
ipcMainHandle(clusterSetFrameIdHandler, (event: IpcMainInvokeEvent, clusterId: ClusterId) => {
const cluster = getClusterById(clusterId);
if (cluster) {
clusterFrameMap.set(cluster.id, { frameId: event.frameId, processId: event.processId });
clusterFrames.set(cluster.id, { frameId: event.frameId, processId: event.processId });
pushCatalogToRenderer();
}
});
ipcMainHandle(clusterDisconnectHandler, (event, clusterId: ClusterId) => {
emitAppEvent({ name: "cluster", action: "stop" });
const cluster = getClusterById(clusterId);
if (!cluster) {
return;
}
const clusterConnection = getClusterConnection(cluster);
clusterConnection.disconnect();
clusterFrameMap.delete(cluster.id);
});
ipcMainHandle(windowActionHandleChannel, (event, action) => handleWindowAction(action));
ipcMainOn(windowLocationChangedChannel, () => onLocationChange());