mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-22 06:56:35 +00:00
fix(realtime): open no socket after a logout, and reconnect as soon as the network is back
A connect still waiting for its ws token when the user logged out went on to open a socket authenticated as that user, which the next login on the same browser then reused. And a socket in its reconnect backoff waited up to 30 s after the network came back or the tab became visible. Both events now reconnect right away.
This commit is contained in:
@@ -22,10 +22,12 @@ vi.mock('../../../src/sync/mutationQueue', () => ({
|
||||
vi.mock('../../../src/sync/tripSyncManager', () => ({
|
||||
tripSyncManager: { syncAll: () => syncAll() },
|
||||
}));
|
||||
let reconnects = 0;
|
||||
vi.mock('../../../src/api/websocket', () => ({
|
||||
setPreReconnectHook: (fn: (() => Promise<void>) | null) => { preReconnect = fn; },
|
||||
setRefetchCallback: (fn: ((tripId: string) => void) | null) => { refetchCb = fn; },
|
||||
getActiveTrips: () => ['7'],
|
||||
reconnectNow: () => { reconnects++; },
|
||||
}));
|
||||
vi.mock('../../../src/store/tripStore', () => ({
|
||||
useTripStore: { getState: () => ({ hydrateActiveTrip: hydrate }) },
|
||||
@@ -45,7 +47,7 @@ const flushMicrotasks = async () => {
|
||||
|
||||
beforeEach(() => {
|
||||
flush.mockClear(); syncAll.mockClear(); hydrate.mockClear(); loadSettings.mockClear();
|
||||
refetchCb = null; preReconnect = null;
|
||||
refetchCb = null; preReconnect = null; reconnects = 0;
|
||||
settingsLoaded = false; authenticated = true;
|
||||
Object.defineProperty(navigator, 'onLine', { value: true, writable: true, configurable: true });
|
||||
});
|
||||
@@ -88,6 +90,18 @@ describe('syncTriggers', () => {
|
||||
expect(flush.mock.invocationCallOrder[0]).toBeLessThan(hydrate.mock.invocationCallOrder[0]);
|
||||
});
|
||||
|
||||
it('online event and a tab coming back reconnect the socket right away instead of waiting out its backoff', async () => {
|
||||
registerSyncTriggers();
|
||||
window.dispatchEvent(new Event('online'));
|
||||
await flushMicrotasks();
|
||||
expect(reconnects).toBe(1);
|
||||
|
||||
Object.defineProperty(document, 'hidden', { value: false, configurable: true });
|
||||
document.dispatchEvent(new Event('visibilitychange'));
|
||||
await flushMicrotasks();
|
||||
expect(reconnects).toBe(2);
|
||||
});
|
||||
|
||||
it('online event retries the settings load when it has not yet succeeded (#1618)', async () => {
|
||||
registerSyncTriggers();
|
||||
window.dispatchEvent(new Event('online'));
|
||||
|
||||
Reference in New Issue
Block a user