Filter out unknown catalog entities (#2816)

* Filter out unknown catelog entities

- Keep raw data

- But filter unknown types until a category is registered

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* fix unit tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* simplify tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Remove getOrDefault, consolodate ExtendedMap

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton authored and GitHub committed 2021-05-20 08:50:51 +03:00
1 parent 5ed4537979
commit b61ba7ef71
4 files changed
+71 -49

No files matched your search

@@ -42,7 +42,7 @@ describe("CatalogEntityRegistry", () => {
spec: {}
}];
catalog.updateItems(items);
(catalog as any).rawItems.replace(items);
expect(catalog.items.length).toEqual(1);
items.push({
@@ -60,7 +60,7 @@ describe("CatalogEntityRegistry", () => {
spec: {}
});
catalog.updateItems(items);
(catalog as any).rawItems.replace(items);
expect(catalog.items.length).toEqual(2);
});
@@ -81,13 +81,13 @@ describe("CatalogEntityRegistry", () => {
spec: {}
}];
catalog.updateItems(items);
(catalog as any).rawItems.replace(items);
expect(catalog.items.length).toEqual(1);
expect(catalog.items[0].status.phase).toEqual("disconnected");
items[0].status.phase = "connected";
catalog.updateItems(items);
(catalog as any).rawItems.replace(items);
expect(catalog.items.length).toEqual(1);
expect(catalog.items[0].status.phase).toEqual("connected");
});
@@ -125,9 +125,9 @@ describe("CatalogEntityRegistry", () => {
}
];
catalog.updateItems(items);
(catalog as any).rawItems.replace(items);
items.splice(0, 1);
catalog.updateItems(items);
(catalog as any).rawItems.replace(items);
expect(catalog.items.length).toEqual(1);
expect(catalog.items[0].metadata.uid).toEqual("456");
});
+13 -12
View File
@@ -19,28 +19,25 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { action, observable } from "mobx";
import { computed, observable } from "mobx";
import { broadcastMessage, subscribeToBroadcast } from "../../common/ipc";
import { CatalogCategory, CatalogEntity, CatalogEntityData, catalogCategoryRegistry, CatalogCategoryRegistry, CatalogEntityKindData } from "../../common/catalog";
import "../../common/catalog-entities";
import { iter } from "../utils";
export class CatalogEntityRegistry {
@observable protected _items: CatalogEntity[] = observable.array([], { deep: true });
protected rawItems = observable.array<CatalogEntityData & CatalogEntityKindData>([], { deep: true });
@observable protected _activeEntity: CatalogEntity;
constructor(private categoryRegistry: CatalogCategoryRegistry) {}
init() {
subscribeToBroadcast("catalog:items", (ev, items: (CatalogEntityData & CatalogEntityKindData)[]) => {
this.updateItems(items);
this.rawItems.replace(items);
});
broadcastMessage("catalog:broadcast");
}
@action updateItems(items: (CatalogEntityData & CatalogEntityKindData)[]) {
this._items = items.map(data => this.categoryRegistry.getEntityForData(data));
}
set activeEntity(entity: CatalogEntity) {
this._activeEntity = entity;
}
@@ -49,23 +46,27 @@ export class CatalogEntityRegistry {
return this._activeEntity;
}
get items() {
return this._items;
@computed get items() {
return Array.from(iter.filterMap(this.rawItems, rawItem => this.categoryRegistry.getEntityForData(rawItem)));
}
@computed get entities(): Map<string, CatalogEntity> {
return new Map(this.items.map(item => [item.metadata.uid, item]));
}
getById(id: string) {
return this._items.find((entity) => entity.metadata.uid === id);
return this.entities.get(id);
}
getItemsForApiKind<T extends CatalogEntity>(apiVersion: string, kind: string): T[] {
const items = this._items.filter((item) => item.apiVersion === apiVersion && item.kind === kind);
const items = this.items.filter((item) => item.apiVersion === apiVersion && item.kind === kind);
return items as T[];
}
getItemsForCategory<T extends CatalogEntity>(category: CatalogCategory): T[] {
const supportedVersions = category.spec.versions.map((v) => `${category.spec.group}/${v.name}`);
const items = this._items.filter((item) => supportedVersions.includes(item.apiVersion) && item.kind === category.spec.names.kind);
const items = this.items.filter((item) => supportedVersions.includes(item.apiVersion) && item.kind === category.spec.names.kind);
return items as T[];
}