1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Add type to app event bus event.

Signed-off-by: Juho Heikka <juho.heikka@gmail.com>
This commit is contained in:
Juho Heikka 2022-04-11 15:21:01 +03:00
parent 06e7dfce5e
commit 07c73b20ff
16 changed files with 70 additions and 36 deletions

View File

@ -19,7 +19,7 @@ describe("event bus tests", () => {
event = data; event = data;
}); });
appEventBus.emit({ name: "foo", action: "bar" }); appEventBus.emit({ type: "APP_EVENT", name: "foo", action: "bar" });
expect(event.name).toBe("foo"); expect(event.name).toBe("foo");
}); });
}); });

View File

@ -5,10 +5,30 @@
import { EventEmitter } from "../event-emitter"; import { EventEmitter } from "../event-emitter";
export interface AppEvent { export interface Keyable { [key: string]: any }
export interface LensAppEvent {
type: "APP_EVENT";
name: string; name: string;
action: 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]>(); export const appEventBus = new EventEmitter<[AppEvent]>();

View File

@ -108,7 +108,7 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
} }
addCluster(clusterOrModel: ClusterModel | Cluster): Cluster { 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 const cluster = clusterOrModel instanceof Cluster
? clusterOrModel ? clusterOrModel

View File

@ -5,27 +5,36 @@
import { JsonApi } from "./json-api"; import { JsonApi } from "./json-api";
import { apiPrefix, isDebugging, isDevelopment } from "../vars"; import { apiPrefix, isDebugging, isDevelopment } from "../vars";
import type { AppEvent, LensProxyEvent } from "../app-event-bus/event-bus";
import { appEventBus } from "../app-event-bus/event-bus"; import { appEventBus } from "../app-event-bus/event-bus";
export let apiBase: JsonApi; export let apiBase: JsonApi;
const isProxyEvent = (event: AppEvent): event is LensProxyEvent => {
return event.type === "LENS_PROXY_EVENT";
};
if (typeof window === "undefined") { if (typeof window === "undefined") {
appEventBus.addListener((event) => { 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:${event.params.port}`,
apiBase = new JsonApi({ apiBase: apiPrefix,
serverAddress: `http://127.0.0.1:${params.port}`, debug: isDevelopment || isDebugging,
apiBase: apiPrefix, }, {
debug: isDevelopment || isDebugging, headers: {
}, { "Host": `localhost:${event.params.port}`,
headers: { },
"Host": `localhost:${params.port}`, });
}, }
});
}); });
} else { } else {
apiBase = new JsonApi({ apiBase = new JsonApi({

View File

@ -99,7 +99,7 @@ export class UserStore extends BaseStore<UserStoreModel> /* implements UserStore
startMainReactions() { startMainReactions() {
// track telemetry availability // track telemetry availability
reaction(() => this.allowTelemetry, allowed => { 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 // open at system start-up
@ -148,7 +148,7 @@ export class UserStore extends BaseStore<UserStoreModel> /* implements UserStore
@action @action
saveLastSeenAppVersion() { saveLastSeenAppVersion() {
appEventBus.emit({ name: "app", action: "whats-new-seen" }); appEventBus.emit({ type: "APP_EVENT", name: "app", action: "whats-new-seen" });
this.lastSeenAppVersion = getAppVersion(); this.lastSeenAppVersion = getAppVersion();
} }

View File

@ -13,7 +13,7 @@ export function exitApp() {
const windowManager = WindowManager.getInstance(false); const windowManager = WindowManager.getInstance(false);
const clusterManager = ClusterManager.getInstance(false); const clusterManager = ClusterManager.getInstance(false);
appEventBus.emit({ name: "service", action: "close" }); appEventBus.emit({ type: "APP_EVENT", name: "service", action: "close" });
windowManager?.hide(); windowManager?.hide();
clusterManager?.stop(); clusterManager?.stop();
logger.info("SERVICE:QUIT"); logger.info("SERVICE:QUIT");

View File

@ -157,7 +157,7 @@ async function main(di: DiContainer) {
logger.info("APP:QUIT"); 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 ClusterManager.getInstance(false)?.stop(); // close cluster connections
const kubeConfigSyncManager = di.inject(kubeconfigSyncManagerInjectable); const kubeConfigSyncManager = di.inject(kubeconfigSyncManagerInjectable);
@ -356,7 +356,7 @@ async function main(di: DiContainer) {
} }
setTimeout(() => { setTimeout(() => {
appEventBus.emit({ name: "service", action: "start" }); appEventBus.emit({ type: "APP_EVENT", name: "service", action: "start" });
}, 1000); }, 1000);
} }

View File

@ -61,7 +61,7 @@ export const initIpcMainHandlers = ({ applicationMenuItems, directoryForLensLoca
}); });
ipcMainHandle(clusterDisconnectHandler, (event, clusterId: ClusterId) => { 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); const cluster = ClusterStore.getInstance().getById(clusterId);
if (cluster) { if (cluster) {
@ -71,7 +71,7 @@ export const initIpcMainHandlers = ({ applicationMenuItems, directoryForLensLoca
}); });
ipcMainHandle(clusterDeleteHandler, async (event, clusterId: ClusterId) => { 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 clusterStore = ClusterStore.getInstance();
const cluster = clusterStore.getById(clusterId); const cluster = clusterStore.getById(clusterId);
@ -105,7 +105,7 @@ export const initIpcMainHandlers = ({ applicationMenuItems, directoryForLensLoca
}); });
ipcMainHandle(clusterKubectlApplyAllHandler, async (event, clusterId: ClusterId, resources: string[], extraArgs: string[]) => { 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); const cluster = ClusterStore.getInstance().getById(clusterId);
if (cluster) { if (cluster) {
@ -124,7 +124,7 @@ export const initIpcMainHandlers = ({ applicationMenuItems, directoryForLensLoca
}); });
ipcMainHandle(clusterKubectlDeleteAllHandler, async (event, clusterId: ClusterId, resources: string[], extraArgs: string[]) => { 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); const cluster = ClusterStore.getInstance().getById(clusterId);
if (cluster) { if (cluster) {

View File

@ -117,7 +117,7 @@ export class LensProxy extends Singleton {
}); });
this.port = port; 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); resolve(port);
}) })
.once("error", (error) => { .once("error", (error) => {

View File

@ -27,7 +27,7 @@ export class ResourceApplier {
* @param ns The optional namespace of the kube resource * @param ns The optional namespace of the kube resource
*/ */
async patch(name: string, kind: string, patch: Patch, ns?: string): Promise<string> { async patch(name: string, kind: string, patch: Patch, ns?: string): Promise<string> {
appEventBus.emit({ name: "resource", action: "patch" }); appEventBus.emit({ type: "APP_EVENT", name: "resource", action: "patch" });
const kubectl = await this.cluster.ensureKubectl(); const kubectl = await this.cluster.ensureKubectl();
const kubectlPath = await kubectl.getPath(); const kubectlPath = await kubectl.getPath();
@ -60,7 +60,7 @@ export class ResourceApplier {
async apply(resource: KubernetesObject | any): Promise<string> { async apply(resource: KubernetesObject | any): Promise<string> {
resource = this.sanitizeObject(resource); 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)); return this.kubectlApply(yaml.dump(resource));
} }

View File

@ -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[] { protected getPathEntries(): string[] {

View File

@ -81,10 +81,10 @@ export class WindowManager extends Singleton {
// open external links in default browser (target=_blank, window.open) // open external links in default browser (target=_blank, window.open)
this.mainWindow this.mainWindow
.on("focus", () => { .on("focus", () => {
appEventBus.emit({ name: "app", action: "focus" }); appEventBus.emit({ type: "APP_EVENT", name: "app", action: "focus" });
}) })
.on("blur", () => { .on("blur", () => {
appEventBus.emit({ name: "app", action: "blur" }); appEventBus.emit({ type: "APP_EVENT", name: "app", action: "blur" });
}) })
.on("closed", () => { .on("closed", () => {
// clean up // clean up
@ -95,7 +95,7 @@ export class WindowManager extends Singleton {
}) })
.webContents .webContents
.on("dom-ready", () => { .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) => { .on("did-fail-load", (_event, code, desc) => {
logger.error(`[WINDOW-MANAGER]: Failed to load Main window`, { 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?.close();
this.splashWindow = undefined; this.splashWindow = undefined;
setTimeout(() => { setTimeout(() => {
appEventBus.emit({ name: "app", action: "start" }); appEventBus.emit({ type: "APP_EVENT", name: "app", action: "start" });
}, 1000); }, 1000);
} catch (error) { } catch (error) {
logger.error(`Showing main window failed: ${error.stack || error}`); logger.error(`Showing main window failed: ${error.stack || error}`);

View File

@ -59,7 +59,7 @@ class NonInjectedAddCluster extends React.Component<Dependencies> {
} }
componentDidMount() { componentDidMount() {
appEventBus.emit({ name: "cluster-add", action: "start" }); appEventBus.emit({ type: "APP_EVENT", name: "cluster-add", action: "start" });
} }
@computed get allErrors(): string[] { @computed get allErrors(): string[] {
@ -85,7 +85,7 @@ class NonInjectedAddCluster extends React.Component<Dependencies> {
addClusters = action(async () => { addClusters = action(async () => {
this.isWaiting = true; this.isWaiting = true;
appEventBus.emit({ name: "cluster-add", action: "click" }); appEventBus.emit({ type: "APP_EVENT", name: "cluster-add", action: "click" });
try { try {
const absPath = this.props.getCustomKubeConfigDirectory(uuid.v4()); const absPath = this.props.getCustomKubeConfigDirectory(uuid.v4());

View File

@ -286,6 +286,7 @@ describe("<Catalog />", () => {
); );
expect(emitEvent).toHaveBeenCalledWith( { expect(emitEvent).toHaveBeenCalledWith( {
type: "APP_EVENT",
action: "open", action: "open",
name: "catalog", name: "catalog",
}); });
@ -299,6 +300,7 @@ describe("<Catalog />", () => {
userEvent.click(screen.getByText("Web Links")); userEvent.click(screen.getByText("Web Links"));
expect(emitEvent).toHaveBeenLastCalledWith({ expect(emitEvent).toHaveBeenLastCalledWith({
type: "APP_EVENT",
action: "change-category", action: "change-category",
name: "catalog", name: "catalog",
params: { params: {

View File

@ -124,6 +124,7 @@ class NonInjectedCatalog extends React.Component<Dependencies> {
})); }));
this.props.emitEvent({ this.props.emitEvent({
type: "APP_EVENT",
name: "catalog", name: "catalog",
action: "open", action: "open",
}); });
@ -170,6 +171,7 @@ class NonInjectedCatalog extends React.Component<Dependencies> {
const activeCategory = this.categories.find(category => category.getId() === tabId); const activeCategory = this.categories.find(category => category.getId() === tabId);
this.props.emitEvent({ this.props.emitEvent({
type: "APP_EVENT",
name: "catalog", name: "catalog",
action: "change-category", action: "change-category",
params: { params: {

View File

@ -67,6 +67,7 @@ export const initClusterFrame =
setTimeout(() => { setTimeout(() => {
emitEvent({ emitEvent({
type: "APP_EVENT",
name: "cluster", name: "cluster",
action: "open", action: "open",
params: { params: {