Apply strictNullChecks to src/utils/local-room.ts (#10915)

* Stricity local room

* Handle error
This commit is contained in:
Michael Weimann 2023-06-01 13:55:26 +02:00 committed by GitHub
parent b5727cb463
commit ca53b11aa9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 14 deletions

View file

@ -89,7 +89,7 @@ export async function createRoomFromLocalRoom(client: MatrixClient, localRoom: L
if (!roomId) throw new Error(`startDm for local room ${localRoom.roomId} didn't return a room Id`);
localRoom.actualRoomId = roomId;
return waitForRoomReadyAndApplyAfterCreateCallbacks(client, localRoom);
return waitForRoomReadyAndApplyAfterCreateCallbacks(client, localRoom, roomId);
},
() => {
logger.warn(`Error creating DM for local room ${localRoom.roomId}`);

View file

@ -22,6 +22,15 @@ import { LocalRoom, LocalRoomState } from "../models/LocalRoom";
import { isLocalRoom } from "./localRoom/isLocalRoom";
import { isRoomReady } from "./localRoom/isRoomReady";
const isActualRoomIdDefined = (actualRoomId: string | undefined): actualRoomId is string => {
if (actualRoomId === undefined) {
// should not happen
throw new Error("Local room in CREATED state without actual room Id occurred");
}
return true;
};
/**
* Does a room action:
* For non-local rooms it calls fn directly.
@ -43,7 +52,7 @@ export async function doMaybeLocalRoomAction<T>(
if (isLocalRoom(roomId)) {
const room = client.getRoom(roomId) as LocalRoom;
if (room.isCreated) {
if (room.isCreated && isActualRoomIdDefined(room.actualRoomId)) {
return fn(room.actualRoomId);
}
@ -69,29 +78,35 @@ export async function doMaybeLocalRoomAction<T>(
* @async
* @param {MatrixClient} client
* @param {LocalRoom} localRoom
* @param actualRoomId Id of the actual room
* @returns {Promise<string>} Resolved to the actual room id
*/
export async function waitForRoomReadyAndApplyAfterCreateCallbacks(
client: MatrixClient,
localRoom: LocalRoom,
actualRoomId: string,
): Promise<string> {
if (isRoomReady(client, localRoom)) {
return applyAfterCreateCallbacks(localRoom, localRoom.actualRoomId).then(() => {
return applyAfterCreateCallbacks(localRoom, actualRoomId).then(() => {
localRoom.state = LocalRoomState.CREATED;
client.emit(ClientEvent.Room, localRoom);
return Promise.resolve(localRoom.actualRoomId);
return Promise.resolve(actualRoomId);
});
}
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
const finish = (): void => {
if (checkRoomStateIntervalHandle) clearInterval(checkRoomStateIntervalHandle);
if (stopgapTimeoutHandle) clearTimeout(stopgapTimeoutHandle);
applyAfterCreateCallbacks(localRoom, localRoom.actualRoomId).then(() => {
applyAfterCreateCallbacks(localRoom, actualRoomId)
.then(() => {
localRoom.state = LocalRoomState.CREATED;
client.emit(ClientEvent.Room, localRoom);
resolve(localRoom.actualRoomId);
resolve(actualRoomId);
})
.catch((err) => {
reject(err);
});
};

View file

@ -196,7 +196,11 @@ describe("direct-messages", () => {
const result = await dmModule.createRoomFromLocalRoom(mockClient, localRoom);
expect(result).toBe(room1.roomId);
expect(localRoom.state).toBe(LocalRoomState.CREATING);
expect(waitForRoomReadyAndApplyAfterCreateCallbacks).toHaveBeenCalledWith(mockClient, localRoom);
expect(waitForRoomReadyAndApplyAfterCreateCallbacks).toHaveBeenCalledWith(
mockClient,
localRoom,
room1.roomId,
);
});
});
});

View file

@ -108,7 +108,11 @@ describe("local-room", () => {
});
it("should invoke the callbacks, set the room state to created and return the actual room id", async () => {
const result = await localRoomModule.waitForRoomReadyAndApplyAfterCreateCallbacks(client, localRoom);
const result = await localRoomModule.waitForRoomReadyAndApplyAfterCreateCallbacks(
client,
localRoom,
room1.roomId,
);
expect(localRoom.state).toBe(LocalRoomState.CREATED);
expect(localRoomCallbackRoomId).toBe(room1.roomId);
expect(result).toBe(room1.roomId);
@ -121,7 +125,11 @@ describe("local-room", () => {
});
it("should invoke the callbacks, set the room state to created and return the actual room id", async () => {
const prom = localRoomModule.waitForRoomReadyAndApplyAfterCreateCallbacks(client, localRoom);
const prom = localRoomModule.waitForRoomReadyAndApplyAfterCreateCallbacks(
client,
localRoom,
room1.roomId,
);
jest.advanceTimersByTime(5000);
const roomId = await prom;
expect(localRoom.state).toBe(LocalRoomState.CREATED);
@ -137,7 +145,11 @@ describe("local-room", () => {
});
it("should invoke the callbacks, set the room state to created and return the actual room id", async () => {
const prom = localRoomModule.waitForRoomReadyAndApplyAfterCreateCallbacks(client, localRoom);
const prom = localRoomModule.waitForRoomReadyAndApplyAfterCreateCallbacks(
client,
localRoom,
room1.roomId,
);
mocked(isRoomReady).mockReturnValue(true);
jest.advanceTimersByTime(500);
const roomId = await prom;