diff --git a/client/src/store/tripAddonsStore.test.ts b/client/src/store/tripAddonsStore.test.ts index c777f56f9..d4ce432ef 100644 --- a/client/src/store/tripAddonsStore.test.ts +++ b/client/src/store/tripAddonsStore.test.ts @@ -1,4 +1,4 @@ -// FE-STORE-TRIPADDONS-001 to FE-STORE-TRIPADDONS-008: the trip addons per trip, read, +// FE-STORE-TRIPADDONS-001 to FE-STORE-TRIPADDONS-009: the trip addons per trip, read, // pushed and switched, and the readers that fall back to the instance switches. import { act, renderHook, waitFor } from '@testing-library/react'; import type { TripAddon } from '@trek/shared'; @@ -87,16 +87,40 @@ describe('tripAddonsStore', () => { const first = useTripAddonsStore.getState().load(4); const second = useTripAddonsStore.getState().load(4); - await second; await waitFor(() => expect(asked).toBe(1)); useTripAddonsStore.getState().publish(4, [addon('budget', false, true)]); release(); - await first; + expect(await first).toEqual([addon('budget', false, true)]); + expect(await second).toEqual([addon('budget', false, true)]); expect(asked).toBe(1); expect(entry(4)).toMatchObject({ status: 'ready', addons: [addon('budget', false, true)] }); }); + it('FE-STORE-TRIPADDONS-009: a second caller during a read gets that read’s answer, not the list held before', async () => { + tripHas(4, [addon('tours', false, false)]); + await useTripAddonsStore.getState().load(4); + let release: () => void = () => {}; + let asked = 0; + server.use( + http.get('/api/trips/4/addons', async () => { + asked++; + await new Promise((resolve) => { + release = resolve; + }); + return HttpResponse.json({ tripId: 4, addons: [addon('tours', true, false)] }); + }) + ); + + const first = useTripAddonsStore.getState().load(4); + const second = useTripAddonsStore.getState().load(4); + await waitFor(() => expect(asked).toBe(1)); + release(); + + expect(await second).toEqual([addon('tours', true, false)]); + expect(await first).toEqual([addon('tours', true, false)]); + }); + it('FE-STORE-TRIPADDONS-004: the owner’s switch puts the server’s answer in place', async () => { const addons = await useTripAddonsStore.getState().setOverride(4, 'collab', false); diff --git a/client/src/store/tripAddonsStore.ts b/client/src/store/tripAddonsStore.ts index 3ed060f0d..2198bdd87 100644 --- a/client/src/store/tripAddonsStore.ts +++ b/client/src/store/tripAddonsStore.ts @@ -44,34 +44,45 @@ function keyOf(tripId: number): string { return `${useAuthStore.getState().user?.id ?? 'anon'}:${tripId}`; } +// The read running per trip, so a second caller waits for its answer instead of taking the older list. +const reading = new Map>(); + export const useTripAddonsStore = create((set, get) => ({ trips: {}, - load: async (tripId) => { - if (!Number.isSafeInteger(tripId) || tripId < 1) return null; + load: (tripId) => { + if (!Number.isSafeInteger(tripId) || tripId < 1) return Promise.resolve(null); const key = keyOf(tripId); const before = get().trips[key]; - 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; - try { - addons = await tripAddonsRepo.list(tripId); - } catch { - // Offline with nothing kept, or the server refused: the readers fall back to the instance switches. - } - 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; + if (before?.status === 'loading') return reading.get(key) ?? Promise.resolve(before.addons); + const read = (async () => { + const rev = (before?.rev ?? 0) + 1; + set((state) => ({ + trips: { ...state.trips, [key]: { addons: before?.addons ?? null, status: 'loading', rev } }, + })); + let addons: TripAddon[] | null = null; + try { + addons = await tripAddonsRepo.list(tripId); + } catch { + // Offline with nothing kept, or the server refused: the readers fall back to the instance switches. + } + 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; + })().finally(() => { + if (reading.get(key) === read) reading.delete(key); + }); + reading.set(key, read); + return read; }, publish: (tripId, addons) => {