Skip to content

Commit ee1dd22

Browse files
committed
Use new method to wait until a room is ready fopr group calls
We were waiting for the group call event handler to process the room, but only if we couldn't get the room from the client - if getRoom returned a room, we just wouldn't wait. This just uses promises rather than an event to wait for the room to be ready. Requires matrix-org/matrix-js-sdk#2641
1 parent 34d5e88 commit ee1dd22

File tree

1 file changed

+8
-31
lines changed

1 file changed

+8
-31
lines changed

src/room/useLoadGroupCall.ts

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,39 +45,15 @@ export const useLoadGroupCall = (
4545
useEffect(() => {
4646
setState({ loading: true });
4747

48-
const waitForRoom = async (roomId: string): Promise<Room> => {
49-
const room = client.getRoom(roomId);
50-
if (room) return room;
51-
console.log(`Room ${roomId} hasn't arrived yet: waiting`);
52-
53-
const waitPromise = new Promise<Room>((resolve) => {
54-
const onRoomEvent = async (room: Room) => {
55-
if (room.roomId === roomId) {
56-
client.removeListener(GroupCallEventHandlerEvent.Room, onRoomEvent);
57-
resolve(room);
58-
}
59-
};
60-
client.on(GroupCallEventHandlerEvent.Room, onRoomEvent);
61-
});
62-
63-
// race the promise with a timeout so we don't
64-
// wait forever for the room
65-
const timeoutPromise = new Promise<Room>((_, reject) => {
66-
setTimeout(() => {
67-
reject(new Error("Timed out trying to join room"));
68-
}, 30000);
69-
});
70-
71-
return Promise.race([waitPromise, timeoutPromise]);
72-
};
73-
7448
const fetchOrCreateRoom = async (): Promise<Room> => {
7549
try {
7650
const room = await client.joinRoom(roomIdOrAlias, { viaServers });
77-
logger.info(`Joined ${roomIdOrAlias}, waiting for Room event`);
78-
// wait for the room to come down the sync stream, otherwise
79-
// client.getRoom() won't return the room.
80-
return waitForRoom(room.roomId);
51+
logger.info(
52+
`Joined ${roomIdOrAlias}, waiting room to be ready for group calls`
53+
);
54+
await client.waitUntilRoomReadyForGroupCalls(room.roomId);
55+
logger.info(`${roomIdOrAlias}, is ready for group calls`);
56+
return room;
8157
} catch (error) {
8258
if (
8359
isLocalRoomId(roomIdOrAlias) &&
@@ -92,7 +68,8 @@ export const useLoadGroupCall = (
9268
createPtt
9369
);
9470
// likewise, wait for the room
95-
return await waitForRoom(roomId);
71+
await client.waitUntilRoomReadyForGroupCalls(roomId);
72+
return client.getRoom(roomId);
9673
} else {
9774
throw error;
9875
}

0 commit comments

Comments
 (0)