Skip to content

Commit

Permalink
fix: upsert data when applying to snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnowack committed Dec 11, 2024
1 parent 1eaf0fb commit 58c432d
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/signaldb/src/SyncManager/getSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,21 @@ export default function getSnapshot<ItemType extends BaseItem<IdType>, IdType>(
if (data.items != null) return data.items

const items = lastSnapshot || []
data.changes.added.forEach(item => items.push(item))
data.changes.added.forEach((item) => {
const index = items.findIndex(i => i.id === item.id)
if (index !== -1) {
items[index] = item
} else {
items.push(item)
}
})
data.changes.modified.forEach((item) => {
const index = items.findIndex(i => i.id === item.id)
if (index !== -1) items[index] = item
if (index !== -1) {
items[index] = item
} else {
items.push(item)
}
})
data.changes.removed.forEach((item) => {
const index = items.findIndex(i => i.id === item.id)
Expand Down

0 comments on commit 58c432d

Please sign in to comment.