diff --git a/client/src/store/tripAddonsStore.ts b/client/src/store/tripAddonsStore.ts index 613418a8f..3ed060f0d 100644 --- a/client/src/store/tripAddonsStore.ts +++ b/client/src/store/tripAddonsStore.ts @@ -29,8 +29,11 @@ interface TripAddonsEntry { interface TripAddonsState { trips: Record; - /** Reads the trip's list into the store; one read per trip at a time. Never throws. */ - load: (tripId: number) => Promise; + /** + * Reads the trip's list into the store; one read per trip at a time. Answers + * the list the store holds afterwards, null while it holds none. Never throws. + */ + load: (tripId: number) => Promise; /** Puts a list the server sent (an answer or a push) in place. */ publish: (tripId: number, addons: TripAddon[]) => void; /** The owner switches one addon for the trip (null: back to the instance setting). Online only; throws on refusal. */ @@ -45,10 +48,10 @@ export const useTripAddonsStore = create((set, get) => ({ trips: {}, load: async (tripId) => { - if (!Number.isSafeInteger(tripId) || tripId < 1) return; + if (!Number.isSafeInteger(tripId) || tripId < 1) return null; const key = keyOf(tripId); const before = get().trips[key]; - if (before?.status === 'loading') return; + if (before?.status === 'loading') return before.addons; const rev = (before?.rev ?? 0) + 1; set((state) => ({ trips: { ...state.trips, [key]: { addons: before?.addons ?? null, status: 'loading', rev } } })); let addons: TripAddon[] | null = null; @@ -57,15 +60,18 @@ export const useTripAddonsStore = create((set, get) => ({ } catch { // Offline with nothing kept, or the server refused: the readers fall back to the instance switches. } - set((state) => { - const current = state.trips[key]; - // A push that landed meanwhile is newer than this answer and stays. - if (current && current.rev !== rev) return state; - const entry: TripAddonsEntry = addons - ? { addons, status: 'ready', rev } - : { addons: current?.addons ?? null, status: 'failed', rev }; - return { trips: { ...state.trips, [key]: entry } }; - }); + const current = get().trips[key]; + // A push that landed meanwhile is newer than this answer and stays, in the + // store and in the device's copy the answer was written over. + if (current && current.rev !== rev) { + if (addons && current.addons) void tripAddonsRepo.remember(tripId, current.addons).catch(() => {}); + return current.addons; + } + const entry: TripAddonsEntry = addons + ? { addons, status: 'ready', rev } + : { addons: current?.addons ?? null, status: 'failed', rev }; + set((state) => ({ trips: { ...state.trips, [key]: entry } })); + return entry.addons; }, publish: (tripId, addons) => { diff --git a/client/src/sync/tripSyncManager.ts b/client/src/sync/tripSyncManager.ts index f78545f63..5c036821a 100644 --- a/client/src/sync/tripSyncManager.ts +++ b/client/src/sync/tripSyncManager.ts @@ -1,7 +1,6 @@ import { roadtripPreferencesRepo } from '../repo/roadtripPreferencesRepo' import { tourRepo } from '../repo/tourRepo' import { tripTagRepo } from '../repo/tripTagRepo' -import { tripAddonsRepo } from '../repo/tripAddonsRepo' /** * Trip sync manager — seeds Dexie with trip data for offline use. * @@ -188,15 +187,10 @@ async function syncTrip(tripId: number): Promise { /** The trip's addons, read and kept for offline use, and put in the store for the open views; null when unreadable. */ async function readTripAddons(tripId: number): Promise { - try { - const addons = await tripAddonsRepo.list(tripId) - useTripAddonsStore.getState().publish(tripId, addons) - return addons - } catch { - // An optional cache like the ones after it: a trip opened offline then shows - // the instance switches, until the next sync reads its own. - return null - } + // Through load, so a push that lands while the read is on its way stays. An + // optional cache like the ones after it: a trip opened offline without a list + // shows the instance switches, until the next sync reads its own. + return useTripAddonsStore.getState().load(tripId) } /** diff --git a/client/tests/unit/sync/tripSyncManager.test.ts b/client/tests/unit/sync/tripSyncManager.test.ts index 88dee0aa1..367455230 100644 --- a/client/tests/unit/sync/tripSyncManager.test.ts +++ b/client/tests/unit/sync/tripSyncManager.test.ts @@ -13,6 +13,8 @@ import { setAuthed } from '../../../src/sync/authGate'; import { setTripPinned, setTripOfflineEnabled, _resetOfflinePrefs } from '../../../src/sync/offlinePrefs'; import { offlineDb, clearAll, upsertTrip } from '../../../src/db/offlineDb'; import { useAddonStore } from '../../../src/store/addonStore'; +import { tripAddonsEntry, useTripAddonsStore } from '../../../src/store/tripAddonsStore'; +import { tripAddonsRepo } from '../../../src/repo/tripAddonsRepo'; import { buildTrip, buildDay, @@ -394,6 +396,24 @@ describe('tripSyncManager.syncAll — tours', () => { expect(await offlineDb.tours.count()).toBe(0); }); + it('a push that lands while the list is on its way outlives the older answer, on screen and offline', async () => { + serve(317, () => HttpResponse.json({ tours: [] })); + server.use( + http.get('/api/trips/317/addons', async () => { + useTripAddonsStore.getState().publish(317, [toursAddon(true, false)]); + await tripAddonsRepo.remember(317, [toursAddon(true, false)]); + return HttpResponse.json({ tripId: 317, addons: [toursAddon(false, false)] }); + }), + ); + + await tripSyncManager.syncAll(); + + expect(tripAddonsEntry(useTripAddonsStore.getState(), 317)?.addons).toEqual([toursAddon(true, false)]); + await vi.waitFor(async () => + expect((await offlineDb.tripAddons.get(317))?.addons).toEqual([toursAddon(true, false)]), + ); + }); + it('follows the instance switch when the list cannot be read, and still stores the trip', async () => { useAddonStore.setState({ addons: [{ id: 'tours', enabled: true }] as never }); server.use(http.get('/api/trips/316/addons', () => HttpResponse.json({ error: 'boom' }, { status: 500 })));