mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 23:50:08 +00:00
feat(atlas): read and write the Atlas offline
The Atlas reads go network first and fall back to a snapshot kept in a new Dexie table (v17, append-only). Marks, unmarks and wishlist deletes made offline wait in their own account-level queue, replayed in order with an idempotency key, and every read lays them over the kept answer.
This commit is contained in:
@@ -59,6 +59,7 @@ const HISTORY: Record<number, StoreSpec> = {
|
||||
14: { routeCache: 'key, savedAt' },
|
||||
15: { tripTags: 'tripId' },
|
||||
16: { tripAddons: 'tripId' },
|
||||
17: { atlasSnapshot: 'part', userMutationQueue: 'id, status, createdAt, entity' },
|
||||
};
|
||||
|
||||
const HISTORY_VERSIONS = Object.keys(HISTORY)
|
||||
@@ -147,6 +148,22 @@ function rowsAt(version: number): Record<string, Record<string, unknown>> {
|
||||
{ id: 'budget', name: 'Costs', icon: 'Wallet', enabled: false, instanceEnabled: true, overridden: true },
|
||||
],
|
||||
},
|
||||
atlasSnapshot: { part: 'stats', data: { countries: [{ code: 'PT', status: 'visited' }], stats: {} } },
|
||||
userMutationQueue: {
|
||||
id: 'account-1',
|
||||
method: 'POST',
|
||||
url: '/addons/atlas/country/PT/mark',
|
||||
ids: {},
|
||||
body: null,
|
||||
resource: 'atlasCountry',
|
||||
entity: 'atlasCountry:PT',
|
||||
createdAt: 2,
|
||||
status: 'pending',
|
||||
attempts: 0,
|
||||
lastError: null,
|
||||
schemaVersion: 1,
|
||||
buildVersion: '5.3.0',
|
||||
},
|
||||
};
|
||||
const tables = schemaAt(version);
|
||||
return Object.fromEntries(Object.entries(all).filter(([table]) => table in tables));
|
||||
@@ -417,6 +434,28 @@ const UPGRADE_CASES: Record<number, UpgradeCase> = {
|
||||
expect((await db.tripAddons.get(1))?.addons[0]).toMatchObject({ id: 'budget', enabled: true, overridden: false });
|
||||
},
|
||||
},
|
||||
17: {
|
||||
intent: 'adds the kept Atlas reads and the account queue, and leaves the trip queue as it was',
|
||||
check: async (db) => {
|
||||
// The trip queue an install already holds is not touched: same row, same indexes.
|
||||
expect(await db.mutationQueue.where('tripId').equals(1).count()).toBe(1);
|
||||
expect(await db.mutationQueue.get('queued-1')).not.toHaveProperty('scope');
|
||||
expect(await db.atlasSnapshot.count()).toBe(0);
|
||||
expect(await db.userMutationQueue.count()).toBe(0);
|
||||
// One kept answer per read: a second one replaces the first.
|
||||
await db.atlasSnapshot.put({ part: 'regions', data: { DE: [] } });
|
||||
await db.atlasSnapshot.put({ part: 'regions', data: { FR: [] } });
|
||||
expect(await db.atlasSnapshot.count()).toBe(1);
|
||||
expect((await db.atlasSnapshot.get('regions'))?.data).toEqual({ FR: [] });
|
||||
await db.table('userMutationQueue').bulkPut([
|
||||
{ id: 'a', entity: 'atlasCountry:DE', status: 'pending', createdAt: 2, ids: {} },
|
||||
{ id: 'b', entity: 'atlasCountry:FR', status: 'failed', createdAt: 1, ids: {} },
|
||||
]);
|
||||
expect(await db.userMutationQueue.where('entity').equals('atlasCountry:DE').count()).toBe(1);
|
||||
expect(await db.userMutationQueue.where('status').equals('failed').count()).toBe(1);
|
||||
expect(await db.userMutationQueue.orderBy('createdAt').primaryKeys()).toEqual(['b', 'a']);
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
describe('offlineDb upgrades', () => {
|
||||
|
||||
Reference in New Issue
Block a user