2022-05-19 02:46:50 +08:00
|
|
|
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';
|
2023-03-02 21:25:08 +08:00
|
|
|
import { throttle } from '/imports/utils/throttle';
|
2022-02-24 03:33:35 +08:00
|
|
|
|
2022-05-19 02:46:50 +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);
|
|
|
|
|
2022-05-19 02:46:50 +08:00
|
|
|
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);
|
2022-05-19 02:46:50 +08:00
|
|
|
throttledSetUsers.current();
|
2022-02-24 03:33:35 +08:00
|
|
|
}, [contextUsers]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
users,
|
|
|
|
isReady,
|
|
|
|
};
|
|
|
|
}
|