Throttle RoomState.members handler to improve performance
Lazy Loading emits a RoomState.members for each member which in the case of a room like Matrix HQ means it happens over 8000 times in a very short span of time causing the UI to lock up.
This commit is contained in:
parent
603a1c8ffb
commit
184c73cca4
2 changed files with 34 additions and 10 deletions
31
src/hooks/useRoomMembers.ts
Normal file
31
src/hooks/useRoomMembers.ts
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
Copyright 2020 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 {useState} from "react";
|
||||
import {Room} from "matrix-js-sdk/src/models/room";
|
||||
import {RoomMember} from "matrix-js-sdk/src/models/room-member";
|
||||
|
||||
import {useEventEmitter} from "./useEventEmitter";
|
||||
import {throttle} from "lodash";
|
||||
|
||||
// Hook to simplify listening to Matrix Room members
|
||||
export const useRoomMembers = (room: Room, throttleWait = 250) => {
|
||||
const [members, setMembers] = useState<RoomMember[]>(room.getJoinedMembers());
|
||||
useEventEmitter(room.currentState, "RoomState.members", throttle(() => {
|
||||
setMembers(room.getJoinedMembers());
|
||||
}, throttleWait, {leading: true, trailing: true}));
|
||||
return members;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue