debounce session fetch as it literally ddoses cloudflare

This commit is contained in:
41666 2022-02-01 21:05:48 -05:00
parent fcaf3af875
commit 140f9d566b

View file

@ -31,6 +31,16 @@ const SessionContext = React.createContext<SessionContextT>({
setupSession: () => {}, setupSession: () => {},
}); });
const debounce = (func: Function, timeout = 300) => {
let timer: any | undefined = undefined;
return (...args: any) => {
clearTimeout(timer);
timer = setTimeout(() => {
func.apply(this, args);
}, timeout);
};
};
export const useSessionContext = () => React.useContext(SessionContext); export const useSessionContext = () => React.useContext(SessionContext);
export const SessionContextProvider = (props: { children: React.ReactNode }) => { export const SessionContextProvider = (props: { children: React.ReactNode }) => {
@ -108,7 +118,8 @@ export const SessionContextProvider = (props: { children: React.ReactNode }) =>
// If not, lets fetch it from the server // If not, lets fetch it from the server
setLock(true); setLock(true);
fetchSession(session.sessionID) debounce(async (sessionID: string) => {
fetchSession(sessionID)
.then((sessionData) => { .then((sessionData) => {
if (sessionData) { if (sessionData) {
setSession({ setSession({
@ -136,6 +147,7 @@ export const SessionContextProvider = (props: { children: React.ReactNode }) =>
session.setupSession(null); session.setupSession(null);
setLock(false); setLock(false);
}); });
}, 2000)(session.sessionID!);
} }
}, [session, locked, setLock, fetch]); }, [session, locked, setLock, fetch]);