Speed up waitForMember if user already in room (#11110)

* Speed up `waitForMember` if user already in room

`waitForMember` waits for a user to join, or be invited, to a room. But if the
user is already in the room (ie, we miss the `NewMember` event), we end up
timing out after 1500ms.

We can save 1.5s here by returning immediately.

* fix strict type errors

* stfu SonarCloud
This commit is contained in:
Richard van der Hoff 2023-06-19 15:11:25 +01:00 committed by GitHub
parent 6780287ecf
commit 889318d3a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 14 deletions

View file

@ -80,9 +80,7 @@ export function isJoinedOrNearlyJoined(membership: string): boolean {
}
/**
* Try to ensure the user is already in the megolm session before continuing
* NOTE: this assumes you've just created the room and there's not been an opportunity
* for other code to run, so we shouldn't miss RoomState.newMember when it comes by.
* Try to ensure the user is in the room (invited or joined) before continuing
*/
export async function waitForMember(
client: MatrixClient,
@ -92,6 +90,12 @@ export async function waitForMember(
): Promise<boolean> {
const { timeout } = opts;
let handler: (event: MatrixEvent, state: RoomState, member: RoomMember) => void;
// check if the user is in the room before we start -- in which case, no need to wait.
if ((client.getRoom(roomId)?.getMember(userId) ?? null) !== null) {
return true;
}
return new Promise<boolean>((resolve) => {
// eslint-disable-next-line @typescript-eslint/naming-convention
handler = function (_, __, member: RoomMember) {
@ -102,7 +106,7 @@ export async function waitForMember(
client.on(RoomStateEvent.NewMember, handler);
/* We don't want to hang if this goes wrong, so we proceed and hope the other
user is already in the megolm session */
user is already in the room */
window.setTimeout(resolve, timeout, false);
}).finally(() => {
client.removeListener(RoomStateEvent.NewMember, handler);