| 14 | export function getOrAdd<TKey, TValue>(map: Map<TKey, TValue>, key: TKey, defaultValue: InitialValue<Promise<TValue>>): Promise<TValue>; |
| 15 | export function getOrAdd<TKey, TValue>(map: Map<TKey, TValue>, key: TKey, defaultValue: InitialValue<TValue>): TValue; |
| 16 | export 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 | } |