Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 415a7a4e4e | |||
| cb4f1b5989 | |||
| b37f935e87 | |||
| 213001b139 |
+55
-10
@@ -32,7 +32,48 @@ function entityKey(item: SyncQueueItem): string {
|
|||||||
return `${item.type}:${item.payloadId}`
|
return `${item.type}:${item.payloadId}`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep only the latest queue entry per entity; delete wins over create/update.
|
function latestQueueItem(items: SyncQueueItem[]): SyncQueueItem {
|
||||||
|
return items.reduce((a, b) => ((a.id ?? 0) > (b.id ?? 0) ? a : b))
|
||||||
|
}
|
||||||
|
|
||||||
|
async function entityExistsLocally(item: SyncQueueItem): Promise<boolean> {
|
||||||
|
switch (item.type) {
|
||||||
|
case 'logbook':
|
||||||
|
return !!(await db.logbooks.get(item.payloadId))
|
||||||
|
case 'yacht':
|
||||||
|
return !!(await db.yachts.get(item.logbookId))
|
||||||
|
case 'deviation':
|
||||||
|
return !!(await db.deviations.get(item.logbookId))
|
||||||
|
case 'crew':
|
||||||
|
return !!(await db.crews.get(item.payloadId))
|
||||||
|
case 'entry':
|
||||||
|
return !!(await db.entries.get(item.payloadId))
|
||||||
|
case 'photo':
|
||||||
|
return !!(await db.photos.get(item.payloadId))
|
||||||
|
case 'gpsTrack':
|
||||||
|
return !!(await db.gpsTracks.get(item.payloadId))
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pick one queue entry per entity. If the record still exists locally, the latest
|
||||||
|
// action wins (supports recreate-after-delete). If it was removed locally, a delete
|
||||||
|
// wins over stale upserts with higher IDs; orphaned upserts are dropped entirely.
|
||||||
|
async function resolveCoalescedItem(group: SyncQueueItem[]): Promise<SyncQueueItem | null> {
|
||||||
|
const exists = await entityExistsLocally(group[0])
|
||||||
|
if (exists) {
|
||||||
|
return latestQueueItem(group)
|
||||||
|
}
|
||||||
|
|
||||||
|
const deletes = group.filter((item) => item.action === 'delete')
|
||||||
|
if (deletes.length > 0) {
|
||||||
|
return latestQueueItem(deletes)
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
async function coalesceSyncQueue(logbookId: string): Promise<SyncQueueItem[]> {
|
async function coalesceSyncQueue(logbookId: string): Promise<SyncQueueItem[]> {
|
||||||
const pending = await db.syncQueue.where({ logbookId }).toArray()
|
const pending = await db.syncQueue.where({ logbookId }).toArray()
|
||||||
if (pending.length <= 1) return pending
|
if (pending.length <= 1) return pending
|
||||||
@@ -49,16 +90,20 @@ async function coalesceSyncQueue(logbookId: string): Promise<SyncQueueItem[]> {
|
|||||||
const staleIds: number[] = []
|
const staleIds: number[] = []
|
||||||
|
|
||||||
for (const group of byEntity.values()) {
|
for (const group of byEntity.values()) {
|
||||||
const deletes = group.filter((item) => item.action === 'delete')
|
const winner = await resolveCoalescedItem(group)
|
||||||
const latest =
|
|
||||||
deletes.length > 0
|
|
||||||
? deletes.reduce((a, b) => ((a.id ?? 0) > (b.id ?? 0) ? a : b))
|
|
||||||
: group.reduce((a, b) => ((a.id ?? 0) > (b.id ?? 0) ? a : b))
|
|
||||||
|
|
||||||
kept.push(latest)
|
if (winner) {
|
||||||
for (const item of group) {
|
kept.push(winner)
|
||||||
if (item.id !== undefined && item.id !== latest.id) {
|
for (const item of group) {
|
||||||
staleIds.push(item.id)
|
if (item.id !== undefined && item.id !== winner.id) {
|
||||||
|
staleIds.push(item.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (const item of group) {
|
||||||
|
if (item.id !== undefined) {
|
||||||
|
staleIds.push(item.id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user