fix(sync): read the open trip's addons again after the socket reconnects

A tripAddons:changed push is not replayed, so a switch made while the socket was
down now reaches the member on the reconnect refetch.
This commit is contained in:
Maurice
2026-11-23 14:55:53 +01:00
parent 769293a554
commit c3d60e1c43
2 changed files with 15 additions and 2 deletions
+4 -1
View File
@@ -18,6 +18,7 @@ import { isEffectivelyOnline, onNetworkModeChange } from './networkMode'
import { setPreReconnectHook, setRefetchCallback, getActiveTrips, reconnectNow } from '../api/websocket'
import { useTripStore } from '../store/tripStore'
import { reloadTourVisits } from '../store/tourVisitStore'
import { useTripAddonsStore } from '../store/tripAddonsStore'
import { useSettingsStore } from '../store/settingsStore'
import { useAuthStore } from '../store/authStore'
@@ -113,10 +114,12 @@ export function registerSyncTriggers(): void {
setPreReconnectHook(() => mutationQueue.flush())
// After the reconnect flush, pull canonical state for the open trip back into
// the store (the WS layer awaits the flush hook before invoking this), and
// the walk visits, so a mark the replay dropped or failed leaves the day plan.
// the walk visits, so a mark the replay dropped or failed leaves the day plan,
// and the trip's addons, whose change pushed while the socket was down is lost.
setRefetchCallback(tripId => {
useTripStore.getState().hydrateActiveTrip(tripId).catch(console.error)
reloadTourVisits(tripId)
void useTripAddonsStore.getState().load(Number(tripId))
})
window.addEventListener('online', onOnline)
+11 -1
View File
@@ -11,6 +11,7 @@ const syncAll = vi.fn(() => Promise.resolve());
const hydrate = vi.fn(() => Promise.resolve());
const loadSettings = vi.fn(() => Promise.resolve());
const reloadVisits = vi.fn();
const loadTripAddons = vi.fn(() => Promise.resolve());
let refetchCb: ((tripId: string) => void) | null = null;
let preReconnect: (() => Promise<void>) | null = null;
@@ -36,6 +37,9 @@ vi.mock('../../../src/store/tripStore', () => ({
vi.mock('../../../src/store/tourVisitStore', () => ({
reloadTourVisits: (tripId: string) => reloadVisits(tripId),
}));
vi.mock('../../../src/store/tripAddonsStore', () => ({
useTripAddonsStore: { getState: () => ({ load: loadTripAddons }) },
}));
vi.mock('../../../src/store/settingsStore', () => ({
useSettingsStore: { getState: () => ({ isLoaded: settingsLoaded, loadSettings }) },
}));
@@ -50,7 +54,7 @@ const flushMicrotasks = async () => {
};
beforeEach(() => {
flush.mockClear(); syncAll.mockClear(); hydrate.mockClear(); loadSettings.mockClear(); reloadVisits.mockClear();
flush.mockClear(); syncAll.mockClear(); hydrate.mockClear(); loadSettings.mockClear(); reloadVisits.mockClear(); loadTripAddons.mockClear();
refetchCb = null; preReconnect = null; reconnects = 0;
settingsLoaded = false; authenticated = true;
Object.defineProperty(navigator, 'onLine', { value: true, writable: true, configurable: true });
@@ -74,6 +78,12 @@ describe('syncTriggers', () => {
expect(reloadVisits).toHaveBeenCalledWith('7');
});
it('the refetch after a reconnect reads the trip addons again, a switch pushed while the socket was down included', () => {
registerSyncTriggers();
refetchCb?.('7');
expect(loadTripAddons).toHaveBeenCalledWith(7);
});
it('also registers the pre-reconnect flush hook', () => {
registerSyncTriggers();
expect(preReconnect).toBeTypeOf('function');