import { action, computed, observable, IComputedValue, IObservableArray, makeObservable, } from "mobx"; import { CatalogEntity } from "./catalog-entity"; import { iter } from "../utils"; export class CatalogEntityRegistry { protected sources = observable.map>([], { deep: true }); constructor() { makeObservable(this); } @action addObservableSource(id: string, source: IObservableArray) { this.sources.set(id, computed(() => source)); } @action addComputedSource(id: string, source: IComputedValue) { this.sources.set(id, source); } @action removeSource(id: string) { this.sources.delete(id); } @computed get items(): CatalogEntity[] { return Array.from(iter.flatMap(this.sources.values(), source => source.get())); } getItemsForApiKind(apiVersion: string, kind: string): T[] { const items = this.items.filter((item) => item.apiVersion === apiVersion && item.kind === kind); return items as T[]; } } export const catalogEntityRegistry = new CatalogEntityRegistry();