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 2021-05-28 19:45:39 +03:00
parent dae0be08f9
commit 92e2c354fd
2 changed files with 95 additions and 19 deletions

View File

@ -22,7 +22,8 @@
import { CatalogEntityRegistry } from "../catalog-entity-registry";
import "../../../common/catalog-entities";
import { catalogCategoryRegistry } from "../../../common/catalog/catalog-category-registry";
import type { CatalogEntityData, CatalogEntityKindData } from "../catalog-entity";
import { CatalogCategory, CatalogEntityData, CatalogEntityKindData } from "../catalog-entity";
import { WebLink } from "../../../common/catalog-entities";
class TestCatalogEntityRegistry extends CatalogEntityRegistry {
replaceItems(items: Array<CatalogEntityData & CatalogEntityKindData>) {
@ -30,6 +31,27 @@ class TestCatalogEntityRegistry extends CatalogEntityRegistry {
}
}
class FooBarCategory extends CatalogCategory {
public readonly apiVersion = "catalog.k8slens.dev/v1alpha1";
public readonly kind = "CatalogCategory";
public metadata = {
name: "FooBars",
icon: "broken"
};
public spec = {
group: "entity.k8slens.dev",
versions: [
{
name: "v1alpha1",
entityClass: WebLink
}
],
names: {
kind: "FooBar"
}
};
}
describe("CatalogEntityRegistry", () => {
describe("updateItems", () => {
it("adds new catalog item", () => {
@ -201,8 +223,31 @@ describe("CatalogEntityRegistry", () => {
];
catalog.replaceItems(items);
expect(catalog.items.length).toBe(1);
});
});
it("does return items after matching category is added", () => {
const catalog = new TestCatalogEntityRegistry(catalogCategoryRegistry);
const items = [
{
apiVersion: "entity.k8slens.dev/v1alpha1",
kind: "FooBar",
metadata: {
uid: "456",
name: "barbaz",
source: "test",
labels: {}
},
status: {
phase: "disconnected"
},
spec: {}
}
];
catalog.replaceItems(items);
catalogCategoryRegistry.add(new FooBarCategory());
expect(catalog.items.length).toBe(1);
});
});

View File

@ -27,8 +27,13 @@ import type { Cluster } from "../../main/cluster";
import { ClusterStore } from "../../common/cluster-store";
export class CatalogEntityRegistry {
protected _entities = observable.map<string, CatalogEntity>([], { deep: true });
@observable.ref activeEntity: CatalogEntity;
protected _entities = observable.map<string, CatalogEntity>([], { deep: true });
/**
* Buffer for keeping entities that don't yet have CatalogCategory synced
*/
protected rawEntities: (CatalogEntityData & CatalogEntityKindData)[] = [];
constructor(private categoryRegistry: CatalogCategoryRegistry) {
makeObservable(this);
@ -41,32 +46,58 @@ export class CatalogEntityRegistry {
}
@action updateItems(items: (CatalogEntityData & CatalogEntityKindData)[]) {
const newIds = items.map((item) => item.metadata.uid);
this.rawEntities.length = 0;
Array.from(this._entities.keys()).forEach((uid) => {
if (!newIds.includes(uid)) this._entities.delete(uid);
});
const newIds = new Set(items.map((item) => item.metadata.uid));
items.forEach((item) => {
const existing = this._entities.get(item.metadata.uid);
if (!existing) {
const entity = this.categoryRegistry.getEntityForData(item);
if (entity) this._entities.set(entity.metadata.uid, entity);
} else {
existing.metadata = item.metadata;
existing.spec = item.spec;
existing.status = item.status;
for (const uid of this._entities.keys()) {
if (!newIds.has(uid)) {
this._entities.delete(uid);
}
});
}
for (const item of items) {
this.updateItem(item);
}
}
@action protected updateItem(item: (CatalogEntityData & CatalogEntityKindData)) {
const existing = this._entities.get(item.metadata.uid);
if (!existing) {
const entity = this.categoryRegistry.getEntityForData(item);
if (entity) {
this._entities.set(entity.metadata.uid, entity);
} else {
this.rawEntities.push(item);
}
} else {
existing.metadata = item.metadata;
existing.spec = item.spec;
existing.status = item.status;
}
}
protected processRawEntities() {
const items = [...this.rawEntities];
this.rawEntities.length = 0;
for (const item of items) {
this.updateItem(item);
}
}
@computed get items() {
this.processRawEntities();
return Array.from(this._entities.values());
}
@computed get entities(): Map<string, CatalogEntity> {
this.processRawEntities();
return this._entities;
}