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

Optional destination property

Signed-off-by: Juho Heikka <juho.heikka@gmail.com>
This commit is contained in:
Juho Heikka 2022-04-12 18:29:26 +03:00
parent 07c73b20ff
commit dc82972327
16 changed files with 41 additions and 70 deletions

View File

@ -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");
});
});

View File

@ -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]>();

View File

@ -108,7 +108,7 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
}
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

View File

@ -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({

View File

@ -99,7 +99,7 @@ export class UserStore extends BaseStore<UserStoreModel> /* 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<UserStoreModel> /* 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();
}

View File

@ -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");

View File

@ -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);
}

View File

@ -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) {

View File

@ -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) => {

View File

@ -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({ 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<string> {
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));
}

View File

@ -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[] {

View File

@ -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}`);

View File

@ -59,7 +59,7 @@ class NonInjectedAddCluster extends React.Component<Dependencies> {
}
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<Dependencies> {
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());

View File

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

View File

@ -124,7 +124,6 @@ class NonInjectedCatalog extends React.Component<Dependencies> {
}));
this.props.emitEvent({
type: "APP_EVENT",
name: "catalog",
action: "open",
});
@ -171,7 +170,6 @@ 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: {

View File

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