Commits
Tim Donohue authored and GitHub committed ab579da6148 Merge
344 344 | value: value |
345 345 | })); |
346 346 | break; |
347 347 | case JsonPatchOperationsActionTypes.NEW_JSON_PATCH_REMOVE_OPERATION: |
348 348 | newBody.push(makeOperationEntry({ op: JsonPatchOperationType.remove, path: targetPath })); |
349 349 | break; |
350 350 | case JsonPatchOperationsActionTypes.NEW_JSON_PATCH_MOVE_OPERATION: |
351 351 | newBody.push(makeOperationEntry({ op: JsonPatchOperationType.move, from: fromPath, path: targetPath })); |
352 352 | break; |
353 353 | } |
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; |
355 376 | } |
356 377 | |
357 378 | function makeOperationEntry(operation) { |
358 379 | return { operation: operation, timeCompleted: new Date().getTime() }; |
359 380 | } |