Conform more code to strict null checking (#10153)

* Conform more code to strict null checking

* Conform more code to strict null checking

* Iterate

* Iterate
This commit is contained in:
Michael Telatynski 2023-02-15 13:36:22 +00:00 committed by GitHub
parent a4ff959aa1
commit 145a5a8a8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
89 changed files with 520 additions and 551 deletions

View file

@ -78,7 +78,7 @@ const getSpaceContextKey = (space: SpaceKey): string => `mx_space_context_${spac
const partitionSpacesAndRooms = (arr: Room[]): [Room[], Room[]] => {
// [spaces, rooms]
return arr.reduce(
return arr.reduce<[Room[], Room[]]>(
(result, room: Room) => {
result[room.isSpaceRoom() ? 0 : 1].push(room);
return result;
@ -165,7 +165,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
return this.rootSpaces;
}
public get activeSpace(): SpaceKey {
public get activeSpace(): SpaceKey | undefined {
return this._activeSpace;
}
@ -228,7 +228,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
public setActiveSpace(space: SpaceKey, contextSwitch = true): void {
if (!space || !this.matrixClient || space === this.activeSpace) return;
let cliSpace: Room;
let cliSpace: Room | null = null;
if (!isMetaSpace(space)) {
cliSpace = this.matrixClient.getRoom(space);
if (!cliSpace?.isSpaceRoom()) return;
@ -246,6 +246,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
// else if the last viewed room in this space is joined then view that
// else view space home or home depending on what is being clicked on
if (
roomId &&
cliSpace?.getMyMembership() !== "invite" &&
this.matrixClient.getRoom(roomId)?.getMyMembership() === "join" &&
this.isRoomInSpace(space, roomId)
@ -348,10 +349,10 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
.filter((ev) => ev.getContent()?.via);
return (
sortBy(childEvents, (ev) => {
return getChildOrder(ev.getContent().order, ev.getTs(), ev.getStateKey());
return getChildOrder(ev.getContent().order, ev.getTs(), ev.getStateKey()!);
})
.map((ev) => {
const history = this.matrixClient.getRoomUpgradeHistory(ev.getStateKey(), true);
const history = this.matrixClient.getRoomUpgradeHistory(ev.getStateKey()!, true);
return history[history.length - 1];
})
.filter((room) => {
@ -373,7 +374,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
const userId = this.matrixClient?.getUserId();
const room = this.matrixClient?.getRoom(roomId);
return (
room?.currentState
(room?.currentState
.getStateEvents(EventType.SpaceParent)
.map((ev) => {
const content = ev.getContent();
@ -396,7 +397,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
return parent;
})
.filter(Boolean) || []
.filter(Boolean) as Room[]) || []
);
}
@ -467,7 +468,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
space: SpaceKey,
includeDescendantSpaces = true,
useCache = true,
): Set<string> => {
): Set<string> | undefined => {
if (space === MetaSpace.Home && this.allRoomsInHome) {
return undefined;
}
@ -490,7 +491,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
private markTreeChildren = (rootSpace: Room, unseen: Set<Room>): void => {
const stack = [rootSpace];
while (stack.length) {
const space = stack.pop();
const space = stack.pop()!;
unseen.delete(space);
this.getChildSpaces(space.roomId).forEach((space) => {
if (unseen.has(space)) {
@ -646,7 +647,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
const enabledMetaSpaces = new Set(this.enabledMetaSpaces);
const visibleRooms = this.matrixClient.getVisibleRooms();
let dmBadgeSpace: MetaSpace;
let dmBadgeSpace: MetaSpace | undefined;
// only show badges on dms on the most relevant space if such exists
if (enabledMetaSpaces.has(MetaSpace.People)) {
dmBadgeSpace = MetaSpace.People;
@ -702,8 +703,8 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
); // put all invites in the Home Space
};
private static isInSpace(member: RoomMember): boolean {
return member.membership === "join" || member.membership === "invite";
private static isInSpace(member?: RoomMember | null): boolean {
return member?.membership === "join" || member?.membership === "invite";
}
// Method for resolving the impact of a single user's membership change in the given Space and its hierarchy
@ -755,11 +756,14 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
this.rootSpaces.forEach((s) => {
// traverse each space tree in DFS to build up the supersets as you go up,
// reusing results from like subtrees.
const traverseSpace = (spaceId: string, parentPath: Set<string>): [Set<string>, Set<string>] => {
const traverseSpace = (
spaceId: string,
parentPath: Set<string>,
): [Set<string>, Set<string>] | undefined => {
if (parentPath.has(spaceId)) return; // prevent cycles
// reuse existing results if multiple similar branches exist
if (this.roomIdsBySpace.has(spaceId) && this.userIdsBySpace.has(spaceId)) {
return [this.roomIdsBySpace.get(spaceId), this.userIdsBySpace.get(spaceId)];
return [this.roomIdsBySpace.get(spaceId)!, this.userIdsBySpace.get(spaceId)!];
}
const [childSpaces, childRooms] = partitionSpacesAndRooms(this.getChildren(spaceId));
@ -865,7 +869,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
if (this.suggestedRooms.find((r) => r.room_id === roomId)) return;
// try to find the canonical parent first
let parent: SpaceKey = this.getCanonicalParent(roomId)?.roomId;
let parent: SpaceKey | undefined = this.getCanonicalParent(roomId)?.roomId;
// otherwise, try to find a root space which contains this room
if (!parent) {