MCPcopy Create free account
hub / github.com/codemix/graph / createGraphObserver

Function createGraphObserver

packages/y-graph-storage/src/YGraph.ts:90–209  ·  view source on GitHub ↗
(graph: YGraph<TSchema>)

Source from the content-addressed store, hash-verified

88}
89
90function createGraphObserver<TSchema extends GraphSchema>(graph: YGraph<TSchema>) {
91 const { schema, storage } = graph;
92 return new Observable<YGraphChange>((o) => {
93 const unsubscribers: (() => void)[] = [];
94 for (const vertexLabel of Object.keys(schema.vertices)) {
95 const collection = storage.getVertexCollectionMap(vertexLabel);
96 const observer = (events: Y.YEvent<any>[]) => {
97 for (const event of events) {
98 const { path } = event;
99 if (path.length === 0) {
100 if (event instanceof Y.YMapEvent) {
101 for (const [uuid, change] of event.changes.keys) {
102 if (change.action === "add") {
103 o.next({
104 kind: "vertex.added",
105 id: `${vertexLabel}:${uuid}`,
106 });
107 } else if (change.action === "delete") {
108 o.next({
109 kind: "vertex.deleted",
110 id: `${vertexLabel}:${uuid}`,
111 });
112 }
113 }
114 }
115 } else if (path.length === 1) {
116 for (const [key] of event.changes.keys) {
117 if (key.startsWith("@")) {
118 continue;
119 }
120 o.next({
121 kind: "vertex.property.set",
122 id: `${vertexLabel}:${path[0]!}`,
123 property: key,
124 });
125 }
126 } else {
127 const [uuid, property, ...path] = event.path as [
128 string,
129 string,
130 ...(string | number)[],
131 ];
132 if (property.startsWith("@")) {
133 continue;
134 }
135 o.next({
136 kind: "vertex.property.changed",
137 id: `${vertexLabel}:${uuid}`,
138 property,
139 path,
140 event,
141 });
142 }
143 }
144 };
145 collection.observeDeep(observer);
146 unsubscribers.push(() => collection.unobserveDeep(observer));
147 }

Callers 1

subscribeMethod · 0.85

Calls 3

getEdgeCollectionMapMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected