Apply strictNullChecks
to src/utils/local-room.ts
(#10915)
* Stricity local room * Handle error
This commit is contained in:
parent
b5727cb463
commit
ca53b11aa9
4 changed files with 45 additions and 14 deletions
|
@ -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`);
|
if (!roomId) throw new Error(`startDm for local room ${localRoom.roomId} didn't return a room Id`);
|
||||||
|
|
||||||
localRoom.actualRoomId = roomId;
|
localRoom.actualRoomId = roomId;
|
||||||
return waitForRoomReadyAndApplyAfterCreateCallbacks(client, localRoom);
|
return waitForRoomReadyAndApplyAfterCreateCallbacks(client, localRoom, roomId);
|
||||||
},
|
},
|
||||||
() => {
|
() => {
|
||||||
logger.warn(`Error creating DM for local room ${localRoom.roomId}`);
|
logger.warn(`Error creating DM for local room ${localRoom.roomId}`);
|
||||||
|
|
|
@ -22,6 +22,15 @@ import { LocalRoom, LocalRoomState } from "../models/LocalRoom";
|
||||||
import { isLocalRoom } from "./localRoom/isLocalRoom";
|
import { isLocalRoom } from "./localRoom/isLocalRoom";
|
||||||
import { isRoomReady } from "./localRoom/isRoomReady";
|
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:
|
* Does a room action:
|
||||||
* For non-local rooms it calls fn directly.
|
* For non-local rooms it calls fn directly.
|
||||||
|
@ -43,7 +52,7 @@ export async function doMaybeLocalRoomAction<T>(
|
||||||
if (isLocalRoom(roomId)) {
|
if (isLocalRoom(roomId)) {
|
||||||
const room = client.getRoom(roomId) as LocalRoom;
|
const room = client.getRoom(roomId) as LocalRoom;
|
||||||
|
|
||||||
if (room.isCreated) {
|
if (room.isCreated && isActualRoomIdDefined(room.actualRoomId)) {
|
||||||
return fn(room.actualRoomId);
|
return fn(room.actualRoomId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,29 +78,35 @@ export async function doMaybeLocalRoomAction<T>(
|
||||||
* @async
|
* @async
|
||||||
* @param {MatrixClient} client
|
* @param {MatrixClient} client
|
||||||
* @param {LocalRoom} localRoom
|
* @param {LocalRoom} localRoom
|
||||||
|
* @param actualRoomId Id of the actual room
|
||||||
* @returns {Promise<string>} Resolved to the actual room id
|
* @returns {Promise<string>} Resolved to the actual room id
|
||||||
*/
|
*/
|
||||||
export async function waitForRoomReadyAndApplyAfterCreateCallbacks(
|
export async function waitForRoomReadyAndApplyAfterCreateCallbacks(
|
||||||
client: MatrixClient,
|
client: MatrixClient,
|
||||||
localRoom: LocalRoom,
|
localRoom: LocalRoom,
|
||||||
|
actualRoomId: string,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
if (isRoomReady(client, localRoom)) {
|
if (isRoomReady(client, localRoom)) {
|
||||||
return applyAfterCreateCallbacks(localRoom, localRoom.actualRoomId).then(() => {
|
return applyAfterCreateCallbacks(localRoom, actualRoomId).then(() => {
|
||||||
localRoom.state = LocalRoomState.CREATED;
|
localRoom.state = LocalRoomState.CREATED;
|
||||||
client.emit(ClientEvent.Room, localRoom);
|
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 => {
|
const finish = (): void => {
|
||||||
if (checkRoomStateIntervalHandle) clearInterval(checkRoomStateIntervalHandle);
|
if (checkRoomStateIntervalHandle) clearInterval(checkRoomStateIntervalHandle);
|
||||||
if (stopgapTimeoutHandle) clearTimeout(stopgapTimeoutHandle);
|
if (stopgapTimeoutHandle) clearTimeout(stopgapTimeoutHandle);
|
||||||
|
|
||||||
applyAfterCreateCallbacks(localRoom, localRoom.actualRoomId).then(() => {
|
applyAfterCreateCallbacks(localRoom, actualRoomId)
|
||||||
|
.then(() => {
|
||||||
localRoom.state = LocalRoomState.CREATED;
|
localRoom.state = LocalRoomState.CREATED;
|
||||||
client.emit(ClientEvent.Room, localRoom);
|
client.emit(ClientEvent.Room, localRoom);
|
||||||
resolve(localRoom.actualRoomId);
|
resolve(actualRoomId);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
reject(err);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -196,7 +196,11 @@ describe("direct-messages", () => {
|
||||||
const result = await dmModule.createRoomFromLocalRoom(mockClient, localRoom);
|
const result = await dmModule.createRoomFromLocalRoom(mockClient, localRoom);
|
||||||
expect(result).toBe(room1.roomId);
|
expect(result).toBe(room1.roomId);
|
||||||
expect(localRoom.state).toBe(LocalRoomState.CREATING);
|
expect(localRoom.state).toBe(LocalRoomState.CREATING);
|
||||||
expect(waitForRoomReadyAndApplyAfterCreateCallbacks).toHaveBeenCalledWith(mockClient, localRoom);
|
expect(waitForRoomReadyAndApplyAfterCreateCallbacks).toHaveBeenCalledWith(
|
||||||
|
mockClient,
|
||||||
|
localRoom,
|
||||||
|
room1.roomId,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -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 () => {
|
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(localRoom.state).toBe(LocalRoomState.CREATED);
|
||||||
expect(localRoomCallbackRoomId).toBe(room1.roomId);
|
expect(localRoomCallbackRoomId).toBe(room1.roomId);
|
||||||
expect(result).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 () => {
|
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);
|
jest.advanceTimersByTime(5000);
|
||||||
const roomId = await prom;
|
const roomId = await prom;
|
||||||
expect(localRoom.state).toBe(LocalRoomState.CREATED);
|
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 () => {
|
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);
|
mocked(isRoomReady).mockReturnValue(true);
|
||||||
jest.advanceTimersByTime(500);
|
jest.advanceTimersByTime(500);
|
||||||
const roomId = await prom;
|
const roomId = await prom;
|
||||||
|
|
Loading…
Reference in a new issue