Skip to content

Commit

Permalink
Dedupe patch operations by op and path
Browse files Browse the repository at this point in the history
  • Loading branch information
wwelling committed Jan 26, 2024
1 parent 5b9a98a commit f40639d
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/app/core/json-patch/json-patch-operations.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,28 @@ function addOperationToList(body: JsonPatchOperationObject[], actionType, target
newBody.push(makeOperationEntry({ op: JsonPatchOperationType.move, from: fromPath, path: targetPath }));
break;
}
return newBody;
return dedupeOperationEntries(newBody);
}

/**
* Dedupe operation entries by op and path. This prevents processing unnecessary patches in a single PATCH request.
*
* @param body JSON patch operation object entries
* @returns deduped JSON patch operation object entries
*/
function dedupeOperationEntries(body: JsonPatchOperationObject[]): JsonPatchOperationObject[] {
const ops = new Map<string, any>();
for (let i = body.length - 1; i >= 0; i--) {
const patch = body[i].operation;
const key = `${patch.op}-${patch.path}`;
if (!ops.has(key)) {
ops.set(key, patch);
} else {
body.splice(i, 1);

Check warning on line 371 in src/app/core/json-patch/json-patch-operations.reducer.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L371 was not covered by tests
}
}

return body;
}

function makeOperationEntry(operation) {
Expand Down

0 comments on commit f40639d

Please sign in to comment.