1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen 2022-03-03 12:43:33 +02:00
parent 44cd91053e
commit 7cb16f7622

View File

@ -11,16 +11,16 @@ import type { KubeApi } from "./kube-api";
import type { KubeObject } from "./kube-object"; import type { KubeObject } from "./kube-object";
import { IKubeObjectRef, parseKubeApi, createKubeApiURL } from "./kube-api-parse"; import { IKubeObjectRef, parseKubeApi, createKubeApiURL } from "./kube-api-parse";
export class ApiManager { export class ApiManager<TKubeObject extends KubeObject = any> {
private apis = observable.map<string, KubeApi<any>>(); private apis = observable.map<string, KubeApi<TKubeObject>>();
private stores = observable.map<string, KubeObjectStore<any>>(); private stores = observable.map<string, KubeObjectStore<TKubeObject>>();
constructor() { constructor() {
makeObservable(this); makeObservable(this);
autoBind(this); autoBind(this);
} }
getApi(pathOrCallback: string | ((api: KubeApi<any>) => boolean)) { getApi(pathOrCallback: string | ((api: KubeApi<TKubeObject>) => boolean)) {
if (typeof pathOrCallback === "string") { if (typeof pathOrCallback === "string") {
return this.apis.get(pathOrCallback) || this.apis.get(parseKubeApi(pathOrCallback).apiBase); return this.apis.get(pathOrCallback) || this.apis.get(parseKubeApi(pathOrCallback).apiBase);
} }
@ -32,7 +32,7 @@ export class ApiManager {
return iter.find(this.apis.values(), api => api.kind === kind && api.apiVersionWithGroup === apiVersion); return iter.find(this.apis.values(), api => api.kind === kind && api.apiVersionWithGroup === apiVersion);
} }
registerApi(apiBase: string, api: KubeApi<any>) { registerApi(apiBase: string, api: KubeApi<TKubeObject>) {
if (!api.apiBase) return; if (!api.apiBase) return;
if (!this.apis.has(apiBase)) { if (!this.apis.has(apiBase)) {
@ -46,13 +46,13 @@ export class ApiManager {
} }
} }
protected resolveApi<K extends KubeObject>(api?: string | KubeApi<K>): KubeApi<K> | undefined { protected resolveApi(api?: string | KubeApi<TKubeObject>): KubeApi<TKubeObject> | undefined {
if (!api) { if (!api) {
return undefined; return undefined;
} }
if (typeof api === "string") { if (typeof api === "string") {
return this.getApi(api) as KubeApi<K>; return this.getApi(api) as KubeApi<TKubeObject>;
} }
return api; return api;
@ -75,7 +75,7 @@ export class ApiManager {
}); });
} }
getStore<S extends KubeObjectStore<any>>(api: string | KubeApi<KubeObject>): S | undefined { getStore<S extends KubeObjectStore<TKubeObject>>(api: string | KubeApi<TKubeObject>): S | undefined {
return this.stores.get(this.resolveApi(api)?.apiBase) as S; return this.stores.get(this.resolveApi(api)?.apiBase) as S;
} }