diff --git a/client/packages/core/src/Reactor.js b/client/packages/core/src/Reactor.js index 75e9207b1..da4b66aa0 100644 --- a/client/packages/core/src/Reactor.js +++ b/client/packages/core/src/Reactor.js @@ -79,6 +79,8 @@ export default class Reactor { /** @type {Record} */ _rooms = {}; + /** @type {Record} */ + _roomsPendingLeave = {}; _presence = {}; _broadcastQueue = []; _broadcastSubs = {}; @@ -395,6 +397,11 @@ export default class Reactor { const joinedRoom = this._rooms[loadingRoomId]; if (!joinedRoom) { + if (this._roomsPendingLeave[roomId]) { + this._tryLeaveRoom(loadingRoomId); + delete this._roomsPendingLeave[roomId]; + } + break; } @@ -1262,10 +1269,17 @@ export default class Reactor { !this._presence[roomId]?.handlers?.length && !Object.keys(this._broadcastSubs[roomId] ?? {}).length ) { + const room = this._rooms[roomId]; + delete this._rooms[roomId]; delete this._presence[roomId]; delete this._broadcastSubs[roomId]; - this._trySendAuthed(uuid(), { op: "leave-room", "room-id": roomId }); + + if (room.isConnected) { + this._tryLeaveRoom(roomId); + } else { + this._roomsPendingLeave[roomId] = true; + } } } @@ -1334,6 +1348,10 @@ export default class Reactor { this._trySendAuthed(uuid(), { op: "join-room", "room-id": roomId }); } + _tryLeaveRoom(roomId) { + this._trySendAuthed(uuid(), { op: "leave-room", "room-id": roomId }); + } + /** * @template {keyof RoomSchema} RoomType * @template {keyof RoomSchema[RoomType]['presence']} Keys