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

clean up / make kubeWatchApi.init() as setter

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2021-01-29 14:40:25 +02:00
parent 219852dce1
commit e0e1b78840

View File

@ -8,7 +8,7 @@ import type { KubeObjectStore } from "../kube-object.store";
import plimit from "p-limit"; import plimit from "p-limit";
import debounce from "lodash/debounce"; import debounce from "lodash/debounce";
import { comparer, computed, observable, reaction } from "mobx"; import { autorun, comparer, computed, observable, reaction } from "mobx";
import { autobind, EventEmitter } from "../utils"; import { autobind, EventEmitter } from "../utils";
import { ensureObjectSelfLink, KubeApi, parseKubeApi } from "./kube-api"; import { ensureObjectSelfLink, KubeApi, parseKubeApi } from "./kube-api";
import { KubeJsonApiData, KubeJsonApiError } from "./kube-json-api"; import { KubeJsonApiData, KubeJsonApiError } from "./kube-json-api";
@ -46,21 +46,13 @@ export class KubeWatchApi {
private reader: ReadableStreamReader<string>; private reader: ReadableStreamReader<string>;
public onMessage = new EventEmitter<[IKubeWatchMessage]>(); public onMessage = new EventEmitter<[IKubeWatchMessage]>();
@observable.ref private getCluster: () => Cluster; @observable.ref private cluster: Cluster;
@observable.ref private getNamespaces: () => string[]; @observable.ref private namespaces: string[] = [];
@observable isConnected = false;
@observable subscribers = observable.map<KubeApi, number>(); @observable subscribers = observable.map<KubeApi, number>();
@observable isConnected = false;
@computed get isReady(): boolean { @computed get isReady(): boolean {
return !!(this.cluster && this.namespaces); return Boolean(this.cluster && this.namespaces);
}
@computed get cluster(): Cluster | undefined {
return this.getCluster?.();
}
@computed get namespaces(): string[] {
return this.getNamespaces?.() || [];
} }
@computed get isActive(): boolean { @computed get isActive(): boolean {
@ -68,8 +60,12 @@ export class KubeWatchApi {
} }
@computed get apis(): string[] { @computed get apis(): string[] {
if (!this.isReady) {
return [];
}
return Array.from(this.subscribers.keys()).map(api => { return Array.from(this.subscribers.keys()).map(api => {
if (!this.isReady || !this.isAllowedApi(api)) { if (!this.isAllowedApi(api)) {
return []; return [];
} }
@ -81,20 +77,14 @@ export class KubeWatchApi {
}).flat(); }).flat();
} }
setupClusterGetter(getter: () => Cluster) {
this.getCluster = getter;
}
setupActiveNamespacesGetter(getter: () => string[]) {
this.getNamespaces = getter;
}
async init({ getCluster, getNamespaces }: { async init({ getCluster, getNamespaces }: {
getCluster: () => Cluster, getCluster: () => Cluster,
getNamespaces: () => string[], getNamespaces: () => string[],
}): Promise<void> { }): Promise<void> {
this.getCluster = getCluster; autorun(() => {
this.getNamespaces = getNamespaces; this.cluster = getCluster();
this.namespaces = getNamespaces();
});
this.bindAutoConnect(); this.bindAutoConnect();
} }
@ -116,7 +106,7 @@ export class KubeWatchApi {
} }
isAllowedApi(api: KubeApi): boolean { isAllowedApi(api: KubeApi): boolean {
return !!this?.cluster?.isAllowedResource(api.kind); return Boolean(this?.cluster.isAllowedResource(api.kind));
} }
subscribeApi(api: KubeApi | KubeApi[]): () => void { subscribeApi(api: KubeApi | KubeApi[]): () => void {