diff --git a/src/common/__tests__/event-bus.test.ts b/src/common/__tests__/event-bus.test.ts index efd76ace4a..e7165aac92 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({ type: "APP_EVENT", name: "foo", action: "bar" }); + appEventBus.emit({ 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 fc481be0b9..c66397f801 100644 --- a/src/common/app-event-bus/event-bus.ts +++ b/src/common/app-event-bus/event-bus.ts @@ -5,30 +5,15 @@ import { EventEmitter } from "../event-emitter"; -export interface Keyable { [key: string]: any } -export interface LensAppEvent { - type: "APP_EVENT"; +interface AppEventParams { + [key: string]: any; +} + +export interface AppEvent { name: string; action: string; - params?: Keyable; + destination?: string; + params?: AppEventParams; } -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 bed3259615..901f340bda 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({ type: "APP_EVENT", name: "cluster", action: "add" }); + appEventBus.emit({ 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 999e0b313f..e511dd8454 100644 --- a/src/common/k8s-api/api-base.ts +++ b/src/common/k8s-api/api-base.ts @@ -5,36 +5,27 @@ 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 (isProxyEvent(event)) { - if(event.name !== "lens-proxy" && event.action !== "listen") { - return; - } + if (event.name !== "lens-proxy" && event.action !== "listen") return; - if (!event.params.port) { - return; - } + const params = event.params as { port?: number }; - apiBase = new JsonApi({ - serverAddress: `http://127.0.0.1:${event.params.port}`, - apiBase: apiPrefix, - debug: isDevelopment || isDebugging, - }, { - headers: { - "Host": `localhost:${event.params.port}`, - }, - }); - } + 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}`, + }, + }); }); } else { apiBase = new JsonApi({ diff --git a/src/common/user-store/user-store.ts b/src/common/user-store/user-store.ts index 8da98e6d6d..a948def9d8 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({ type: "APP_EVENT", name: "telemetry", action: allowed ? "enabled" : "disabled" }); + appEventBus.emit({ 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({ type: "APP_EVENT", name: "app", action: "whats-new-seen" }); + appEventBus.emit({ name: "app", action: "whats-new-seen" }); this.lastSeenAppVersion = getAppVersion(); } diff --git a/src/main/exit-app.ts b/src/main/exit-app.ts index 25583a8f57..c9a5bc5bf1 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({ type: "APP_EVENT", name: "service", action: "close" }); + appEventBus.emit({ 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 5745db0200..4162b30bcd 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({ type: "APP_EVENT", name: "app", action: "close" }); + appEventBus.emit({ 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({ type: "APP_EVENT", name: "service", action: "start" }); + appEventBus.emit({ 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 51222666ba..1a657b78be 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({ type: "APP_EVENT", name: "cluster", action: "stop" }); + appEventBus.emit({ 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({ type: "APP_EVENT", name: "cluster", action: "remove" }); + appEventBus.emit({ 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({ type: "APP_EVENT", name: "cluster", action: "kubectl-apply-all" }); + appEventBus.emit({ 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({ type: "APP_EVENT", name: "cluster", action: "kubectl-delete-all" }); + appEventBus.emit({ 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 f7970275fa..f37b7a087c 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({ type: "APP_EVENT", name: "lens-proxy", action: "listen", params: { port }}); + appEventBus.emit({ 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 c445c8a7f8..11c26ad171 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({ type: "APP_EVENT", name: "resource", action: "patch" }); + appEventBus.emit({ 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({ type: "APP_EVENT", name: "resource", action: "apply" }); + appEventBus.emit({ 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 43e36186cc..831c24bc02 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({ type: "APP_EVENT", name: this.ShellType, action: "open" }); + appEventBus.emit({ name: this.ShellType, action: "open" }); } protected getPathEntries(): string[] { diff --git a/src/main/window-manager.ts b/src/main/window-manager.ts index 805e5c8964..260a82ccc0 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({ type: "APP_EVENT", name: "app", action: "focus" }); + appEventBus.emit({ name: "app", action: "focus" }); }) .on("blur", () => { - appEventBus.emit({ type: "APP_EVENT", name: "app", action: "blur" }); + appEventBus.emit({ name: "app", action: "blur" }); }) .on("closed", () => { // clean up @@ -95,7 +95,7 @@ export class WindowManager extends Singleton { }) .webContents .on("dom-ready", () => { - appEventBus.emit({ type: "APP_EVENT", name: "app", action: "dom-ready" }); + appEventBus.emit({ 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({ type: "APP_EVENT", name: "app", action: "start" }); + appEventBus.emit({ 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 5ee6d36a82..ce53440966 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({ type: "APP_EVENT", name: "cluster-add", action: "start" }); + appEventBus.emit({ 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({ type: "APP_EVENT", name: "cluster-add", action: "click" }); + appEventBus.emit({ 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 4754ce7264..9a481d9535 100644 --- a/src/renderer/components/+catalog/catalog.test.tsx +++ b/src/renderer/components/+catalog/catalog.test.tsx @@ -286,7 +286,6 @@ describe("", () => { ); expect(emitEvent).toHaveBeenCalledWith( { - type: "APP_EVENT", action: "open", name: "catalog", }); @@ -300,7 +299,6 @@ 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 65f539844d..78456761a3 100644 --- a/src/renderer/components/+catalog/catalog.tsx +++ b/src/renderer/components/+catalog/catalog.tsx @@ -124,7 +124,6 @@ class NonInjectedCatalog extends React.Component { })); this.props.emitEvent({ - type: "APP_EVENT", name: "catalog", action: "open", }); @@ -171,7 +170,6 @@ 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 4cf9b88e22..4fe1081c02 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,7 +67,6 @@ export const initClusterFrame = setTimeout(() => { emitEvent({ - type: "APP_EVENT", name: "cluster", action: "open", params: {