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

Function createStoreData

packages/joint-react/src/data/create-store-data.ts:29–76  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

27 * ```
28 */
29export function createStoreData<Element extends GraphElement = GraphElement>(): StoreData<Element> {
30 /**
31 * Update the store data with the graph data.
32 * @param graph - The graph to update the store data with..
33 * @description
34 */
35 function updateStore(graph: dia.Graph): Set<dia.Cell.ID> {
36 const cells = graph.get('cells');
37
38 if (!cells) throw new Error('Graph cells are not initialized');
39
40 // New updates, if cell is inserted or updated, we track it inside this diff.
41 const elementsDiff = new CellMap<Element>();
42 const linkDiff = new CellMap<GraphLink>();
43 const diffIds = new Set<dia.Cell.ID>();
44 for (const cell of cells) {
45 if (cell.isElement()) {
46 const newElement = getElement<Element>(cell);
47 if (!util.isEqual(newElement, data.elements.get(cell.id))) {
48 elementsDiff.set(cell.id, newElement);
49 diffIds.add(cell.id);
50 }
51 } else if (cell.isLink()) {
52 const newLink = getLink(cell);
53 if (!util.isEqual(newLink, data.links.get(cell.id))) {
54 linkDiff.set(cell.id, newLink);
55 diffIds.add(cell.id);
56 }
57 }
58 }
59
60 data.elements = diffUpdate(data.elements, elementsDiff, (cellId) => cells.has(cellId));
61 data.links = diffUpdate(data.links, linkDiff, (cellId) => cells.has(cellId));
62 return diffIds;
63 }
64
65 const data: StoreData<Element> = {
66 updateStore,
67 elements: new CellMap(),
68 links: new CellMap(),
69 destroy: () => {
70 data.elements.clear();
71 data.links.clear();
72 },
73 };
74
75 return data;
76}

Callers 2

createStoreFunction · 0.90

Calls 1

clearMethod · 0.45

Tested by

no test coverage detected