Remove getOrDefault, consolodate ExtendedMap

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton committed 2021-05-19 14:01:46 -04:00
1 parent 4ae7eae81c
commit 0e14729e32
2 files changed
+33 -36

No files matched your search

@@ -20,7 +20,7 @@
*/ */
import { action, computed, observable } from "mobx"; import { action, computed, observable } from "mobx";
import { Disposer, ExtendedMap, StrictMap } from "../utils"; import { Disposer, ExtendedMap } from "../utils";
import { CatalogCategory, CatalogEntityData, CatalogEntityKindData } from "./catalog-entity"; import { CatalogCategory, CatalogEntityData, CatalogEntityKindData } from "./catalog-entity";
export class CatalogCategoryRegistry { export class CatalogCategoryRegistry {
@@ -33,10 +33,13 @@ export class CatalogCategoryRegistry {
} }
@computed private get groupKindLookup(): Map<string, Map<string, CatalogCategory>> { @computed private get groupKindLookup(): Map<string, Map<string, CatalogCategory>> {
const res = new ExtendedMap<string, StrictMap<string, CatalogCategory>>(StrictMap.new); // ExtendedMap has the convenience methods `getOrInsert` and `strictSet`
const res = new ExtendedMap<string, ExtendedMap<string, CatalogCategory>>();
for (const category of this.categories) { 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; return res;
+27 -33
View File
@@ -21,13 +21,29 @@
import { action, IEnhancer, IObservableMapInitialValues, ObservableMap } from "mobx"; import { action, IEnhancer, IObservableMapInitialValues, ObservableMap } from "mobx";
export class StrictMap<K, V> extends Map<K, V> { export class ExtendedMap<K, V> extends Map<K, V> {
static new<K, V>(): StrictMap<K, V> { static new<K, V>(entries?: readonly (readonly [K, V])[] | null): ExtendedMap<K, V> {
return new StrictMap(); return new ExtendedMap<K, V>(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 * @throws if `key` already in map
* @returns `this` so that `strictSet` can be chained
*/ */
strictSet(key: K, val: V): this { strictSet(key: K, val: V): this {
if (this.has(key)) { if (this.has(key)) {
@@ -36,40 +52,18 @@ export class StrictMap<K, V> extends Map<K, V> {
return this.set(key, val); return this.set(key, val);
} }
/**
* 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");
} }
export class ExtendedMap<K, V> extends Map<K, V> {
static new<K, MK, MV>(): ExtendedMap<K, Map<MK, MV>> {
return new ExtendedMap<K, Map<MK, MV>>(() => new Map<MK, MV>());
}
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); return this.get(key);
} }
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);
}
} }
export class ExtendedObservableMap<K, V> extends ObservableMap<K, V> { export class ExtendedObservableMap<K, V> extends ObservableMap<K, V> {