Enable lint rules for Promise handling to discourage misuse of them. (#2607)

* Enable lint rules for Promise handling to discourage misuse of them.
Squashed all of Hugh's commits into one.

---------

Co-authored-by: Hugh Nimmo-Smith <hughns@element.io>
This commit is contained in:
Timo
2024-09-10 09:49:35 +02:00
committed by GitHub
parent c30c8ac7d6
commit c3edd3e25e
35 changed files with 369 additions and 241 deletions

View File

@@ -260,34 +260,40 @@ export async function createRoom(
});
// Wait for the room to arrive
await new Promise<void>((resolve, reject) => {
const onRoom = async (room: Room): Promise<void> => {
if (room.roomId === (await createPromise).room_id) {
resolve();
cleanUp();
}
};
const roomId = await new Promise<string>((resolve, reject) => {
createPromise.catch((e) => {
reject(e);
cleanUp();
});
const onRoom = (room: Room): void => {
createPromise.then(
(result) => {
if (room.roomId === result.room_id) {
resolve(room.roomId);
cleanUp();
}
},
(e) => {
logger.error("Failed to wait for the room to arrive", e);
},
);
};
const cleanUp = (): void => {
client.off(ClientEvent.Room, onRoom);
};
client.on(ClientEvent.Room, onRoom);
});
const result = await createPromise;
let password;
let password: string | undefined;
if (e2ee == E2eeType.SHARED_KEY) {
password = secureRandomBase64Url(16);
saveKeyForRoom(result.room_id, password);
saveKeyForRoom(roomId, password);
}
return {
roomId: result.room_id,
roomId,
alias: e2ee ? undefined : fullAliasFromRoomName(name, client),
password,
};

View File

@@ -11,11 +11,11 @@ Please see LICENSE in the repository root for full details.
* @param devices The list of devices to search
* @returns A matching media device or undefined if no matching device was found
*/
export async function findDeviceByName(
export function findDeviceByName(
deviceName: string,
kind: MediaDeviceKind,
devices: MediaDeviceInfo[],
): Promise<string | undefined> {
): string | undefined {
const deviceInfo = devices.find(
(d) => d.kind === kind && d.label === deviceName,
);