From 07c73b20ffc4beaffe9132836bd1cf3e0590cd4e Mon Sep 17 00:00:00 2001 From: Juho Heikka Date: Mon, 11 Apr 2022 15:21:01 +0300 Subject: [PATCH] Add type to app event bus event. Signed-off-by: Juho Heikka --- src/common/__tests__/event-bus.test.ts | 2 +- src/common/app-event-bus/event-bus.ts | 24 +++++++++++-- src/common/cluster-store/cluster-store.ts | 2 +- src/common/k8s-api/api-base.ts | 35 ++++++++++++------- src/common/user-store/user-store.ts | 4 +-- src/main/exit-app.ts | 2 +- src/main/index.ts | 4 +-- .../init-ipc-main-handlers.ts | 8 ++--- src/main/lens-proxy.ts | 2 +- src/main/resource-applier.ts | 4 +-- src/main/shell-session/shell-session.ts | 2 +- src/main/window-manager.ts | 8 ++--- .../components/+add-cluster/add-cluster.tsx | 4 +-- .../components/+catalog/catalog.test.tsx | 2 ++ src/renderer/components/+catalog/catalog.tsx | 2 ++ .../init-cluster-frame/init-cluster-frame.ts | 1 + 16 files changed, 70 insertions(+), 36 deletions(-) diff --git a/src/common/__tests__/event-bus.test.ts b/src/common/__tests__/event-bus.test.ts index e7165aac92..efd76ace4a 100644 --- a/src/common/__tests__/event-bus.test.ts +++ b/src/common/__tests__/event-bus.test.ts @@ -19,7 +19,7 @@ describe("event bus tests", () => { event = data; }); - appEventBus.emit({ name: "foo", action: "bar" }); + appEventBus.emit({ type: "APP_EVENT", name: "foo", action: "bar" }); expect(event.name).toBe("foo"); }); }); diff --git a/src/common/app-event-bus/event-bus.ts b/src/common/app-event-bus/event-bus.ts index 71a27e1a42..fc481be0b9 100644 --- a/src/common/app-event-bus/event-bus.ts +++ b/src/common/app-event-bus/event-bus.ts @@ -5,10 +5,30 @@ import { EventEmitter } from "../event-emitter"; -export interface AppEvent { +export interface Keyable { [key: string]: any } +export interface LensAppEvent { + type: "APP_EVENT"; name: string; action: string; - params?: object; + params?: Keyable; } +export interface TelemetryEvent { + type: "TELEMETRY_EVENT"; + name: string; + credentials: Keyable; + event: Keyable; +} + +export interface LensProxyEvent { + type: "LENS_PROXY_EVENT"; + name: string; + action: string; + params: { + port: number; + }; +} + +export type AppEvent = LensAppEvent | TelemetryEvent | LensProxyEvent; + export const appEventBus = new EventEmitter<[AppEvent]>(); diff --git a/src/common/cluster-store/cluster-store.ts b/src/common/cluster-store/cluster-store.ts index 901f340bda..bed3259615 100644 --- a/src/common/cluster-store/cluster-store.ts +++ b/src/common/cluster-store/cluster-store.ts @@ -108,7 +108,7 @@ export class ClusterStore extends BaseStore { } addCluster(clusterOrModel: ClusterModel | Cluster): Cluster { - appEventBus.emit({ name: "cluster", action: "add" }); + appEventBus.emit({ type: "APP_EVENT", name: "cluster", action: "add" }); const cluster = clusterOrModel instanceof Cluster ? clusterOrModel diff --git a/src/common/k8s-api/api-base.ts b/src/common/k8s-api/api-base.ts index e511dd8454..999e0b313f 100644 --- a/src/common/k8s-api/api-base.ts +++ b/src/common/k8s-api/api-base.ts @@ -5,27 +5,36 @@ import { JsonApi } from "./json-api"; import { apiPrefix, isDebugging, isDevelopment } from "../vars"; +import type { AppEvent, LensProxyEvent } from "../app-event-bus/event-bus"; import { appEventBus } from "../app-event-bus/event-bus"; export let apiBase: JsonApi; +const isProxyEvent = (event: AppEvent): event is LensProxyEvent => { + return event.type === "LENS_PROXY_EVENT"; +}; + if (typeof window === "undefined") { appEventBus.addListener((event) => { - if (event.name !== "lens-proxy" && event.action !== "listen") return; + if (isProxyEvent(event)) { + if(event.name !== "lens-proxy" && event.action !== "listen") { + return; + } - const params = event.params as { port?: number }; + if (!event.params.port) { + return; + } - if (!params.port) return; - - apiBase = new JsonApi({ - serverAddress: `http://127.0.0.1:${params.port}`, - apiBase: apiPrefix, - debug: isDevelopment || isDebugging, - }, { - headers: { - "Host": `localhost:${params.port}`, - }, - }); + apiBase = new JsonApi({ + serverAddress: `http://127.0.0.1:${event.params.port}`, + apiBase: apiPrefix, + debug: isDevelopment || isDebugging, + }, { + headers: { + "Host": `localhost:${event.params.port}`, + }, + }); + } }); } else { apiBase = new JsonApi({ diff --git a/src/common/user-store/user-store.ts b/src/common/user-store/user-store.ts index a948def9d8..8da98e6d6d 100644 --- a/src/common/user-store/user-store.ts +++ b/src/common/user-store/user-store.ts @@ -99,7 +99,7 @@ export class UserStore extends BaseStore /* implements UserStore startMainReactions() { // track telemetry availability reaction(() => this.allowTelemetry, allowed => { - appEventBus.emit({ name: "telemetry", action: allowed ? "enabled" : "disabled" }); + appEventBus.emit({ type: "APP_EVENT", name: "telemetry", action: allowed ? "enabled" : "disabled" }); }); // open at system start-up @@ -148,7 +148,7 @@ export class UserStore extends BaseStore /* implements UserStore @action saveLastSeenAppVersion() { - appEventBus.emit({ name: "app", action: "whats-new-seen" }); + appEventBus.emit({ type: "APP_EVENT", name: "app", action: "whats-new-seen" }); this.lastSeenAppVersion = getAppVersion(); } diff --git a/src/main/exit-app.ts b/src/main/exit-app.ts index c9a5bc5bf1..25583a8f57 100644 --- a/src/main/exit-app.ts +++ b/src/main/exit-app.ts @@ -13,7 +13,7 @@ export function exitApp() { const windowManager = WindowManager.getInstance(false); const clusterManager = ClusterManager.getInstance(false); - appEventBus.emit({ name: "service", action: "close" }); + appEventBus.emit({ type: "APP_EVENT", name: "service", action: "close" }); windowManager?.hide(); clusterManager?.stop(); logger.info("SERVICE:QUIT"); diff --git a/src/main/index.ts b/src/main/index.ts index 4162b30bcd..5745db0200 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -157,7 +157,7 @@ async function main(di: DiContainer) { logger.info("APP:QUIT"); - appEventBus.emit({ name: "app", action: "close" }); + appEventBus.emit({ type: "APP_EVENT", name: "app", action: "close" }); ClusterManager.getInstance(false)?.stop(); // close cluster connections const kubeConfigSyncManager = di.inject(kubeconfigSyncManagerInjectable); @@ -356,7 +356,7 @@ async function main(di: DiContainer) { } setTimeout(() => { - appEventBus.emit({ name: "service", action: "start" }); + appEventBus.emit({ type: "APP_EVENT", name: "service", action: "start" }); }, 1000); } diff --git a/src/main/initializers/init-ipc-main-handlers/init-ipc-main-handlers.ts b/src/main/initializers/init-ipc-main-handlers/init-ipc-main-handlers.ts index 1a657b78be..51222666ba 100644 --- a/src/main/initializers/init-ipc-main-handlers/init-ipc-main-handlers.ts +++ b/src/main/initializers/init-ipc-main-handlers/init-ipc-main-handlers.ts @@ -61,7 +61,7 @@ export const initIpcMainHandlers = ({ applicationMenuItems, directoryForLensLoca }); ipcMainHandle(clusterDisconnectHandler, (event, clusterId: ClusterId) => { - appEventBus.emit({ name: "cluster", action: "stop" }); + appEventBus.emit({ type: "APP_EVENT", name: "cluster", action: "stop" }); const cluster = ClusterStore.getInstance().getById(clusterId); if (cluster) { @@ -71,7 +71,7 @@ export const initIpcMainHandlers = ({ applicationMenuItems, directoryForLensLoca }); ipcMainHandle(clusterDeleteHandler, async (event, clusterId: ClusterId) => { - appEventBus.emit({ name: "cluster", action: "remove" }); + appEventBus.emit({ type: "APP_EVENT", name: "cluster", action: "remove" }); const clusterStore = ClusterStore.getInstance(); const cluster = clusterStore.getById(clusterId); @@ -105,7 +105,7 @@ export const initIpcMainHandlers = ({ applicationMenuItems, directoryForLensLoca }); ipcMainHandle(clusterKubectlApplyAllHandler, async (event, clusterId: ClusterId, resources: string[], extraArgs: string[]) => { - appEventBus.emit({ name: "cluster", action: "kubectl-apply-all" }); + appEventBus.emit({ type: "APP_EVENT", name: "cluster", action: "kubectl-apply-all" }); const cluster = ClusterStore.getInstance().getById(clusterId); if (cluster) { @@ -124,7 +124,7 @@ export const initIpcMainHandlers = ({ applicationMenuItems, directoryForLensLoca }); ipcMainHandle(clusterKubectlDeleteAllHandler, async (event, clusterId: ClusterId, resources: string[], extraArgs: string[]) => { - appEventBus.emit({ name: "cluster", action: "kubectl-delete-all" }); + appEventBus.emit({ type: "APP_EVENT", name: "cluster", action: "kubectl-delete-all" }); const cluster = ClusterStore.getInstance().getById(clusterId); if (cluster) { diff --git a/src/main/lens-proxy.ts b/src/main/lens-proxy.ts index f37b7a087c..f7970275fa 100644 --- a/src/main/lens-proxy.ts +++ b/src/main/lens-proxy.ts @@ -117,7 +117,7 @@ export class LensProxy extends Singleton { }); this.port = port; - appEventBus.emit({ name: "lens-proxy", action: "listen", params: { port }}); + appEventBus.emit({ type: "APP_EVENT", name: "lens-proxy", action: "listen", params: { port }}); resolve(port); }) .once("error", (error) => { diff --git a/src/main/resource-applier.ts b/src/main/resource-applier.ts index 11c26ad171..c445c8a7f8 100644 --- a/src/main/resource-applier.ts +++ b/src/main/resource-applier.ts @@ -27,7 +27,7 @@ export class ResourceApplier { * @param ns The optional namespace of the kube resource */ async patch(name: string, kind: string, patch: Patch, ns?: string): Promise { - appEventBus.emit({ name: "resource", action: "patch" }); + appEventBus.emit({ type: "APP_EVENT", name: "resource", action: "patch" }); const kubectl = await this.cluster.ensureKubectl(); const kubectlPath = await kubectl.getPath(); @@ -60,7 +60,7 @@ export class ResourceApplier { async apply(resource: KubernetesObject | any): Promise { resource = this.sanitizeObject(resource); - appEventBus.emit({ name: "resource", action: "apply" }); + appEventBus.emit({ type: "APP_EVENT", name: "resource", action: "apply" }); return this.kubectlApply(yaml.dump(resource)); } diff --git a/src/main/shell-session/shell-session.ts b/src/main/shell-session/shell-session.ts index 831c24bc02..43e36186cc 100644 --- a/src/main/shell-session/shell-session.ts +++ b/src/main/shell-session/shell-session.ts @@ -286,7 +286,7 @@ export abstract class ShellSession { } }); - appEventBus.emit({ name: this.ShellType, action: "open" }); + appEventBus.emit({ type: "APP_EVENT", name: this.ShellType, action: "open" }); } protected getPathEntries(): string[] { diff --git a/src/main/window-manager.ts b/src/main/window-manager.ts index 260a82ccc0..805e5c8964 100644 --- a/src/main/window-manager.ts +++ b/src/main/window-manager.ts @@ -81,10 +81,10 @@ export class WindowManager extends Singleton { // open external links in default browser (target=_blank, window.open) this.mainWindow .on("focus", () => { - appEventBus.emit({ name: "app", action: "focus" }); + appEventBus.emit({ type: "APP_EVENT", name: "app", action: "focus" }); }) .on("blur", () => { - appEventBus.emit({ name: "app", action: "blur" }); + appEventBus.emit({ type: "APP_EVENT", name: "app", action: "blur" }); }) .on("closed", () => { // clean up @@ -95,7 +95,7 @@ export class WindowManager extends Singleton { }) .webContents .on("dom-ready", () => { - appEventBus.emit({ name: "app", action: "dom-ready" }); + appEventBus.emit({ type: "APP_EVENT", name: "app", action: "dom-ready" }); }) .on("did-fail-load", (_event, code, desc) => { logger.error(`[WINDOW-MANAGER]: Failed to load Main window`, { code, desc }); @@ -178,7 +178,7 @@ export class WindowManager extends Singleton { this.splashWindow?.close(); this.splashWindow = undefined; setTimeout(() => { - appEventBus.emit({ name: "app", action: "start" }); + appEventBus.emit({ type: "APP_EVENT", name: "app", action: "start" }); }, 1000); } catch (error) { logger.error(`Showing main window failed: ${error.stack || error}`); diff --git a/src/renderer/components/+add-cluster/add-cluster.tsx b/src/renderer/components/+add-cluster/add-cluster.tsx index ce53440966..5ee6d36a82 100644 --- a/src/renderer/components/+add-cluster/add-cluster.tsx +++ b/src/renderer/components/+add-cluster/add-cluster.tsx @@ -59,7 +59,7 @@ class NonInjectedAddCluster extends React.Component { } componentDidMount() { - appEventBus.emit({ name: "cluster-add", action: "start" }); + appEventBus.emit({ type: "APP_EVENT", name: "cluster-add", action: "start" }); } @computed get allErrors(): string[] { @@ -85,7 +85,7 @@ class NonInjectedAddCluster extends React.Component { addClusters = action(async () => { this.isWaiting = true; - appEventBus.emit({ name: "cluster-add", action: "click" }); + appEventBus.emit({ type: "APP_EVENT", name: "cluster-add", action: "click" }); try { const absPath = this.props.getCustomKubeConfigDirectory(uuid.v4()); diff --git a/src/renderer/components/+catalog/catalog.test.tsx b/src/renderer/components/+catalog/catalog.test.tsx index 9a481d9535..4754ce7264 100644 --- a/src/renderer/components/+catalog/catalog.test.tsx +++ b/src/renderer/components/+catalog/catalog.test.tsx @@ -286,6 +286,7 @@ describe("", () => { ); expect(emitEvent).toHaveBeenCalledWith( { + type: "APP_EVENT", action: "open", name: "catalog", }); @@ -299,6 +300,7 @@ describe("", () => { userEvent.click(screen.getByText("Web Links")); expect(emitEvent).toHaveBeenLastCalledWith({ + type: "APP_EVENT", action: "change-category", name: "catalog", params: { diff --git a/src/renderer/components/+catalog/catalog.tsx b/src/renderer/components/+catalog/catalog.tsx index 78456761a3..65f539844d 100644 --- a/src/renderer/components/+catalog/catalog.tsx +++ b/src/renderer/components/+catalog/catalog.tsx @@ -124,6 +124,7 @@ class NonInjectedCatalog extends React.Component { })); this.props.emitEvent({ + type: "APP_EVENT", name: "catalog", action: "open", }); @@ -170,6 +171,7 @@ class NonInjectedCatalog extends React.Component { const activeCategory = this.categories.find(category => category.getId() === tabId); this.props.emitEvent({ + type: "APP_EVENT", name: "catalog", action: "change-category", params: { diff --git a/src/renderer/frames/cluster-frame/init-cluster-frame/init-cluster-frame.ts b/src/renderer/frames/cluster-frame/init-cluster-frame/init-cluster-frame.ts index 4fe1081c02..4cf9b88e22 100644 --- a/src/renderer/frames/cluster-frame/init-cluster-frame/init-cluster-frame.ts +++ b/src/renderer/frames/cluster-frame/init-cluster-frame/init-cluster-frame.ts @@ -67,6 +67,7 @@ export const initClusterFrame = setTimeout(() => { emitEvent({ + type: "APP_EVENT", name: "cluster", action: "open", params: {