Wait for the created room to come down sync before placing a group call

This commit is contained in:
Robin Townsend 2022-07-15 11:25:09 -04:00
parent 034552a063
commit b4a56f6dd7

View file

@ -8,6 +8,7 @@ import { MemoryCryptoStore } from "matrix-js-sdk/src/crypto/store/memory-crypto-
import { createClient, MatrixClient } from "matrix-js-sdk/src/matrix"; import { createClient, MatrixClient } from "matrix-js-sdk/src/matrix";
import { ICreateClientOpts } from "matrix-js-sdk/src/matrix"; import { ICreateClientOpts } from "matrix-js-sdk/src/matrix";
import { ClientEvent } from "matrix-js-sdk/src/client"; import { ClientEvent } from "matrix-js-sdk/src/client";
import { Room } from "matrix-js-sdk/src/models/room";
import { Visibility, Preset } from "matrix-js-sdk/src/@types/partials"; import { Visibility, Preset } from "matrix-js-sdk/src/@types/partials";
import { import {
GroupCallIntent, GroupCallIntent,
@ -226,6 +227,23 @@ export async function createRoom(
name: string, name: string,
isPtt = false isPtt = false
): Promise<string> { ): Promise<string> {
let setExpectedRoomId: (roomId: string) => void;
const expectedRoomId = new Promise<string>(
(resolve) => (setExpectedRoomId = resolve)
);
// There's no telling whether the new room will come down sync in the middle
// of the createRoom request, or after it, so start watching for it beforehand
const roomReceived = new Promise<void>((resolve) => {
const onRoom = async (room: Room) => {
if (room.roomId === (await expectedRoomId)) {
resolve();
client.off(ClientEvent.Room, onRoom);
}
};
client.on(ClientEvent.Room, onRoom);
});
const createRoomResult = await client.createRoom({ const createRoomResult = await client.createRoom({
visibility: Visibility.Private, visibility: Visibility.Private,
preset: Preset.PublicChat, preset: Preset.PublicChat,
@ -256,6 +274,10 @@ export async function createRoom(
}, },
}); });
// Wait for the room to come down sync before doing anything with it
setExpectedRoomId(createRoomResult.room_id);
await roomReceived;
console.log(`Creating ${isPtt ? "PTT" : "video"} group call room`); console.log(`Creating ${isPtt ? "PTT" : "video"} group call room`);
await client.createGroupCall( await client.createGroupCall(