Skip to content

Commit

Permalink
race condition fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Marky @ Instant committed Sep 11, 2024
1 parent bbe49e4 commit 441af5a
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion client/packages/core/src/Reactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export default class Reactor {

/** @type {Record<string, {isConnected: boolean; error: any}>} */
_rooms = {};
/** @type {Record<string, boolean>} */
_roomsPendingLeave = {};
_presence = {};
_broadcastQueue = [];
_broadcastSubs = {};
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 441af5a

Please sign in to comment.