Skip to content

Commit 217c6c1

Browse files
Merge pull request #105 from MegaAntiCheat/fix/ImproperIntervals
Backend reconnect polling: modify polling behaviour to prevent async spam
2 parents 92f1aca + 1885d4c commit 217c6c1

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

src/App/App.tsx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ const ConfigurationModal = ({ closeModal }: ConfigurationModalProps) => {
9191

9292
function App() {
9393
const { isMinimode } = useMinimode();
94+
const [isDead, setDead] = React.useState(false);
9495
const [currentPage, setCurrentPage] = React.useState(PAGES.PLAYER_LIST);
9596

9697
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -138,17 +139,15 @@ function App() {
138139
};
139140

140141
const verificationRoutine = async () => {
141-
let connected = false;
142-
let dead = false;
143-
do {
144-
connected = await isBackendConnected();
145-
if (!connected) {
146-
dead = true;
147-
await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait 1 second before retrying
142+
if (await isBackendConnected()) {
143+
if (isDead) {
144+
setDead(false);
145+
closeModal();
148146
}
149-
} while (!connected);
150-
if (dead) closeModal(); // If backend died, we need to remove the modal once it recovers.
151-
verifyConfigured();
147+
verifyConfigured();
148+
} else {
149+
if (!isDead) setDead(true);
150+
}
152151
};
153152

154153
React.useEffect(() => {
@@ -163,14 +162,12 @@ function App() {
163162

164163
// Don't verify backend if we're using fakedata (dev environment)
165164
if (useFakedata) return;
166-
167-
verificationRoutine();
168165
const intervalId = setInterval(verificationRoutine, 1000);
169166

170167
return () => {
171168
clearInterval(intervalId);
172169
};
173-
}, [currentPage]);
170+
}, [currentPage, isDead]);
174171

175172
return (
176173
<div className="App">

src/api/globals/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ export const COMMAND_ENDPOINT = `${APIURL}/commands/v1`;
1515
export const useFakedata = process.env.NODE_ENV?.includes('development');
1616

1717
export async function verifyBackend(): Promise<boolean> {
18-
return await fetch(SERVERFETCH)
18+
const controller = new AbortController();
19+
const timeoutId = setTimeout(() => controller.abort(), 500);
20+
21+
return await fetch(SERVERFETCH, { signal: controller.signal })
1922
.then((res) => res.ok)
20-
.catch(() => false);
23+
.catch(() => false)
24+
.finally(() => clearTimeout(timeoutId));
2125
}
2226

2327
export async function isBackendConfigured(): Promise<boolean> {

0 commit comments

Comments
 (0)