diff --git a/projects/kit/src/lib/realtime/kit-realtime-connection.spec.ts b/projects/kit/src/lib/realtime/kit-realtime-connection.spec.ts index 2742856..1505a5e 100644 --- a/projects/kit/src/lib/realtime/kit-realtime-connection.spec.ts +++ b/projects/kit/src/lib/realtime/kit-realtime-connection.spec.ts @@ -164,6 +164,18 @@ describe('KitRealtimeConnection', () => { connection.stop(); }); + it('requests a REST resync when the initial socket connection is fully established', async () => { + const connection = new TestConnection(); + const reconnected = vi.fn(); + connection.reconnected$.subscribe(reconnected); + await connection.openForTest(); + + connection.sockets[0].open(); + + expect(reconnected).toHaveBeenCalledOnce(); + connection.stop(); + }); + it('emits resync after a partial multi-target connection failure recovers', async () => { vi.useFakeTimers(); const connection = new TestConnection(); diff --git a/projects/kit/src/lib/realtime/kit-realtime-connection.ts b/projects/kit/src/lib/realtime/kit-realtime-connection.ts index e2bbf2b..57cc6be 100644 --- a/projects/kit/src/lib/realtime/kit-realtime-connection.ts +++ b/projects/kit/src/lib/realtime/kit-realtime-connection.ts @@ -113,8 +113,6 @@ export abstract class KitRealtimeConnection { #reconnectTimer: ReturnType | null = null; #pingTimer: ReturnType | null = null; #reconnectAttempt = 0; - #hasOpened = false; - #needsResync = false; #isAppActive = true; #isNetworkConnected = true; #lifecycleRegistration: Promise | null = null; @@ -122,7 +120,7 @@ export abstract class KitRealtimeConnection { /** All parsed events received by this connection. */ readonly events$: Observable> = this.#events$.asObservable(); - /** Emits once after every fully restored connection cycle, prompting consumers to resync via REST. */ + /** Emits after every fully established connection cycle, including the first, prompting a REST resync. */ readonly reconnected$: Observable = this.#reconnected$.asObservable(); /** Override timing/protocol defaults in specialized clients or tests. */ @@ -254,8 +252,6 @@ export abstract class KitRealtimeConnection { /** Reset resync and backoff history when the owning session ends. */ protected resetConnectionState(): void { - this.#hasOpened = false; - this.#needsResync = false; this.#reconnectAttempt = 0; } @@ -302,11 +298,7 @@ export abstract class KitRealtimeConnection { return; } this.#opening = false; - if (this.#hasOpened || this.#needsResync) { - this.#reconnected$.next(); - } - this.#hasOpened = true; - this.#needsResync = false; + this.#reconnected$.next(); }; socket.onmessage = ({ data }) => { if (generation !== this.#generation || typeof data !== 'string') { @@ -341,9 +333,6 @@ export abstract class KitRealtimeConnection { if (generation !== this.#generation) { return; } - if ([...this.#sockets].some((socket) => socket.readyState === WebSocket.OPEN)) { - this.#needsResync = true; - } this.#closeSockets(); void this.handleConnectionFailure() .catch(() => undefined)