From 29e063d88ddc6e85980905024099334609e3b539 Mon Sep 17 00:00:00 2001 From: nessi Date: Sun, 2 Aug 2026 09:22:48 +0200 Subject: [PATCH] Implement dynamic lobby polling with faster refresh rate and prevent concurrent requests Added faster polling interval (700ms) for pre-game lobby to show joining players in near real-time, while keeping slower 2.5s refresh for active games. Implemented pending request flag to prevent concurrent API calls from overlapping. Added `cache: "no-store"` to fetch configuration to ensure fresh data on every request. Updated useEffect dependency array to re-initialize polling when game start state changes. --- frontend/src/App.jsx | 13 +++++++++---- frontend/src/api/client.js | 3 ++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 35b3d74..1b71f6e 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -225,18 +225,23 @@ export default function App() { // eslint-disable-next-line react-hooks/exhaustive-deps }, [gameId]); - // ✅ Live refresh (Members/Meta) – damit neue Joiner ohne Reload sichtbar sind - // Für 5–6 Spieler reicht 2.5s völlig, ist "live genug" und schont Backend. + // ✅ Live refresh (Members/Meta) – Lobby schnell, laufendes Spiel genügsamer. useEffect(() => { if (!me || !gameId) return; let alive = true; + let pending = false; + const refreshInterval = gameMeta?.started ? 2500 : 700; const tick = async () => { + if (pending) return; + pending = true; try { await loadGameMeta(); // refresh members + winner meta } catch { // ignore + } finally { + pending = false; } }; @@ -246,14 +251,14 @@ export default function App() { const id = setInterval(() => { if (!alive) return; tick(); - }, 2500); + }, refreshInterval); return () => { alive = false; clearInterval(id); }; // eslint-disable-next-line react-hooks/exhaustive-deps - }, [me?.id, gameId]); + }, [me?.id, gameId, gameMeta?.started]); useEffect(() => { // wid kann auch "" sein (kein Sieger) diff --git a/frontend/src/api/client.js b/frontend/src/api/client.js index 7926117..55ad1e4 100644 --- a/frontend/src/api/client.js +++ b/frontend/src/api/client.js @@ -3,9 +3,10 @@ import { API_BASE } from "../constants"; export async function api(path, opts = {}) { const res = await fetch(API_BASE + path, { credentials: "include", + cache: "no-store", headers: { "Content-Type": "application/json" }, ...opts, }); if (!res.ok) throw new Error(await res.text()); return res.json(); -} \ No newline at end of file +}