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

review comments

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-06-03 09:44:48 -04:00
parent a3073cc99b
commit 97f2ecd88d
3 changed files with 25 additions and 25 deletions

View File

@ -82,7 +82,7 @@ export class ApiManager {
}); });
} }
getStore<S extends KubeObjectStore<KubeObject>>(api: string | KubeApi<KubeObject>): S { getStore<S extends KubeObjectStore<KubeObject>>(api: string | KubeApi<KubeObject>): S | undefined {
return this.stores.get(this.resolveApi(api)?.apiBase) as S; return this.stores.get(this.resolveApi(api)?.apiBase) as S;
} }
} }

View File

@ -171,7 +171,7 @@ export class Table<Item> extends React.Component<TableProps<Item>> {
}); });
} }
getContent() { private getContent() {
const { items, renderRow, children } = this.props; const { items, renderRow, children } = this.props;
const content = React.Children.toArray(children) as (TableRowElem | TableHeadElem)[]; const content = React.Children.toArray(children) as (TableRowElem | TableHeadElem)[];

View File

@ -37,10 +37,10 @@ export interface KubeObjectStoreLoadingParams<K extends KubeObject> {
reqInit?: RequestInit; reqInit?: RequestInit;
} }
export abstract class KubeObjectStore<K extends KubeObject> extends ItemStore<K> { export abstract class KubeObjectStore<T extends KubeObject> extends ItemStore<T> {
static defaultContext = observable.box<ClusterContext>(); // TODO: support multiple cluster contexts static defaultContext = observable.box<ClusterContext>(); // TODO: support multiple cluster contexts
abstract api: KubeApi<K>; abstract api: KubeApi<T>;
public readonly limit?: number; public readonly limit?: number;
public readonly bufferSize: number = 50000; public readonly bufferSize: number = 50000;
@observable private loadedNamespaces?: string[]; @observable private loadedNamespaces?: string[];
@ -64,7 +64,7 @@ export abstract class KubeObjectStore<K extends KubeObject> extends ItemStore<K>
return KubeObjectStore.defaultContext.get(); return KubeObjectStore.defaultContext.get();
} }
@computed get contextItems(): K[] { @computed get contextItems(): T[] {
const namespaces = this.context?.contextNamespaces ?? []; const namespaces = this.context?.contextNamespaces ?? [];
return this.items.filter(item => { return this.items.filter(item => {
@ -88,9 +88,9 @@ export abstract class KubeObjectStore<K extends KubeObject> extends ItemStore<K>
return { limit }; return { limit };
} }
getStatuses?(items: K[]): Record<string, number>; getStatuses?(items: T[]): Record<string, number>;
getAllByNs(namespace: string | string[], strict = false): K[] { getAllByNs(namespace: string | string[], strict = false): T[] {
const namespaces: string[] = [].concat(namespace); const namespaces: string[] = [].concat(namespace);
if (namespaces.length) { if (namespaces.length) {
@ -108,7 +108,7 @@ export abstract class KubeObjectStore<K extends KubeObject> extends ItemStore<K>
return this.items.find(item => item.getId() === id); return this.items.find(item => item.getId() === id);
} }
getByName(name: string, namespace?: string): K { getByName(name: string, namespace?: string): T {
return this.items.find(item => { return this.items.find(item => {
return item.getName() === name && ( return item.getName() === name && (
namespace ? item.getNs() === namespace : true namespace ? item.getNs() === namespace : true
@ -116,19 +116,19 @@ export abstract class KubeObjectStore<K extends KubeObject> extends ItemStore<K>
}); });
} }
getByPath(path: string): K { getByPath(path: string): T {
return this.items.find(item => item.selfLink === path); return this.items.find(item => item.selfLink === path);
} }
getByLabel(labels: string[] | { [label: string]: string }): K[] { getByLabel(labels: string[] | { [label: string]: string }): T[] {
if (Array.isArray(labels)) { if (Array.isArray(labels)) {
return this.items.filter((item: K) => { return this.items.filter((item: T) => {
const itemLabels = item.getLabels(); const itemLabels = item.getLabels();
return labels.every(label => itemLabels.includes(label)); return labels.every(label => itemLabels.includes(label));
}); });
} else { } else {
return this.items.filter((item: K) => { return this.items.filter((item: T) => {
const itemLabels = item.metadata.labels || {}; const itemLabels = item.metadata.labels || {};
return Object.entries(labels) return Object.entries(labels)
@ -137,7 +137,7 @@ export abstract class KubeObjectStore<K extends KubeObject> extends ItemStore<K>
} }
} }
protected async loadItems({ namespaces, api, reqInit }: KubeObjectStoreLoadingParams<K>): Promise<K[]> { protected async loadItems({ namespaces, api, reqInit }: KubeObjectStoreLoadingParams<T>): Promise<T[]> {
if (this.context?.cluster.isAllowedResource(api.kind)) { if (this.context?.cluster.isAllowedResource(api.kind)) {
if (!api.isNamespaced) { if (!api.isNamespaced) {
return api.list({ reqInit }, this.query); return api.list({ reqInit }, this.query);
@ -163,12 +163,12 @@ export abstract class KubeObjectStore<K extends KubeObject> extends ItemStore<K>
return []; return [];
} }
protected filterItemsOnLoad(items: K[]) { protected filterItemsOnLoad(items: T[]) {
return items; return items;
} }
@action @action
async loadAll(options: { namespaces?: string[], merge?: boolean, reqInit?: RequestInit } = {}): Promise<void | K[]> { async loadAll(options: { namespaces?: string[], merge?: boolean, reqInit?: RequestInit } = {}): Promise<void | T[]> {
await this.contextReady; await this.contextReady;
this.isLoading = true; this.isLoading = true;
@ -215,7 +215,7 @@ export abstract class KubeObjectStore<K extends KubeObject> extends ItemStore<K>
} }
@action @action
protected mergeItems(partialItems: K[], { replace = false, updateStore = true, sort = true, filter = true } = {}): K[] { protected mergeItems(partialItems: T[], { replace = false, updateStore = true, sort = true, filter = true } = {}): T[] {
let items = partialItems; let items = partialItems;
// update existing items // update existing items
@ -239,12 +239,12 @@ export abstract class KubeObjectStore<K extends KubeObject> extends ItemStore<K>
if (error) this.reset(); if (error) this.reset();
} }
protected async loadItem(params: { name: string; namespace?: string }): Promise<K> { protected async loadItem(params: { name: string; namespace?: string }): Promise<T> {
return this.api.get(params); return this.api.get(params);
} }
@action @action
async load(params: { name: string; namespace?: string }): Promise<K> { async load(params: { name: string; namespace?: string }): Promise<T> {
const { name, namespace } = params; const { name, namespace } = params;
let item = this.getByName(name, namespace); let item = this.getByName(name, namespace);
@ -265,11 +265,11 @@ export abstract class KubeObjectStore<K extends KubeObject> extends ItemStore<K>
return this.load({ name, namespace }); return this.load({ name, namespace });
} }
protected async createItem(params: { name: string; namespace?: string }, data?: Partial<K>): Promise<K> { protected async createItem(params: { name: string; namespace?: string }, data?: Partial<T>): Promise<T> {
return this.api.create(params, data); return this.api.create(params, data);
} }
async create(params: { name: string; namespace?: string }, data?: Partial<K>): Promise<K> { async create(params: { name: string; namespace?: string }, data?: Partial<T>): Promise<T> {
const newItem = await this.createItem(params, data); const newItem = await this.createItem(params, data);
const items = this.sortItems([...this.items, newItem]); const items = this.sortItems([...this.items, newItem]);
@ -278,7 +278,7 @@ export abstract class KubeObjectStore<K extends KubeObject> extends ItemStore<K>
return newItem; return newItem;
} }
async update(item: K, data: Partial<K>): Promise<K> { async update(item: T, data: Partial<T>): Promise<T> {
const newItem = await item.update(data); const newItem = await item.update(data);
ensureObjectSelfLink(this.api, newItem); ensureObjectSelfLink(this.api, newItem);
@ -290,7 +290,7 @@ export abstract class KubeObjectStore<K extends KubeObject> extends ItemStore<K>
return newItem; return newItem;
} }
async remove(item: K) { async remove(item: T) {
await item.delete(); await item.delete();
this.items.remove(item); this.items.remove(item);
this.selectedItemsIds.delete(item.getId()); this.selectedItemsIds.delete(item.getId());
@ -352,7 +352,7 @@ export abstract class KubeObjectStore<K extends KubeObject> extends ItemStore<K>
}; };
} }
private watchNamespace(api: KubeApi<K>, namespace: string, abortController: AbortController) { private watchNamespace(api: KubeApi<T>, namespace: string, abortController: AbortController) {
let timedRetry: NodeJS.Timeout; let timedRetry: NodeJS.Timeout;
const watch = () => api.watch({ const watch = () => api.watch({
namespace, namespace,
@ -362,7 +362,7 @@ export abstract class KubeObjectStore<K extends KubeObject> extends ItemStore<K>
const { signal } = abortController; const { signal } = abortController;
const callback = (data: IKubeWatchEvent<K>, error: any) => { const callback = (data: IKubeWatchEvent<T>, error: any) => {
if (!this.isLoaded || error instanceof DOMException) return; if (!this.isLoaded || error instanceof DOMException) return;
if (error instanceof Response) { if (error instanceof Response) {
@ -410,7 +410,7 @@ export abstract class KubeObjectStore<K extends KubeObject> extends ItemStore<K>
switch (type) { switch (type) {
case "ADDED": case "ADDED":
case "MODIFIED": case "MODIFIED":
const newItem = new api.objectConstructor(object) as K; const newItem = new api.objectConstructor(object) as T;
if (!item) { if (!item) {
items.push(newItem); items.push(newItem);