Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions src/diffPatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,21 +267,19 @@ function diffArray(
const isSingle = itemA.length - itemB.length === 1
const unsetItems = itemA.slice(itemB.length)

// If we're revision locked, we can safely unset ranges (eg 5:<end-of-array>).
// Also, if we don't have unique array keys, we can't use any better approach
// than array indexes. If we _do_ have unique array keys, we'll want to unset
// by key, as this is safer in a realtime, collaborative setting
if (isRevisionLocked(options) || !isUniquelyKeyed(unsetItems)) {
patches.push({
op: 'unset',
path: path.concat([isSingle ? itemB.length : [itemB.length, '']]),
})
} else {
// If we have unique array keys, we'll want to unset by key, as this is
// safer in a realtime, collaborative setting
if (isUniquelyKeyed(unsetItems)) {
patches.push(
...unsetItems.map(
(item): UnsetPatch => ({op: 'unset', path: path.concat({_key: item._key})}),
),
)
} else {
patches.push({
op: 'unset',
path: path.concat([isSingle ? itemB.length : [itemB.length, '']]),
})
}
}

Expand Down Expand Up @@ -598,10 +596,6 @@ function nullifyUndefined(item: unknown, path: Path, index: number, options: Pat
return null
}

function isRevisionLocked(options: PatchOptions): boolean {
return Boolean(options.ifRevisionID)
}

function yes(_: unknown) {
return true
}