Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix possible soft crash from a race condition in space hierarchies #9254

Merged
merged 2 commits into from Sep 8, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/components/structures/SpaceHierarchy.tsx
Expand Up @@ -499,12 +499,12 @@ const INITIAL_PAGE_SIZE = 20;
export const useRoomHierarchy = (space: Room): {
loading: boolean;
rooms?: IHierarchyRoom[];
hierarchy: RoomHierarchy;
error: Error;
hierarchy?: RoomHierarchy;
error?: Error;
loadMore(pageSize?: number): Promise<void>;
} => {
const [rooms, setRooms] = useState<IHierarchyRoom[]>([]);
const [roomHierarchy, setHierarchy] = useState<RoomHierarchy>();
const [hierarchy, setHierarchy] = useState<RoomHierarchy>();
const [error, setError] = useState<Error | undefined>();

const resetHierarchy = useCallback(() => {
Expand All @@ -526,19 +526,21 @@ export const useRoomHierarchy = (space: Room): {
}));

const loadMore = useCallback(async (pageSize?: number) => {
if (roomHierarchy.loading || !roomHierarchy.canLoadMore || roomHierarchy.noSupport || error) return;
await roomHierarchy.load(pageSize).catch(setError);
setRooms(roomHierarchy.rooms);
}, [error, roomHierarchy]);
if (hierarchy.loading || !hierarchy.canLoadMore || hierarchy.noSupport || error) return;
await hierarchy.load(pageSize).catch(setError);
setRooms(hierarchy.rooms);
}, [error, hierarchy]);

// Only return the hierarchy if it is for the space requested
let hierarchy = roomHierarchy;
if (hierarchy?.root !== space) {
hierarchy = undefined;
return {
loading: true,
loadMore,
};
}

return {
loading: hierarchy?.loading ?? true,
loading: hierarchy.loading,
rooms,
hierarchy,
loadMore,
Expand Down