fix(room): fix stale room subscribe after async connect

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.
This commit is contained in:
ZhenYi 2026-04-16 19:34:07 +08:00
parent 7989f7ba4b
commit beea8854ce

View File

@ -248,7 +248,11 @@ export function RoomProvider({
if (client.getStatus() !== 'open') { if (client.getStatus() !== 'open') {
await client.connect(); 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); loadMoreRef.current?.(null);
}; };
setup(); setup();