From beea8854ce0dba36e37ffa80781528bc45300337 Mon Sep 17 00:00:00 2001 From: ZhenYi <434836402@qq.com> Date: Thu, 16 Apr 2026 19:34:07 +0800 Subject: [PATCH] fix(room): fix stale room subscribe after async connect MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit connect() is async/fire-and-forget — if the user switches rooms while WS is still connecting, the subscribeRoom() call captures the stale (activeRoomId) closure value and subscribes to the wrong room. Fix by re-reading activeRoomIdRef.current after the await so we always subscribe to the room that is active when the connection actually opens. --- src/contexts/room-context.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/contexts/room-context.tsx b/src/contexts/room-context.tsx index 0c36d56..09490bf 100644 --- a/src/contexts/room-context.tsx +++ b/src/contexts/room-context.tsx @@ -248,7 +248,11 @@ export function RoomProvider({ if (client.getStatus() !== 'open') { await client.connect(); } - await client.subscribeRoom(activeRoomId); + // 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?.(null); }; setup();