MCPcopy Create free account
hub / github.com/clientdb/clientdb / observableMapGetOrCreate

Function observableMapGetOrCreate

core/queryIndex.ts:40–55  ·  view source on GitHub ↗

* Will get existing value or create new one from observable map.

(
  map: ObservableMap<K, V>,
  key: K,
  getter: () => V
)

Source from the content-addressed store, hash-verified

38 * Will get existing value or create new one from observable map.
39 */
40function observableMapGetOrCreate<K, V>(
41 map: ObservableMap<K, V>,
42 key: K,
43 getter: () => V
44): V {
45 if (map.has(key)) {
46 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
47 return map.get(key)!;
48 }
49
50 const newValue = getter();
51
52 map.set(key, newValue);
53
54 return newValue;
55}
56
57/**
58 * Will create unique key index for entity store that will automatically add/update/delete items from the index on changes.

Callers 1

updateItemIndexWithValueFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected