MCPcopy
hub / github.com/clientIO/joint / diffUpdate

Function diffUpdate

packages/joint-react/src/utils/diff-update.ts:12–38  ·  view source on GitHub ↗
(
  original: M,
  diff: Map<K, V>,
  newDataContainKey: (key: K) => boolean
)

Source from the content-addressed store, hash-verified

10 * @description
11 */
12export function diffUpdate<K, V, M extends Map<K, V>>(
13 original: M,
14 diff: Map<K, V>,
15 newDataContainKey: (key: K) => boolean
16): M {
17 let hasDelete = false;
18
19 for (const [key] of original) {
20 if (!newDataContainKey(key)) {
21 original.delete(key);
22 hasDelete = true;
23 }
24 }
25
26 if (diff.size === 0 && !hasDelete) {
27 return original;
28 }
29
30 const NewMapConstructor = original.constructor as new (entries?: Iterable<[K, V]>) => M;
31 const newMap = new NewMapConstructor(original); // shallow copy to preserve type
32
33 for (const [key, value] of diff) {
34 newMap.set(key, value);
35 }
36
37 return newMap;
38}

Callers 1

updateStoreFunction · 0.90

Calls 2

deleteMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected