Skip to content

Commit 5a42f39

Browse files
authored
Merge pull request #2778 from TAMULib/dedupe-patch-operations
Dedupe patch operations by op and path
2 parents 85369aa + f40639d commit 5a42f39

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

src/app/core/json-patch/json-patch-operations.reducer.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,28 @@ function addOperationToList(body: JsonPatchOperationObject[], actionType, target
351351
newBody.push(makeOperationEntry({ op: JsonPatchOperationType.move, from: fromPath, path: targetPath }));
352352
break;
353353
}
354-
return newBody;
354+
return dedupeOperationEntries(newBody);
355+
}
356+
357+
/**
358+
* Dedupe operation entries by op and path. This prevents processing unnecessary patches in a single PATCH request.
359+
*
360+
* @param body JSON patch operation object entries
361+
* @returns deduped JSON patch operation object entries
362+
*/
363+
function dedupeOperationEntries(body: JsonPatchOperationObject[]): JsonPatchOperationObject[] {
364+
const ops = new Map<string, any>();
365+
for (let i = body.length - 1; i >= 0; i--) {
366+
const patch = body[i].operation;
367+
const key = `${patch.op}-${patch.path}`;
368+
if (!ops.has(key)) {
369+
ops.set(key, patch);
370+
} else {
371+
body.splice(i, 1);
372+
}
373+
}
374+
375+
return body;
355376
}
356377

357378
function makeOperationEntry(operation) {

0 commit comments

Comments
 (0)