MCPcopy Index your code
hub / github.com/atomicdata-dev/atomic-data-browser / parseAndApply

Function parseAndApply

lib/src/commit.ts:221–261  ·  view source on GitHub ↗
(jsonAdObjStr: string, store: Store)

Source from the content-addressed store, hash-verified

219
220/** Parses a JSON-AD Commit, applies it to the store. Does not perform checks */
221export function parseAndApply(jsonAdObjStr: string, store: Store) {
222 const jsonAdObj = JSON.parse(jsonAdObjStr);
223 // Parses the commit
224 const subject = jsonAdObj[urls.properties.commit.subject];
225 const set = jsonAdObj[urls.properties.commit.set];
226 const remove: string[] | undefined = jsonAdObj[urls.properties.commit.remove];
227 const destroy: boolean | undefined =
228 jsonAdObj[urls.properties.commit.destroy];
229 // const createdAt = jsonAdObj[urls.properties.commit.createdAt];
230 // const signer = jsonAdObj[urls.properties.commit.signer];
231 const signature = jsonAdObj[urls.properties.commit.signature];
232
233 let resource = store.resources.get(subject);
234
235 // If the resource doesn't exist in the store, create the resource
236 if (resource == undefined) {
237 resource = new Resource(subject);
238 } else {
239 if (resource.appliedCommitSignatures.has(signature)) {
240 return;
241 }
242 }
243
244 set &&
245 Object.keys(set).forEach(propUrl => {
246 resource.setUnsafe(propUrl, set[propUrl]);
247 });
248
249 remove &&
250 remove.forEach(propUrl => {
251 resource.removePropValLocally(propUrl);
252 });
253
254 if (destroy) {
255 store.removeResource(subject);
256 return;
257 } else {
258 resource.appliedCommitSignatures.add(signature);
259 store.addResource(resource);
260 }
261}

Callers 2

handleMessageFunction · 0.90
commit.test.tsFile · 0.85

Calls 5

getMethod · 0.80
setUnsafeMethod · 0.80
removePropValLocallyMethod · 0.80
removeResourceMethod · 0.80
addResourceMethod · 0.80

Tested by

no test coverage detected