squashRecordDiffs

See source code

Combines multiple RecordsDiff objects into a single consolidated diff. This function intelligently merges changes, handling cases where the same record is modified multiple times across different diffs. For example, if a record is added in one diff and then updated in another, the result will show it as added with the final state.

function squashRecordDiffs<T extends UnknownRecord>(
  diffs: RecordsDiff<T>[],
  options?: {
    mutateFirstDiff?: boolean;
  },
): RecordsDiff<T>;

Example

const diff1: RecordsDiff<Book> = {
  added: { "book:1": { id: "book:1", title: "New Book" } },
  updated: {},
  removed: {},
};

const diff2: RecordsDiff<Book> = {
  added: {},
  updated: {
    "book:1": [
      { id: "book:1", title: "New Book" },
      { id: "book:1", title: "Updated Title" },
    ],
  },
  removed: {},
};

const squashed = squashRecordDiffs([diff1, diff2]);
// Result: {
//   added: { 'book:1': { id: 'book:1', title: 'Updated Title' } },
//   updated: {},
//   removed: {}
// }

Parameters

NameDescription

diffs

RecordsDiff<T>[];

An array of diffs to combine into a single diff

options

{
  mutateFirstDiff?: boolean;
};

Configuration options for the squashing operation - mutateFirstDiff - If true, modifies the first diff in place instead of creating a new one

Returns

RecordsDiff<T>;

A single diff that represents the cumulative effect of all input diffs

Prev
reverseRecordsDiff
Next
useSync