MCPcopy
hub / github.com/tinyplex/tinybase / getChangesFromYDoc

Function getChangesFromYDoc

src/persisters/persister-yjs/index.ts:29–87  ·  view source on GitHub ↗
(
  yContent: YMap<any>,
  events: YEvent<any>[],
)

Source from the content-addressed store, hash-verified

27const getYContent = (yContent: YMap<any>) => [yContent.get(T), yContent.get(V)];
28
29const getChangesFromYDoc = (
30 yContent: YMap<any>,
31 events: YEvent<any>[],
32): Changes => {
33 if (size(events) == 1 && arrayIsEmpty(events[0].path)) {
34 return [yContent.get(T).toJSON(), yContent.get(V).toJSON(), 1];
35 }
36 const [yTables, yValues] = getYContent(yContent);
37 const tables = {} as any;
38 const values = {} as any;
39 arrayForEach(events, ({path, changes: {keys}}) =>
40 arrayShift(path) == T
41 ? ifNotUndefined(
42 arrayShift(path) as string,
43 (yTableId) => {
44 const table = objEnsure(tables, yTableId, objNew) as any;
45 const yTable = yTables.get(yTableId) as YMap<YMap<Cell>>;
46 ifNotUndefined(
47 arrayShift(path) as string,
48 (yRowId) => {
49 const row = objEnsure(table, yRowId, objNew) as any;
50 const yRow = yTable.get(yRowId) as YMap<Cell>;
51 mapForEach(
52 keys,
53 (cellId, {action}) =>
54 (row[cellId] =
55 action == DELETE ? undefined : yRow.get(cellId)),
56 );
57 },
58 () =>
59 mapForEach(
60 keys,
61 (rowId, {action}) =>
62 (table[rowId] =
63 action == DELETE
64 ? undefined
65 : yTable.get(rowId)?.toJSON()),
66 ),
67 );
68 },
69 () =>
70 mapForEach(
71 keys,
72 (tableId, {action}) =>
73 (tables[tableId] =
74 action == DELETE
75 ? undefined
76 : yTables.get(tableId)?.toJSON()),
77 ),
78 )
79 : mapForEach(
80 keys,
81 (valueId, {action}) =>
82 (values[valueId] =
83 action == DELETE ? undefined : yValues.get(valueId)),
84 ),
85 );
86 return [tables, values, 1];

Callers 1

observerFunction · 0.85

Calls 8

sizeFunction · 0.90
arrayIsEmptyFunction · 0.90
arrayForEachFunction · 0.90
arrayShiftFunction · 0.90
objEnsureFunction · 0.90
mapForEachFunction · 0.90
getYContentFunction · 0.85
getMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…