Refactor ipc module (#1225)

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen authored and GitHub committed 2020-11-19 11:48:13 +02:00
1 parent c4b98534dc
commit 5aaacb21f9
20 files changed
+224 -242

No files matched your search

+3
View File
@@ -14,6 +14,7 @@ import { clusterStore } from "../common/cluster-store";
import { i18nStore } from "./i18n";
import { themeStore } from "./theme.store";
import { extensionsStore } from "../extensions/extensions-store";
import { extensionLoader } from "../extensions/extension-loader";
type AppComponent = React.ComponentType & {
init?(): Promise<void>;
@@ -30,6 +31,8 @@ export async function bootstrap(App: AppComponent) {
const rootElem = document.getElementById("app")
rootElem.classList.toggle("is-mac", isMac);
extensionLoader.init()
// preload common stores
await Promise.all([
userStore.load(),
@@ -12,8 +12,9 @@ import { Cluster } from "../../../main/cluster";
import { ClusterIcon } from "../cluster-icon";
import { IClusterSettingsRouteParams } from "./cluster-settings.route";
import { clusterStore } from "../../../common/cluster-store";
import { clusterIpc } from "../../../common/cluster-ipc";
import { PageLayout } from "../layout/page-layout";
import { requestMain } from "../../../common/ipc";
import { clusterActivateHandler, clusterRefreshHandler } from "../../../common/cluster-ipc"
interface Props extends RouteComponentProps<IClusterSettingsRouteParams> {
}
@@ -41,8 +42,8 @@ export class ClusterSettings extends React.Component<Props> {
refreshCluster = async () => {
if (this.cluster) {
await clusterIpc.activate.invokeFromRenderer(this.cluster.id);
await clusterIpc.refresh.invokeFromRenderer(this.cluster.id);
await requestMain(clusterActivateHandler, this.cluster.id)
await requestMain(clusterRefreshHandler, this.cluster.id)
}
}
@@ -1,7 +1,6 @@
import React from "react";
import { Trans } from "@lingui/macro";
import { observer } from "mobx-react";
import { clusterIpc } from "../../../../common/cluster-ipc";
import { clusterStore } from "../../../../common/cluster-store";
import { Cluster } from "../../../../main/cluster";
import { autobind } from "../../../utils";
+4 -3
View File
@@ -33,12 +33,13 @@ import { ErrorBoundary } from "./error-boundary";
import { Terminal } from "./dock/terminal";
import { getHostedCluster, getHostedClusterId } from "../../common/cluster-store";
import logger from "../../main/logger";
import { clusterIpc } from "../../common/cluster-ipc";
import { webFrame } from "electron";
import { clusterPageRegistry } from "../../extensions/registries/page-registry";
import { extensionLoader } from "../../extensions/extension-loader";
import { appEventBus } from "../../common/event-bus";
import { appEventBus } from "../../common/event-bus"
import { requestMain } from "../../common/ipc";
import whatInput from 'what-input';
import { clusterSetFrameIdHandler } from "../../common/cluster-ipc";
@observer
export class App extends React.Component {
@@ -48,7 +49,7 @@ export class App extends React.Component {
logger.info(`[APP]: Init dashboard, clusterId=${clusterId}, frameId=${frameId}`)
await Terminal.preloadFonts()
await clusterIpc.setFrameId.invokeFromRenderer(clusterId, frameId);
await requestMain(clusterSetFrameIdHandler, clusterId, frameId)
await getHostedCluster().whenReady; // cluster.activate() is done at this point
extensionLoader.loadOnClusterRenderer();
appEventBus.emit({
@@ -5,13 +5,14 @@ import React from "react";
import { observer } from "mobx-react";
import { ipcRenderer } from "electron";
import { computed, observable } from "mobx";
import { clusterIpc } from "../../../common/cluster-ipc";
import { requestMain, subscribeToBroadcast } from "../../../common/ipc";
import { Icon } from "../icon";
import { Button } from "../button";
import { cssNames, IClassName } from "../../utils";
import { Cluster } from "../../../main/cluster";
import { ClusterId, clusterStore } from "../../../common/cluster-store";
import { CubeSpinner } from "../spinner";
import { clusterActivateHandler } from "../../../common/cluster-ipc";
interface Props {
className?: IClassName;
@@ -32,7 +33,7 @@ export class ClusterStatus extends React.Component<Props> {
}
async componentDidMount() {
ipcRenderer.on(`kube-auth:${this.cluster.id}`, (evt, res: KubeAuthProxyLog) => {
subscribeToBroadcast(`kube-auth:${this.cluster.id}`, (evt, res: KubeAuthProxyLog) => {
this.authOutput.push({
data: res.data.trimRight(),
error: res.error,
@@ -48,7 +49,7 @@ export class ClusterStatus extends React.Component<Props> {
}
activateCluster = async (force = false) => {
await clusterIpc.activate.invokeFromRenderer(this.props.clusterId, force);
await requestMain(clusterActivateHandler, this.props.clusterId, force)
}
reconnect = async () => {
@@ -2,6 +2,7 @@ import "./clusters-menu.scss"
import React from "react";
import { remote } from "electron"
import { requestMain } from "../../../common/ipc";
import type { Cluster } from "../../../main/cluster";
import { DragDropContext, Draggable, DraggableProvided, Droppable, DroppableProvided, DropResult } from "react-beautiful-dnd";
import { observer } from "mobx-react";
@@ -20,9 +21,9 @@ import { clusterSettingsURL } from "../+cluster-settings";
import { landingURL } from "../+landing-page";
import { Tooltip } from "../tooltip";
import { ConfirmDialog } from "../confirm-dialog";
import { clusterIpc } from "../../../common/cluster-ipc";
import { clusterViewURL } from "./cluster-view.route";
import { getExtensionPageUrl, globalPageMenuRegistry, globalPageRegistry } from "../../../extensions/registries";
import { clusterDisconnectHandler } from "../../../common/cluster-ipc";
interface Props {
className?: IClassName;
@@ -60,7 +61,7 @@ export class ClustersMenu extends React.Component<Props> {
navigate(landingURL());
clusterStore.setActive(null);
}
await clusterIpc.disconnect.invokeFromRenderer(cluster.id);
await requestMain(clusterDisconnectHandler, cluster.id)
}
}))
}
+3 -2
View File
@@ -13,16 +13,17 @@ import { WhatsNew, whatsNewRoute } from "./components/+whats-new";
import { Notifications } from "./components/notifications";
import { ConfirmDialog } from "./components/confirm-dialog";
import { extensionLoader } from "../extensions/extension-loader";
import { broadcastMessage } from "../common/ipc";
@observer
export class LensApp extends React.Component {
static async init() {
extensionLoader.loadOnClusterManagerRenderer();
window.addEventListener("offline", () => {
ipcRenderer.send("network:offline")
broadcastMessage("network:offline")
})
window.addEventListener("online", () => {
ipcRenderer.send("network:online")
broadcastMessage("network:online")
})
}
+6 -5
View File
@@ -7,6 +7,7 @@ import { createObservableHistory } from "mobx-observable-history";
import { createBrowserHistory, LocationDescriptor } from "history";
import logger from "../main/logger";
import { clusterViewRoute, IClusterViewRouteParams } from "./components/cluster-manager/cluster-view.route";
import { broadcastMessage, subscribeToBroadcast } from "../common/ipc";
export const history = createBrowserHistory();
export const navigation = createObservableHistory(history);
@@ -111,19 +112,19 @@ export function getMatchedClusterId(): string {
if (process.isMainFrame) {
// Keep track of active cluster-id for handling IPC/menus/etc.
reaction(() => getMatchedClusterId(), clusterId => {
ipcRenderer.send("cluster-view:current-id", clusterId);
broadcastMessage("cluster-view:current-id", clusterId)
}, {
fireImmediately: true
})
}
// Handle navigation via IPC (e.g. from top menu)
ipcRenderer.on("renderer:navigate", (event, location: LocationDescriptor) => {
subscribeToBroadcast("renderer:navigate", (event, location: LocationDescriptor) => {
logger.info(`[IPC]: ${event.type} ${JSON.stringify(location)}`, event);
navigate(location);
});
})
// Reload dashboard window
ipcRenderer.on("renderer:reload", () => {
subscribeToBroadcast("renderer:reload", () => {
location.reload();
});
})