diff --git a/src/common/catalog/catalog-category-registry.ts b/src/common/catalog/catalog-category-registry.ts index 68e3e811b8..a0319d773b 100644 --- a/src/common/catalog/catalog-category-registry.ts +++ b/src/common/catalog/catalog-category-registry.ts @@ -20,7 +20,7 @@ */ import { action, computed, observable } from "mobx"; -import { Disposer, ExtendedMap, StrictMap } from "../utils"; +import { Disposer, ExtendedMap } from "../utils"; import { CatalogCategory, CatalogEntityData, CatalogEntityKindData } from "./catalog-entity"; export class CatalogCategoryRegistry { @@ -33,10 +33,13 @@ export class CatalogCategoryRegistry { } @computed private get groupKindLookup(): Map> { - const res = new ExtendedMap>(StrictMap.new); + // ExtendedMap has the convenience methods `getOrInsert` and `strictSet` + const res = new ExtendedMap>(); for (const category of this.categories) { - res.getOrDefault(category.spec.group).strictSet(category.spec.names.kind, category); + res + .getOrInsert(category.spec.group, ExtendedMap.new) + .strictSet(category.spec.names.kind, category); } return res; diff --git a/src/common/utils/extended-map.ts b/src/common/utils/extended-map.ts index 460e716f1c..c8b7ff4c65 100644 --- a/src/common/utils/extended-map.ts +++ b/src/common/utils/extended-map.ts @@ -21,13 +21,29 @@ import { action, IEnhancer, IObservableMapInitialValues, ObservableMap } from "mobx"; -export class StrictMap extends Map { - static new(): StrictMap { - return new StrictMap(); +export class ExtendedMap extends Map { + static new(entries?: readonly (readonly [K, V])[] | null): ExtendedMap { + return new ExtendedMap(entries); } /** + * Get the value behind `key`. If it was not pressent, first insert the value returned by `getVal` + * @param key The key to insert into the map with + * @param getVal A function that returns a new instance of `V`. + * @returns The value in the map + */ + getOrInsert(key: K, getVal: () => V): V { + if (this.has(key)) { + return this.get(key); + } + + return this.set(key, getVal()).get(key); + } + + /** + * Set the value associated with `key` iff there was not a previous value * @throws if `key` already in map + * @returns `this` so that `strictSet` can be chained */ strictSet(key: K, val: V): this { if (this.has(key)) { @@ -36,39 +52,17 @@ export class StrictMap extends Map { return this.set(key, val); } -} -export class ExtendedMap extends Map { - static new(): ExtendedMap> { - return new ExtendedMap>(() => new Map()); - } - - constructor(protected getDefault: () => V, entries?: readonly (readonly [K, V])[] | null) { - super(entries); - } - - getOrInsert(key: K, val: V): V { - if (this.has(key)) { - return this.get(key); + /** + * Get the value associated with `key` + * @throws if `key` did not a value associated with it + */ + strictGet(key: K): V { + if (!this.has(key)) { + throw new TypeError("key not in map"); } - return this.set(key, val).get(key); - } - - getOrInsertWith(key: K, getVal: () => V): V { - if (this.has(key)) { - return this.get(key); - } - - return this.set(key, getVal()).get(key); - } - - getOrDefault(key: K): V { - if (this.has(key)) { - return this.get(key); - } - - return this.set(key, this.getDefault()).get(key); + return this.get(key); } }