mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-04-24 19:39:11 +00:00
debounce session fetch as it literally ddoses cloudflare
This commit is contained in:
parent
fcaf3af875
commit
140f9d566b
1 changed files with 38 additions and 26 deletions
|
@ -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]);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue