From 344e4b6c5bfa105bb03046b08dc99e7803d47bba Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 19 Apr 2021 16:15:24 +0100 Subject: [PATCH] Trigger lazy loading when filtering using spaces so that the filtered DMs are correct --- src/stores/SpaceStore.tsx | 20 ++++++++++++++++++++ src/stores/room-list/SpaceWatcher.ts | 16 +++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/stores/SpaceStore.tsx b/src/stores/SpaceStore.tsx index 52060f86a5..dcdbc73cb7 100644 --- a/src/stores/SpaceStore.tsx +++ b/src/stores/SpaceStore.tsx @@ -525,6 +525,26 @@ export class SpaceStoreClass extends AsyncStoreWithClient { this.notificationStateMap.set(key, state); return state; } + + // traverse space tree with DFS calling fn on each space including the given root one + public traverseSpace( + spaceId: string, + fn: (roomId: string) => void, + includeRooms = false, + parentPath?: Set, + ) { + if (parentPath && parentPath.has(spaceId)) return; // prevent cycles + + fn(spaceId); + + const newPath = new Set(parentPath).add(spaceId); + const [childSpaces, childRooms] = partitionSpacesAndRooms(this.getChildren(spaceId)); + + if (includeRooms) { + childRooms.forEach(r => fn(r.roomId)); + } + childSpaces.forEach(s => this.traverseSpace(s.roomId, fn, includeRooms, newPath)); + } } export default class SpaceStore { diff --git a/src/stores/room-list/SpaceWatcher.ts b/src/stores/room-list/SpaceWatcher.ts index d26f563a91..13e1d83901 100644 --- a/src/stores/room-list/SpaceWatcher.ts +++ b/src/stores/room-list/SpaceWatcher.ts @@ -28,12 +28,22 @@ export class SpaceWatcher { private activeSpace: Room = SpaceStore.instance.activeSpace; constructor(private store: RoomListStoreClass) { - this.filter.updateSpace(this.activeSpace); // get the filter into a consistent state + this.updateFilter(); // get the filter into a consistent state store.addFilter(this.filter); SpaceStore.instance.on(UPDATE_SELECTED_SPACE, this.onSelectedSpaceUpdated); } - private onSelectedSpaceUpdated = (activeSpace) => { - this.filter.updateSpace(this.activeSpace = activeSpace); + private onSelectedSpaceUpdated = (activeSpace: Room) => { + this.activeSpace = activeSpace; + this.updateFilter(); + }; + + private updateFilter = () => { + if (this.activeSpace) { + SpaceStore.instance.traverseSpace(this.activeSpace.roomId, roomId => { + this.store.matrixClient?.getRoom(roomId)?.loadMembersIfNeeded(); + }); + } + this.filter.updateSpace(this.activeSpace); }; }