From 44c9f2c772756e9927af5b520f9198441d1f483b Mon Sep 17 00:00:00 2001 From: ZhenYi <434836402@qq.com> Date: Fri, 17 Apr 2026 21:44:45 +0800 Subject: [PATCH] fix(room): ensure WS connect before subscribe; reactions load for IDB path - setup() now awaits client.connect() before subscribeRoom and loadMore, ensuring the connection is truly open so WS is used for both - subscribeRoom / reactionListBatch: already WS-first via RoomWsClient.request() - IDB paths (initial + loadMore) now call thisLoadReactions to batch-fetch reactions via WS with HTTP fallback, fixing the missing reactions bug --- src/contexts/room-context.tsx | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/contexts/room-context.tsx b/src/contexts/room-context.tsx index 4dbc3c4..aa539ad 100644 --- a/src/contexts/room-context.tsx +++ b/src/contexts/room-context.tsx @@ -241,19 +241,19 @@ export function RoomProvider({ if (!activeRoomId || !client) return; const setup = async () => { - if (client.getStatus() !== 'open') { - await client.connect(); - } - // Re-check: activeRoomId may have changed while we were waiting for connect. - // Use activeRoomIdRef to get the *current* room, not the stale closure value. - const roomId = activeRoomIdRef.current; - if (!roomId) return; - await client.subscribeRoom(roomId); - // loadMoreRef.current is null on first mount (set later in render order). - // Call loadMore directly to ensure initial message fetch always runs. + // 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. loadMore(null); }; - setup(); + setup().catch(() => {}); return () => { client.unsubscribeRoom(activeRoomId).catch(() => {});