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:
parent
06e7dfce5e
commit
07c73b20ff
@ -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");
|
||||
});
|
||||
});
|
||||
|
||||
@ -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]>();
|
||||
|
||||
@ -108,7 +108,7 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@ -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({
|
||||
|
||||
@ -99,7 +99,7 @@ export class UserStore extends BaseStore<UserStoreModel> /* 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<UserStoreModel> /* 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();
|
||||
}
|
||||
|
||||
|
||||
@ -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");
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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) => {
|
||||
|
||||
@ -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<string> {
|
||||
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<string> {
|
||||
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));
|
||||
}
|
||||
|
||||
@ -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[] {
|
||||
|
||||
@ -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}`);
|
||||
|
||||
@ -59,7 +59,7 @@ class NonInjectedAddCluster extends React.Component<Dependencies> {
|
||||
}
|
||||
|
||||
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<Dependencies> {
|
||||
|
||||
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());
|
||||
|
||||
@ -286,6 +286,7 @@ describe("<Catalog />", () => {
|
||||
);
|
||||
|
||||
expect(emitEvent).toHaveBeenCalledWith( {
|
||||
type: "APP_EVENT",
|
||||
action: "open",
|
||||
name: "catalog",
|
||||
});
|
||||
@ -299,6 +300,7 @@ describe("<Catalog />", () => {
|
||||
userEvent.click(screen.getByText("Web Links"));
|
||||
|
||||
expect(emitEvent).toHaveBeenLastCalledWith({
|
||||
type: "APP_EVENT",
|
||||
action: "change-category",
|
||||
name: "catalog",
|
||||
params: {
|
||||
|
||||
@ -124,6 +124,7 @@ class NonInjectedCatalog extends React.Component<Dependencies> {
|
||||
}));
|
||||
|
||||
this.props.emitEvent({
|
||||
type: "APP_EVENT",
|
||||
name: "catalog",
|
||||
action: "open",
|
||||
});
|
||||
@ -170,6 +171,7 @@ class NonInjectedCatalog extends React.Component<Dependencies> {
|
||||
const activeCategory = this.categories.find(category => category.getId() === tabId);
|
||||
|
||||
this.props.emitEvent({
|
||||
type: "APP_EVENT",
|
||||
name: "catalog",
|
||||
action: "change-category",
|
||||
params: {
|
||||
|
||||
@ -67,6 +67,7 @@ export const initClusterFrame =
|
||||
|
||||
setTimeout(() => {
|
||||
emitEvent({
|
||||
type: "APP_EVENT",
|
||||
name: "cluster",
|
||||
action: "open",
|
||||
params: {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user