1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Remove getOrDefault, consolodate ExtendedMap

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

View File

@ -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<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) {
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;

View File

@ -21,13 +21,29 @@
import { action, IEnhancer, IObservableMapInitialValues, ObservableMap } from "mobx";
export class StrictMap<K, V> extends Map<K, V> {
static new<K, V>(): StrictMap<K, V> {
return new StrictMap();
export class ExtendedMap<K, V> extends Map<K, V> {
static new<K, V>(entries?: readonly (readonly [K, V])[] | null): ExtendedMap<K, V> {
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
* @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<K, V> extends Map<K, V> {
return this.set(key, val);
}
}
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);
/**
* 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);
}
}