MCPcopy Index your code
hub / github.com/microsoft/vscode-cpptools / getOrAdd

Function getOrAdd

Extension/src/Utility/System/map.ts:16–35  ·  view source on GitHub ↗
(map: Map<TKey, TValue> | WeakMap<any, TValue>, key: TKey, defaultValue: InitialValue<TValue | Promise<TValue>>)

Source from the content-addressed store, hash-verified

14export function getOrAdd<TKey, TValue>(map: Map<TKey, TValue>, key: TKey, defaultValue: InitialValue<Promise<TValue>>): Promise<TValue>;
15export function getOrAdd<TKey, TValue>(map: Map<TKey, TValue>, key: TKey, defaultValue: InitialValue<TValue>): TValue;
16export function getOrAdd<TKey, TValue>(map: Map<TKey, TValue> | WeakMap<any, TValue>, key: TKey, defaultValue: InitialValue<TValue | Promise<TValue>>): TValue | Promise<TValue> {
17 const value = map.get(key);
18 if (!is.nullish(value)) {
19 return value;
20 }
21 const initializer = defaultValue instanceof Function ? defaultValue() : defaultValue;
22 if (is.promise(initializer)) {
23 return initializer.then(v => {
24 if (v !== undefined) {
25 map.set(key, v);
26 }
27 return v;
28 });
29 } else {
30 if (initializer !== undefined) {
31 map.set(key, initializer);
32 }
33 return initializer;
34 }
35}

Callers 2

onFunction · 0.90
addMethod · 0.90

Calls 5

nullishMethod · 0.80
promiseMethod · 0.80
getMethod · 0.45
thenMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected