perform test #1
test to courses Signed-off-by: Kenneth Obsequio <k80308392@gmail.com>
This commit is contained in:
@@ -3,7 +3,8 @@ import api from "@/utils/api.util";
|
||||
|
||||
const ClientNotificationContext = createContext(null);
|
||||
|
||||
const POLL_INTERVAL = 60_000;
|
||||
const POLL_INTERVAL_NORMAL = 60_000;
|
||||
const POLL_INTERVAL_FAST = 10_000;
|
||||
|
||||
export function useClientNotifications() {
|
||||
const ctx = useContext(ClientNotificationContext);
|
||||
@@ -15,7 +16,8 @@ export function ClientNotificationProvider({ children }) {
|
||||
const [notifications, setNotifications] = useState([]);
|
||||
const [unseenCount, setUnseenCount] = useState(0);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const intervalRef = useRef(null);
|
||||
const intervalRef = useRef(null);
|
||||
const pollSpeedRef = useRef(POLL_INTERVAL_NORMAL);
|
||||
|
||||
const fetchUnseen = useCallback(async () => {
|
||||
try {
|
||||
@@ -62,9 +64,27 @@ export function ClientNotificationProvider({ children }) {
|
||||
}
|
||||
}, []);
|
||||
|
||||
const restartPoll = useCallback((interval) => {
|
||||
clearInterval(intervalRef.current);
|
||||
intervalRef.current = setInterval(fetchUnseen, interval);
|
||||
}, [fetchUnseen]);
|
||||
|
||||
// Speed up polling while a student is mid-assessment; restore when done
|
||||
const accelerate = useCallback(() => {
|
||||
if (pollSpeedRef.current === POLL_INTERVAL_FAST) return;
|
||||
pollSpeedRef.current = POLL_INTERVAL_FAST;
|
||||
restartPoll(POLL_INTERVAL_FAST);
|
||||
}, [restartPoll]);
|
||||
|
||||
const decelerate = useCallback(() => {
|
||||
if (pollSpeedRef.current === POLL_INTERVAL_NORMAL) return;
|
||||
pollSpeedRef.current = POLL_INTERVAL_NORMAL;
|
||||
restartPoll(POLL_INTERVAL_NORMAL);
|
||||
}, [restartPoll]);
|
||||
|
||||
useEffect(() => {
|
||||
fetchUnseen();
|
||||
intervalRef.current = setInterval(fetchUnseen, POLL_INTERVAL);
|
||||
intervalRef.current = setInterval(fetchUnseen, POLL_INTERVAL_NORMAL);
|
||||
return () => clearInterval(intervalRef.current);
|
||||
}, [fetchUnseen]);
|
||||
|
||||
@@ -76,6 +96,8 @@ export function ClientNotificationProvider({ children }) {
|
||||
fetchNotifications,
|
||||
markSeen,
|
||||
markAllSeen,
|
||||
accelerate,
|
||||
decelerate,
|
||||
}}>
|
||||
{children}
|
||||
</ClientNotificationContext.Provider>
|
||||
|
||||
Reference in New Issue
Block a user