fix(atlas): keep online actions usable when the cache fails

Return successful server responses even when IndexedDB is unavailable. Keep the offline queue in use whenever it is readable, and do not cache a response after the account changes.
This commit is contained in:
Maurice
2026-12-06 16:02:48 +01:00
parent a796383e3f
commit da0ecc3f88
2 changed files with 63 additions and 8 deletions
+44
View File
@@ -77,6 +77,22 @@ describe('atlasRepo reads', () => {
expect((await atlasRepo.flights()).totals.flights).toBe(0);
});
it('FE-REPO-ATLAS-004: returns the server answer if the cache cannot be written', async () => {
serveAtlas();
vi.spyOn(offlineDb.atlasSnapshot, 'put').mockRejectedValue(new Error('QuotaExceededError'));
expect(await atlasRepo.stats()).toEqual(STATS);
});
it('FE-REPO-ATLAS-005: returns the server answer if the queue cannot be opened', async () => {
serveAtlas();
vi.spyOn(offlineDb.userMutationQueue, 'where').mockImplementation(() => {
throw new Error('DatabaseClosedError');
});
expect(await atlasRepo.stats()).toEqual(STATS);
});
it('FE-REPO-ATLAS-002: a request that never reaches the server falls back to the kept answer', async () => {
await readOnline();
server.use(http.get('/api/addons/atlas/stats', () => HttpResponse.error()));
@@ -150,6 +166,34 @@ describe('atlasRepo writes online', () => {
expect(stats.countries.some((c) => c.code === 'IS' && c.status === 'visited')).toBe(true);
});
it('FE-REPO-ATLAS-023: sends an online write when the queue cannot be opened', async () => {
vi.spyOn(offlineDb.userMutationQueue, 'where').mockImplementation(() => {
throw new Error('DatabaseClosedError');
});
let sent = 0;
server.use(
http.post('/api/addons/atlas/country/IS/mark', () => {
sent += 1;
return HttpResponse.json({ success: true });
})
);
await expect(atlasRepo.markCountry('IS')).resolves.toBeUndefined();
expect(sent).toBe(1);
});
it('FE-REPO-ATLAS-024: a failed cache update after a save keeps the server result', async () => {
await readOnline();
vi.spyOn(offlineDb.atlasSnapshot, 'put').mockRejectedValue(new Error('QuotaExceededError'));
server.use(
http.post('/api/addons/atlas/country/IS/mark', () => HttpResponse.json({ success: true })),
http.post('/api/addons/atlas/bucket-list', () => HttpResponse.json({ item: { id: 9, name: 'Faro' } }))
);
await expect(atlasRepo.markCountry('IS')).resolves.toBeUndefined();
await expect(atlasRepo.addBucketItem({ name: 'Faro' })).resolves.toEqual({ id: 9, name: 'Faro' });
});
it('FE-REPO-ATLAS-021: online behind an older queued write, a write waits in line', async () => {
await readOnline();
setAuthed(false); // the flush stays off, so the line is visible
+19 -8
View File
@@ -1,5 +1,5 @@
import apiClient from '../api/client';
import type { AtlasSnapshotPart } from '../db/offlineDb';
import { offlineDb, type AtlasSnapshotPart } from '../db/offlineDb';
import {
ATLAS_RESOURCE,
keepAtlasPart,
@@ -83,9 +83,12 @@ export const ATLAS_NOT_CACHED = 'The Atlas has not been opened online on this de
async function readThrough<T>(part: AtlasSnapshotPart, fetch: () => Promise<T>): Promise<T> {
return onlineThenCache(
async () => {
const account = offlineDb.name;
const data = await fetch();
await keepAtlasPart(part, data);
return (await withWaitingWrites(part, data)) as T;
if (account !== offlineDb.name) return data;
await keepAtlasPart(part, data).catch(() => undefined);
if (account !== offlineDb.name) return data;
return (await withWaitingWrites(part, data).catch(() => data)) as T;
},
async () => {
const kept = await keptAtlasPart(part);
@@ -114,20 +117,24 @@ interface WriteSpec {
async function write(spec: WriteSpec): Promise<void> {
const id = generateUUID();
const body = spec.body ?? null;
if (!(await userMutationQueue.mustQueue(spec.entity, []))) {
const account = offlineDb.name;
if (!(await userMutationQueue.mustQueue(spec.entity, []).catch(() => false))) {
try {
const res = await apiClient.request({
await apiClient.request({
method: spec.method,
url: spec.url,
data: spec.body,
headers: { 'X-Idempotency-Key': id },
});
await reconcileAtlasWrite({ ...spec, ids: {}, body, createdAt: Date.now(), answer: res.data });
if (account === offlineDb.name) {
await reconcileAtlasWrite({ ...spec, ids: {}, body, createdAt: Date.now() }).catch(() => undefined);
}
return;
} catch (err) {
if (!isNetworkError(err)) throw err;
}
}
if (account !== offlineDb.name) return;
await userMutationQueue.enqueue({
id,
method: spec.method,
@@ -227,9 +234,13 @@ export const atlasRepo = {
/** Online only: the server refuses a wish that is on the list already (409). */
async addBucketItem(data: Record<string, unknown>): Promise<unknown> {
const account = offlineDb.name;
const item = (await apiClient.post('/addons/atlas/bucket-list', data)).data?.item as unknown;
const kept = await keptAtlasPart('bucketList');
if (kept && Array.isArray(kept.data) && item) await keepAtlasPart('bucketList', [item, ...kept.data]);
if (account !== offlineDb.name) return item;
const kept = await keptAtlasPart('bucketList').catch(() => undefined);
if (account === offlineDb.name && kept && Array.isArray(kept.data) && item) {
await keepAtlasPart('bucketList', [item, ...kept.data]).catch(() => undefined);
}
return item;
},