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());
@@ -16,9 +16,7 @@ import setupMainWindowVisibilityAfterActivationInjectable from "./electron-app/r
import setupDeviceShutdownInjectable from "./electron-app/runnables/setup-device-shutdown.injectable";
import setupApplicationNameInjectable from "./electron-app/runnables/setup-application-name.injectable";
import setupRunnablesBeforeClosingOfApplicationInjectable from "./electron-app/runnables/setup-runnables-before-closing-of-application.injectable";
import clusterFramesInjectable from "../common/cluster-frames.injectable";
import type { ClusterFrameInfo } from "../common/cluster-frames";
import { observable, runInAction } from "mobx";
import { runInAction } from "mobx";
import broadcastMessageInjectable from "../common/ipc/broadcast-message.injectable";
import electronQuitAndInstallUpdateInjectable from "./electron-app/features/electron-quit-and-install-update.injectable";
import electronUpdaterIsActiveInjectable from "./electron-app/features/electron-updater-is-active.injectable";
@@ -65,8 +63,6 @@ export function getDiForUnitTesting() {
overrideElectronFeatures(di);
getOverrideFsWithFakes()(di);
di.override(clusterFramesInjectable, () => observable.map<string, ClusterFrameInfo>());
di.override(broadcastMessageInjectable, () => (channel) => {
throw new Error(`Tried to broadcast message to channel "${channel}" over IPC without explicit override.`);
});
@@ -5,7 +5,7 @@
import { getInjectable } from "@ogre-tools/injectable";
import type { ContentSource, ElectronWindowTitleBarStyle } from "./create-electron-window.injectable";
import createElectronWindowForInjectable from "./create-electron-window.injectable";
import type { ClusterFrameInfo } from "../../../../common/cluster-frames";
import type { ClusterFrameInfo } from "../../../../common/cluster-frames.injectable";
export interface ElectronWindow {
show: () => void;
@@ -8,7 +8,7 @@ import clusterFramesInjectable from "../../../common/cluster-frames.injectable";
import type { MessageChannel } from "../../../common/utils/channel/message-channel-listener-injection-token";
import { sendMessageToChannelInjectionToken } from "../../../common/utils/channel/message-to-channel-injection-token";
import type { DiContainer } from "@ogre-tools/injectable";
import type { ClusterFrameInfo } from "../../../common/cluster-frames";
import type { ClusterFrameInfo } from "../../../common/cluster-frames.injectable";
describe("message-to-channel", () => {
let di: DiContainer;