element-web/src/utils/local-room.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

140 lines
4.7 KiB
TypeScript
Raw Normal View History

2022-07-11 05:33:37 +00:00
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { logger } from "matrix-js-sdk/src/logger";
import { ClientEvent, MatrixClient } from "matrix-js-sdk/src/matrix";
2022-07-11 05:33:37 +00:00
import defaultDispatcher from "../dispatcher/dispatcher";
import { LocalRoom, LocalRoomState } from "../models/LocalRoom";
import { isLocalRoom } from "./localRoom/isLocalRoom";
import { isRoomReady } from "./localRoom/isRoomReady";
2022-07-11 05:33:37 +00:00
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;
};
2022-07-11 05:33:37 +00:00
/**
* Does a room action:
* For non-local rooms it calls fn directly.
* For local rooms it adds the callback function to the room's afterCreateCallbacks and
* dispatches a "local_room_event".
*
* @async
* @template T
* @param {string} roomId Room ID of the target room
* @param {(actualRoomId: string) => Promise<T>} fn Callback to be called directly or collected at the local room
* @param {MatrixClient} [client]
* @returns {Promise<T>} Promise that gets resolved after the callback has finished
*/
export async function doMaybeLocalRoomAction<T>(
roomId: string,
fn: (actualRoomId: string) => Promise<T>,
client: MatrixClient,
2022-07-11 05:33:37 +00:00
): Promise<T> {
if (isLocalRoom(roomId)) {
2022-07-11 05:33:37 +00:00
const room = client.getRoom(roomId) as LocalRoom;
if (room.isCreated && isActualRoomIdDefined(room.actualRoomId)) {
2022-07-11 05:33:37 +00:00
return fn(room.actualRoomId);
}
return new Promise<T>((resolve, reject) => {
room.afterCreateCallbacks.push((newRoomId: string) => {
fn(newRoomId).then(resolve).catch(reject);
});
defaultDispatcher.dispatch({
action: "local_room_event",
roomId: room.roomId,
});
});
}
return fn(roomId);
}
/**
* Waits until a room is ready and then applies the after-create local room callbacks.
* Also implements a stopgap timeout after that a room is assumed to be ready.
*
* @see isRoomReady
* @async
* @param {MatrixClient} client
* @param {LocalRoom} localRoom
* @param actualRoomId Id of the actual room
2022-07-11 05:33:37 +00:00
* @returns {Promise<string>} Resolved to the actual room id
*/
export async function waitForRoomReadyAndApplyAfterCreateCallbacks(
client: MatrixClient,
localRoom: LocalRoom,
actualRoomId: string,
2022-07-11 05:33:37 +00:00
): Promise<string> {
if (isRoomReady(client, localRoom)) {
return applyAfterCreateCallbacks(localRoom, actualRoomId).then(() => {
2022-07-11 05:33:37 +00:00
localRoom.state = LocalRoomState.CREATED;
client.emit(ClientEvent.Room, localRoom);
return Promise.resolve(actualRoomId);
2022-07-11 05:33:37 +00:00
});
}
return new Promise((resolve, reject) => {
const finish = (): void => {
2022-07-11 05:33:37 +00:00
if (checkRoomStateIntervalHandle) clearInterval(checkRoomStateIntervalHandle);
if (stopgapTimeoutHandle) clearTimeout(stopgapTimeoutHandle);
applyAfterCreateCallbacks(localRoom, actualRoomId)
.then(() => {
localRoom.state = LocalRoomState.CREATED;
client.emit(ClientEvent.Room, localRoom);
resolve(actualRoomId);
})
.catch((err) => {
reject(err);
});
2022-07-11 05:33:37 +00:00
};
const stopgapFinish = (): void => {
2022-07-11 05:33:37 +00:00
logger.warn(`Assuming local room ${localRoom.roomId} is ready after hitting timeout`);
finish();
};
const checkRoomStateIntervalHandle = window.setInterval(() => {
if (isRoomReady(client, localRoom)) finish();
2022-07-11 05:33:37 +00:00
}, 500);
const stopgapTimeoutHandle = window.setTimeout(stopgapFinish, 5000);
2022-07-11 05:33:37 +00:00
});
}
/**
* Applies the after-create callback of a local room.
*
* @async
* @param {LocalRoom} localRoom
* @param {string} roomId
* @returns {Promise<void>} Resolved after all callbacks have been called
*/
async function applyAfterCreateCallbacks(localRoom: LocalRoom, roomId: string): Promise<void> {
for (const afterCreateCallback of localRoom.afterCreateCallbacks) {
await afterCreateCallback(roomId);
}
localRoom.afterCreateCallbacks = [];
}