1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/common/utils/collection-functions.ts
Sebastian Malton bc14522005 Fix pod logs dock tab
- Move all dependencies into a transient LogsViewModel

- Remove dependencies on dockStore.selectedTab

- Make all bindings as late as possible, as per mobx rules

Signed-off-by: Sebastian Malton <sebastian@malton.name>
2022-01-24 10:06:56 -05:00

39 lines
1.0 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
/**
* Get the value behind `key`. If it was not present, first insert `value`
* @param map The map to interact with
* @param key The key to insert into the map with
* @param value The value to optional add to the map
* @returns The value in the map
*/
export function getOrInsert<K, V>(map: Map<K, V>, key: K, value: V): V {
if (!map.has(key)) {
map.set(key, value);
}
return map.get(key);
}
/**
* Like `getOrInsert` but specifically for when `V` is `Map<any, any>` so that
* the typings are inferred.
*/
export function getOrInsertMap<K, MK, MV>(map: Map<K, Map<MK, MV>>, key: K): Map<MK, MV> {
return getOrInsert(map, key, new Map<MK, MV>());
}
/**
* Like `getOrInsert` but with delayed creation of the item
*/
export function getOrInsertWith<K, V>(map: Map<K, V>, key: K, value: () => V): V {
if (!map.has(key)) {
map.set(key, value());
}
return map.get(key);
}