import { MatrixClient , Room } from "@robertlong/matrix-js-sdk" ; export async function fetchRoom ( client , roomIdOrAlias , timeout = 5000 ) { const { roomId } = await client . joinRoom ( roomIdOrAlias ) ; return new Promise ( ( resolve , reject ) => { let timeoutId ; function onRoom ( room ) { if ( room && room . roomId === roomId ) { clearTimeout ( timeoutId ) ; client . removeListener ( "Room" , onRoom ) ; resolve ( room ) ; } } const room = client . getRoom ( roomId ) ; if ( room ) { resolve ( room ) ; } client . on ( "Room" , onRoom ) ; if ( timeout ) { timeoutId = setTimeout ( ( ) => { client . removeListener ( "Room" , onRoom ) ; reject ( new Error ( "Fetching room timed out." ) ) ; } , timeout ) ; } } ) ; }