fix(offline): an online edit waits behind an older queued write to the same entity

Holding later queued writes behind a parked one only ordered writes that
were in the queue. Online, the repos send an edit straight to the server, so
an edit made while an older write to the same visit, tour or driving settings
was backing off or parked landed first, and Try again (or the next flush)
then replayed the older write over it. Those writes carry no version token,
so nothing refused the stale replay.

The repos of visits, day clears, tours and driving settings now ask the
queue before an online write. While an older write to the same entity is
pending, in flight, parked or a conflict, the new write is queued too, with
the same optimistic Dexie write as offline, and a flush is started so it
goes out as soon as the one ahead of it does. When the driving settings left
the cache since the older write, they are read in first.

Places and packing items keep their direct online path: they send the base
token, so a stale replay is refused with a 409 and becomes a conflict. What
this does not cover, and the docs now say so: a collaborator's change to a
token-less entity is overwritten by the retried write, as by any replayed
offline write of those resources, and the planner's two time edits that call
the API directly are not held. The wording of the parking commit also named
budget items, which never go through the queue.
This commit is contained in:
Maurice
2026-10-08 11:46:48 +02:00
parent b61a55f037
commit 48b1e879d2
10 changed files with 170 additions and 15 deletions
@@ -715,6 +715,34 @@ describe('mutationQueue: writes to one entity keep their order around a parked o
});
});
describe('mutationQueue.mustQueue: a write to an entity with an older one still in the queue', () => {
const visit = (status: 'pending' | 'syncing' | 'failed' | 'conflict', entityId = 7) => ({
id: generateUUID(), tripId: 1, method: 'PUT' as const, url: `/trips/1/assignments/${entityId}/time`,
body: {}, createdAt: 1, status, attempts: 0, lastError: null, resource: 'assignments', entityId,
});
it('is always true offline', async () => {
Object.defineProperty(navigator, 'onLine', { value: false });
expect(await mutationQueue.mustQueue('assignments', 7)).toBe(true);
});
it('is false online while nothing is queued for the entity', async () => {
await offlineDb.mutationQueue.put(visit('failed', 8));
await offlineDb.mutationQueue.put({ ...visit('failed'), resource: 'places' });
expect(await mutationQueue.mustQueue('assignments', 7)).toBe(false);
});
it.each(['pending', 'syncing', 'failed', 'conflict'] as const)('is true online behind a %s write to the entity', async status => {
await offlineDb.mutationQueue.put(visit(status));
expect(await mutationQueue.mustQueue('assignments', 7)).toBe(true);
});
it('is true for an edit of an offline create that has not synced yet', async () => {
await mutationQueue.enqueue({ id: generateUUID(), tripId: 1, method: 'POST', url: '/trips/1/tours', body: {}, resource: 'tours', tempId: -42 });
expect(await mutationQueue.mustQueue('tours', -42)).toBe(true);
});
});
describe('mutationQueue.flush: one tab at a time', () => {
/** A Web Locks stand-in: one holder per name, later requests wait their turn. */
function fakeLocks() {