mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-06-19 13:21:46 +00:00
fix(sync): monotonic createdAt in mutationQueue to prevent FIFO race on fast CI
This commit is contained in:
@@ -36,6 +36,9 @@ export function generateUUID(): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let _flushing = false
|
let _flushing = false
|
||||||
|
// Monotonically increasing timestamp so same-millisecond enqueues
|
||||||
|
// still get a deterministic FIFO order when sorted by createdAt.
|
||||||
|
let _lastTs = 0
|
||||||
|
|
||||||
export const mutationQueue = {
|
export const mutationQueue = {
|
||||||
/**
|
/**
|
||||||
@@ -45,11 +48,13 @@ export const mutationQueue = {
|
|||||||
async enqueue(
|
async enqueue(
|
||||||
mutation: Omit<QueuedMutation, 'status' | 'attempts' | 'createdAt' | 'lastError'>,
|
mutation: Omit<QueuedMutation, 'status' | 'attempts' | 'createdAt' | 'lastError'>,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
|
const now = Date.now()
|
||||||
|
_lastTs = now > _lastTs ? now : _lastTs + 1
|
||||||
const item: QueuedMutation = {
|
const item: QueuedMutation = {
|
||||||
...mutation,
|
...mutation,
|
||||||
status: 'pending',
|
status: 'pending',
|
||||||
attempts: 0,
|
attempts: 0,
|
||||||
createdAt: Date.now(),
|
createdAt: _lastTs,
|
||||||
lastError: null,
|
lastError: null,
|
||||||
}
|
}
|
||||||
await offlineDb.mutationQueue.put(item)
|
await offlineDb.mutationQueue.put(item)
|
||||||
@@ -155,8 +160,9 @@ export const mutationQueue = {
|
|||||||
.count()
|
.count()
|
||||||
},
|
},
|
||||||
|
|
||||||
/** Reset internal flushing flag — useful in tests. */
|
/** Reset internal flushing flag and timestamp counter — useful in tests. */
|
||||||
_resetFlushing(): void {
|
_resetFlushing(): void {
|
||||||
_flushing = false
|
_flushing = false
|
||||||
|
_lastTs = 0
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user