bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/components-data/users-context/service.js

32 lines
810 B
JavaScript
Raw Normal View History

import {
useState, useContext, useRef, useEffect,
} from 'react';
2022-02-24 03:33:35 +08:00
import { UsersContext } from '/imports/ui/components/components-data/users-context/context';
import { throttle } from '/imports/utils/throttle';
2022-02-24 03:33:35 +08:00
const USER_JOIN_UPDATE_THROTTLE_TIME = 1000;
2022-02-24 03:33:35 +08:00
export default function useContextUsers() {
const usingUsersContext = useContext(UsersContext);
const { users: contextUsers } = usingUsersContext;
const [users, setUsers] = useState(null);
const [isReady, setIsReady] = useState(true);
const throttledSetUsers = useRef(throttle(() => {
setUsers(contextUsers);
setIsReady(true);
},
USER_JOIN_UPDATE_THROTTLE_TIME, { trailing: true }));
2022-02-24 03:33:35 +08:00
useEffect(() => {
setIsReady(false);
throttledSetUsers.current();
2022-02-24 03:33:35 +08:00
}, [contextUsers]);
return {
users,
isReady,
};
}