Skip to content

Commit

Permalink
Add more specific IPC helpers
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Malton <sebastian@malton.name>
  • Loading branch information
Nokel81 committed May 25, 2021
1 parent fe4ce27 commit a09ac6e
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 78 deletions.
18 changes: 6 additions & 12 deletions src/common/base-store.ts
Expand Up @@ -22,11 +22,11 @@
import path from "path";
import Config from "conf";
import type { Options as ConfOptions } from "conf/dist/source/types";
import { app, ipcMain, IpcMainEvent, ipcRenderer, IpcRendererEvent, remote } from "electron";
import { app, ipcMain, ipcRenderer, remote } from "electron";
import { IReactionOptions, makeObservable, observable, reaction, runInAction, when } from "mobx";
import { getAppVersion, Singleton, toJS, Disposer } from "./utils";
import logger from "../main/logger";
import { broadcastMessage, subscribeToBroadcast, unsubscribeFromBroadcast } from "./ipc";
import { broadcastMessage, ipcMainOn, ipcRendererOn } from "./ipc";
import isEqual from "lodash/isEqual";

export interface BaseStoreParams<T = any> extends ConfOptions<T> {
Expand Down Expand Up @@ -126,23 +126,17 @@ export abstract class BaseStore<T = any> extends Singleton {
);

if (ipcMain) {
const callback = (event: IpcMainEvent, model: T) => {
this.syncDisposers.push(ipcMainOn(this.syncMainChannel, (event, model: T) => {
logger.silly(`[STORE]: SYNC ${this.name} from renderer`, { model });
this.onSync(model);
};

subscribeToBroadcast(this.syncMainChannel, callback);
this.syncDisposers.push(() => unsubscribeFromBroadcast(this.syncMainChannel, callback));
}));
}

if (ipcRenderer) {
const callback = (event: IpcRendererEvent, model: T) => {
this.syncDisposers.push(ipcRendererOn(this.syncRendererChannel, (event, model: T) => {
logger.silly(`[STORE]: SYNC ${this.name} from main`, { model });
this.onSyncFromMain(model);
};

subscribeToBroadcast(this.syncRendererChannel, callback);
this.syncDisposers.push(() => unsubscribeFromBroadcast(this.syncRendererChannel, callback));
}));
}
}

Expand Down
13 changes: 6 additions & 7 deletions src/common/cluster-ipc.ts
Expand Up @@ -19,7 +19,6 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { handleRequest } from "./ipc";
import { ClusterId, ClusterStore } from "./cluster-store";
import { appEventBus } from "./event-bus";
import { ResourceApplier } from "../main/resource-applier";
Expand All @@ -34,13 +33,13 @@ export const clusterKubectlApplyAllHandler = "cluster:kubectl-apply-all";
export const clusterKubectlDeleteAllHandler = "cluster:kubectl-delete-all";

if (ipcMain) {
handleRequest(clusterActivateHandler, (event, clusterId: ClusterId, force = false) => {
ipcMain.handle(clusterActivateHandler, (event, clusterId: ClusterId, force = false) => {
return ClusterStore.getInstance()
.getById(clusterId)
?.activate(force);
});

handleRequest(clusterSetFrameIdHandler, (event: IpcMainInvokeEvent, clusterId: ClusterId) => {
ipcMain.handle(clusterSetFrameIdHandler, (event: IpcMainInvokeEvent, clusterId: ClusterId) => {
const cluster = ClusterStore.getInstance().getById(clusterId);

if (cluster) {
Expand All @@ -49,13 +48,13 @@ if (ipcMain) {
}
});

handleRequest(clusterRefreshHandler, (event, clusterId: ClusterId) => {
ipcMain.handle(clusterRefreshHandler, (event, clusterId: ClusterId) => {
return ClusterStore.getInstance()
.getById(clusterId)
?.refresh({ refreshMetadata: true });
});

handleRequest(clusterDisconnectHandler, (event, clusterId: ClusterId) => {
ipcMain.handle(clusterDisconnectHandler, (event, clusterId: ClusterId) => {
appEventBus.emit({name: "cluster", action: "stop"});
const cluster = ClusterStore.getInstance().getById(clusterId);

Expand All @@ -65,7 +64,7 @@ if (ipcMain) {
}
});

handleRequest(clusterKubectlApplyAllHandler, async (event, clusterId: ClusterId, resources: string[], extraArgs: string[]) => {
ipcMain.handle(clusterKubectlApplyAllHandler, async (event, clusterId: ClusterId, resources: string[], extraArgs: string[]) => {
appEventBus.emit({name: "cluster", action: "kubectl-apply-all"});
const cluster = ClusterStore.getInstance().getById(clusterId);

Expand All @@ -84,7 +83,7 @@ if (ipcMain) {
}
});

handleRequest(clusterKubectlDeleteAllHandler, async (event, clusterId: ClusterId, resources: string[], extraArgs: string[]) => {
ipcMain.handle(clusterKubectlDeleteAllHandler, async (event, clusterId: ClusterId, resources: string[], extraArgs: string[]) => {
appEventBus.emit({name: "cluster", action: "kubectl-delete-all"});
const cluster = ClusterStore.getInstance().getById(clusterId);

Expand Down
24 changes: 17 additions & 7 deletions src/common/cluster-store.ts
Expand Up @@ -31,7 +31,7 @@ import { appEventBus } from "./event-bus";
import { dumpConfigYaml } from "./kube-helpers";
import { saveToAppFiles } from "./utils/saveToAppFiles";
import type { KubeConfig } from "@kubernetes/client-node";
import { handleRequest, requestMain, subscribeToBroadcast, unsubscribeAllFromBroadcast } from "./ipc";
import { ipcMainOn, ipcRendererOn, requestMain } from "./ipc";
import type { ResourceType } from "../renderer/components/cluster-settings/components/cluster-metrics-setting";
import { disposer, noop, toJS } from "./utils";

Expand Down Expand Up @@ -114,6 +114,8 @@ export interface ClusterPrometheusPreferences {
}

export class ClusterStore extends BaseStore<ClusterStoreModel> {
private static StateChannel = "cluster:state";

static get storedKubeConfigFolder(): string {
return path.resolve((app || remote.app).getPath("userData"), "kubeconfigs");
}
Expand Down Expand Up @@ -172,7 +174,7 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
}
});
} else if (ipcMain) {
handleRequest(ClusterStore.stateRequestChannel, (): clusterStateSync[] => {
ipcMain.handle(ClusterStore.stateRequestChannel, (): clusterStateSync[] => {
const clusterStates: clusterStateSync[] = [];

this.clustersList.forEach((cluster) => {
Expand All @@ -193,17 +195,25 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
reaction(() => this.connectedClustersList, () => {
this.pushState();
}),
() => unsubscribeAllFromBroadcast("cluster:state"),
);
}
}

handleStateChange = (event: any, clusterId: string, state: ClusterState) => {
logger.silly(`[CLUSTER-STORE]: received push-state at ${location.host} (${webFrame.routingId})`, clusterId, state);
this.getById(clusterId)?.setState(state);
};

registerIpcListener() {
logger.info(`[CLUSTER-STORE] start to listen (${webFrame.routingId})`);
subscribeToBroadcast("cluster:state", (event, clusterId: string, state: ClusterState) => {
logger.silly(`[CLUSTER-STORE]: received push-state at ${location.host} (${webFrame.routingId})`, clusterId, state);
this.getById(clusterId)?.setState(state);
});

if (ipcMain) {
this.disposer.push(ipcMainOn(ClusterStore.StateChannel, this.handleStateChange));
}

if (ipcRenderer) {
this.disposer.push(ipcRendererOn(ClusterStore.StateChannel, this.handleStateChange));
}
}

unregisterIpcListener() {
Expand Down
45 changes: 15 additions & 30 deletions src/common/ipc/ipc.ts
Expand Up @@ -26,22 +26,21 @@
import { ipcMain, ipcRenderer, remote, webContents } from "electron";
import { toJS } from "../utils/toJS";
import logger from "../../main/logger";
import { ClusterFrameInfo, clusterFrameMap } from "../cluster-frames";
import { ClusterFrameInfo, clusterFrameMap } from "../cluster-frames";
import type { Disposer } from "../utils";

const subFramesChannel = "ipc:get-sub-frames";

export function handleRequest(channel: string, listener: (event: Electron.IpcMainInvokeEvent, ...args: any[]) => any) {
ipcMain.handle(channel, async (event, ...args) => {
const payload = await listener(event, ...args);

return sanitizePayload(payload);
});
}

export async function requestMain(channel: string, ...args: any[]) {
return ipcRenderer.invoke(channel, ...args.map(sanitizePayload));
}

export function ipcMainHandle(channel: string, listener: (event: Electron.IpcMainInvokeEvent, ...args: any[]) => any) {
ipcMain.handle(channel, async (event, ...args) => {
return sanitizePayload(await listener(event, ...args));
});
}

function getSubFrames(): ClusterFrameInfo[] {
return Array.from(clusterFrameMap.values());
}
Expand Down Expand Up @@ -76,34 +75,20 @@ export function broadcastMessage(channel: string, ...args: any[]) {
});
}

export function subscribeToBroadcast(channel: string, listener: (...args: any[]) => any) {
if (ipcRenderer) {
ipcRenderer.on(channel, listener);
} else if (ipcMain) {
ipcMain.on(channel, listener);
}
export function ipcMainOn(channel: string, listener: (event: Electron.IpcMainEvent, ...args: any[]) => any): Disposer {
ipcMain.on(channel, listener);

return listener;
return () => ipcMain.off(channel, listener);
}

export function unsubscribeFromBroadcast(channel: string, listener: (...args: any[]) => any) {
if (ipcRenderer) {
ipcRenderer.off(channel, listener);
} else if (ipcMain) {
ipcMain.off(channel, listener);
}
}
export function ipcRendererOn(channel: string, listener: (event: Electron.IpcRendererEvent, ...args: any[]) => any): Disposer {
ipcRenderer.on(channel, listener);

export function unsubscribeAllFromBroadcast(channel: string) {
if (ipcRenderer) {
ipcRenderer.removeAllListeners(channel);
} else if (ipcMain) {
ipcMain.removeAllListeners(channel);
}
return () => ipcRenderer.off(channel, listener);
}

export function bindBroadcastHandlers() {
handleRequest(subFramesChannel, () => getSubFrames());
ipcMainHandle(subFramesChannel, () => getSubFrames());
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/extensions/extension-discovery.ts
Expand Up @@ -20,13 +20,13 @@
*/

import { watch } from "chokidar";
import { ipcRenderer } from "electron";
import { ipcMain, ipcRenderer } from "electron";
import { EventEmitter } from "events";
import fse from "fs-extra";
import { observable, reaction, when, makeObservable } from "mobx";
import os from "os";
import path from "path";
import { broadcastMessage, handleRequest, requestMain, subscribeToBroadcast } from "../common/ipc";
import { broadcastMessage, ipcRendererOn, requestMain } from "../common/ipc";
import { Singleton, toJS } from "../common/utils";
import logger from "../main/logger";
import { ExtensionInstallationStateStore } from "../renderer/components/+extensions/extension-install.store";
Expand Down Expand Up @@ -139,13 +139,13 @@ export class ExtensionDiscovery extends Singleton {
};

requestMain(ExtensionDiscovery.extensionDiscoveryChannel).then(onMessage);
subscribeToBroadcast(ExtensionDiscovery.extensionDiscoveryChannel, (_event, message: ExtensionDiscoveryChannelMessage) => {
ipcRendererOn(ExtensionDiscovery.extensionDiscoveryChannel, (_event, message: ExtensionDiscoveryChannelMessage) => {
onMessage(message);
});
}

async initMain() {
handleRequest(ExtensionDiscovery.extensionDiscoveryChannel, () => this.toJSON());
ipcMain.handle(ExtensionDiscovery.extensionDiscoveryChannel, () => this.toJSON());

reaction(() => this.toJSON(), () => {
this.broadcast();
Expand Down
8 changes: 4 additions & 4 deletions src/extensions/extension-loader.ts
Expand Up @@ -25,7 +25,7 @@ import { isEqual } from "lodash";
import { action, computed, makeObservable, observable, reaction, when } from "mobx";
import path from "path";
import { getHostedCluster } from "../common/cluster-store";
import { broadcastMessage, handleRequest, requestMain, subscribeToBroadcast } from "../common/ipc";
import { broadcastMessage, ipcMainOn, ipcRendererOn, requestMain, ipcMainHandle } from "../common/ipc";
import { Singleton, toJS } from "../common/utils";
import logger from "../main/logger";
import type { InstalledExtension } from "./extension-discovery";
Expand Down Expand Up @@ -174,11 +174,11 @@ export class ExtensionLoader extends Singleton {
this.isLoaded = true;
this.loadOnMain();

handleRequest(ExtensionLoader.extensionsMainChannel, () => {
ipcMainHandle(ExtensionLoader.extensionsMainChannel, () => {
return Array.from(this.toJSON());
});

subscribeToBroadcast(ExtensionLoader.extensionsRendererChannel, (_event, extensions: [LensExtensionId, InstalledExtension][]) => {
ipcMainOn(ExtensionLoader.extensionsRendererChannel, (event, extensions: [LensExtensionId, InstalledExtension][]) => {
this.syncExtensions(extensions);
});
}
Expand All @@ -199,7 +199,7 @@ export class ExtensionLoader extends Singleton {
};

requestMain(ExtensionLoader.extensionsMainChannel).then(extensionListHandler);
subscribeToBroadcast(ExtensionLoader.extensionsMainChannel, (_event, extensions: [LensExtensionId, InstalledExtension][]) => {
ipcRendererOn(ExtensionLoader.extensionsMainChannel, (event, extensions: [LensExtensionId, InstalledExtension][]) => {
extensionListHandler(extensions);
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/window-manager.ts
Expand Up @@ -24,7 +24,7 @@ import { makeObservable, observable } from "mobx";
import { app, BrowserWindow, dialog, shell, webContents } from "electron";
import windowStateKeeper from "electron-window-state";
import { appEventBus } from "../common/event-bus";
import { subscribeToBroadcast } from "../common/ipc";
import { ipcMainOn } from "../common/ipc";
import { initMenu } from "./menu";
import { initTray } from "./tray";
import { Singleton } from "../common/utils";
Expand Down Expand Up @@ -141,7 +141,7 @@ export class WindowManager extends Singleton {

protected bindEvents() {
// track visible cluster from ui
subscribeToBroadcast(IpcRendererNavigationEvents.CLUSTER_VIEW_CURRENT_ID, (event, clusterId: ClusterId) => {
ipcMainOn(IpcRendererNavigationEvents.CLUSTER_VIEW_CURRENT_ID, (event, clusterId: ClusterId) => {
this.activeClusterId = clusterId;
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/api/catalog-entity-registry.ts
Expand Up @@ -20,7 +20,7 @@
*/

import { computed, observable, makeObservable } from "mobx";
import { subscribeToBroadcast } from "../../common/ipc";
import { ipcRendererOn } from "../../common/ipc";
import { CatalogCategory, CatalogEntity, CatalogEntityData, catalogCategoryRegistry, CatalogCategoryRegistry, CatalogEntityKindData } from "../../common/catalog";
import "../../common/catalog-entities";
import { iter } from "../utils";
Expand All @@ -34,7 +34,7 @@ export class CatalogEntityRegistry {
}

init() {
subscribeToBroadcast("catalog:items", (ev, items: (CatalogEntityData & CatalogEntityKindData)[]) => {
ipcRendererOn("catalog:items", (event, items: (CatalogEntityData & CatalogEntityKindData)[]) => {
this.rawItems.replace(items);
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/components/cluster-manager/cluster-status.tsx
Expand Up @@ -26,7 +26,7 @@ import React from "react";
import { observer } from "mobx-react";
import { ipcRenderer } from "electron";
import { computed, observable, makeObservable } from "mobx";
import { requestMain, subscribeToBroadcast } from "../../../common/ipc";
import { ipcRendererOn, requestMain } from "../../../common/ipc";
import { Icon } from "../icon";
import { Button } from "../button";
import { cssNames, IClassName } from "../../utils";
Expand Down Expand Up @@ -59,7 +59,7 @@ export class ClusterStatus extends React.Component<Props> {
}

async componentDidMount() {
subscribeToBroadcast(`kube-auth:${this.cluster.id}`, (evt, res: KubeAuthProxyLog) => {
ipcRendererOn(`kube-auth:${this.cluster.id}`, (evt, res: KubeAuthProxyLog) => {
this.authOutput.push({
data: res.data.trimRight(),
error: res.error,
Expand Down
8 changes: 4 additions & 4 deletions src/renderer/components/command-palette/command-container.tsx
Expand Up @@ -26,7 +26,7 @@ import { observer } from "mobx-react";
import React from "react";
import { Dialog } from "../dialog";
import { EventEmitter } from "../../../common/event-emitter";
import { subscribeToBroadcast } from "../../../common/ipc";
import { ipcRendererOn } from "../../../common/ipc";
import { CommandDialog } from "./command-dialog";
import type { ClusterId } from "../../../common/cluster-store";
import { CommandRegistration, CommandRegistry } from "../../../extensions/registries/command-registry";
Expand Down Expand Up @@ -84,16 +84,16 @@ export class CommandContainer extends React.Component<CommandContainerProps> {

componentDidMount() {
if (this.props.clusterId) {
subscribeToBroadcast(`command-palette:run-action:${this.props.clusterId}`, (event, commandId: string) => {
ipcRendererOn(`command-palette:run-action:${this.props.clusterId}`, (event, commandId: string) => {
const command = this.findCommandById(commandId);

if (command) {
this.runCommand(command);
}
});
} else {
subscribeToBroadcast("command-palette:open", () => {
this.commandComponent = <CommandDialog />;
ipcRendererOn("command-palette:open", () => {
CommandOverlay.open(<CommandDialog />);
});
}
window.addEventListener("keyup", (e) => this.escHandler(e), true);
Expand Down

0 comments on commit a09ac6e

Please sign in to comment.