(jsonAdObjStr: string, store: Store)
| 219 | |
| 220 | /** Parses a JSON-AD Commit, applies it to the store. Does not perform checks */ |
| 221 | export 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 | } |
no test coverage detected