From 4767e1d6928e41f10f6c9f347b8b311f6a1bcd06 Mon Sep 17 00:00:00 2001 From: ZhenYi <434836402@qq.com> Date: Fri, 17 Apr 2026 21:45:55 +0800 Subject: [PATCH] fix(room): IDB load no longer waits for WS connection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit loadMore(null) fires immediately — cached messages render instantly. WS connect runs in parallel; subscribeRoom and reaction batch-fetch use WS-first request() which falls back to HTTP transparently. --- src/contexts/room-context.tsx | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/contexts/room-context.tsx b/src/contexts/room-context.tsx index aa539ad..51019ec 100644 --- a/src/contexts/room-context.tsx +++ b/src/contexts/room-context.tsx @@ -241,17 +241,16 @@ export function RoomProvider({ if (!activeRoomId || !client) return; const setup = async () => { - // Ensure WS is open before subscribing. connect() is idempotent — if already - // connecting/open, it returns immediately. While connecting, messages load from - // IDB (instant) and reactions batch-fetch via WS-first request(). - await client.connect(); - // Guard: room may have changed while we were waiting for connect. - if (activeRoomIdRef.current !== activeRoomId) return; - // subscribeRoom is WS-first with HTTP fallback; failure is non-fatal. - client.subscribeRoom(activeRoomId).catch(() => {}); - // loadMore checks IDB first (no WS needed), then batch-fetches reactions - // via WS with HTTP fallback. Safe to call without awaiting. + // IDB load does NOT need WS — show cached messages immediately. + // loadMore checks IDB first, then falls back to API (WS-first + HTTP). loadMore(null); + + // Connect WS in parallel for real-time push + reactions batch-fetch. + // connect() is idempotent — no-op if already connecting/open. + // subscribeRoom uses WS-first request() with HTTP fallback. + await client.connect(); + if (activeRoomIdRef.current !== activeRoomId) return; + client.subscribeRoom(activeRoomId).catch(() => {}); }; setup().catch(() => {});