fix(trip-addons): let a second reader wait for the trip's list being read

A read started while another one runs now answers with that read's list instead of the one held before, so the offline sync no longer skips the Tours and Road trip caches next to a planner load.
This commit is contained in:
Maurice
2026-11-23 15:35:47 +01:00
parent 918efe35fe
commit b9637ff90c
2 changed files with 61 additions and 26 deletions
+27 -3
View File
@@ -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<void>((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);
+34 -23
View File
@@ -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<string, Promise<TripAddon[] | null>>();
export const useTripAddonsStore = create<TripAddonsState>((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) => {