From c74036b8c6be4f23ffb893c59773a9cc3c05e4dc Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 29 Jul 2026 19:39:04 +0200 Subject: [PATCH] After the improving of performance3 --- .../candle-predict/CandlePredictPage.tsx | 11 ++--- .../candle-predict/hooks/useCandlePredict.ts | 41 ++++++++++++++----- .../candle-predict/hooks/useGameSocket.ts | 26 ++++++++++++ 3 files changed, 62 insertions(+), 16 deletions(-) diff --git a/prediqo_web/src/features/candle-predict/CandlePredictPage.tsx b/prediqo_web/src/features/candle-predict/CandlePredictPage.tsx index 3dd14660..80dd7248 100644 --- a/prediqo_web/src/features/candle-predict/CandlePredictPage.tsx +++ b/prediqo_web/src/features/candle-predict/CandlePredictPage.tsx @@ -101,11 +101,12 @@ export function CandlePredictPage() { const autoMut = useAutoPredictionSettings(); const balancePollMs = useMemo(() => { - if (!round) return 15_000; - if (round.status === 'locked') return 5_000; - if (round.status === 'betting') return 12_000; - if (round.status === 'settled' || round.outcome) return 8_000; - return 15_000; + // Balances refresh on bet/settle events; background poll is only a safety net. + if (!round) return 60_000; + if (round.status === 'locked') return 20_000; + if (round.status === 'betting') return 45_000; + if (round.status === 'settled' || round.outcome) return 30_000; + return 60_000; }, [round?.status, round?.outcome]); const balancesQ = useLiveBalances({ refetchIntervalMs: balancePollMs }); diff --git a/prediqo_web/src/features/candle-predict/hooks/useCandlePredict.ts b/prediqo_web/src/features/candle-predict/hooks/useCandlePredict.ts index 4eea257e..4cbcbfad 100644 --- a/prediqo_web/src/features/candle-predict/hooks/useCandlePredict.ts +++ b/prediqo_web/src/features/candle-predict/hooks/useCandlePredict.ts @@ -78,7 +78,7 @@ function withLightJitter(baseMs: number, ratio = 0.12): number { return Math.max(1000, baseMs + offset); } -export function useCandlePredictState(intervalMinutes?: number, pollMs = 3000) { +export function useCandlePredictState(intervalMinutes?: number, _pollMs = 3000) { const wsEnabled = isWsEnabled(); const socket = useGameSocket(intervalMinutes); const stateCacheKey = stateCacheKeyFor(intervalMinutes); @@ -95,11 +95,30 @@ export function useCandlePredictState(intervalMinutes?: number, pollMs = 3000) { if (res.status === 304 && cached) { return cached.data; } + const next = res.data; + // Avoid re-render when payload is timer-only churn (same round/status/pools). + if ( + cached?.data?.round?.id && + next?.round?.id === cached.data.round.id && + next?.round?.status === cached.data.round.status && + next?.round?.outcome === cached.data.round.outcome && + next?.round?.shortPool === cached.data.round.shortPool && + next?.round?.longPool === cached.data.round.longPool && + next?.userBet?.id === cached.data.userBet?.id && + next?.userMultiBet?.id === cached.data.userMultiBet?.id && + next?.autoSettings?.enabled === cached.data.autoSettings?.enabled + ) { + stateEtagCache.set(stateCacheKey, { + etag: String(res.headers.etag ?? cached.etag), + data: cached.data, + }); + return cached.data; + } stateEtagCache.set(stateCacheKey, { etag: String(res.headers.etag ?? ''), - data: res.data, + data: next, }); - return res.data; + return next; }, retry: (failureCount, error) => { const status = (error as { response?: { status?: number } })?.response?.status; @@ -107,19 +126,18 @@ export function useCandlePredictState(intervalMinutes?: number, pollMs = 3000) { return failureCount < 1; }, refetchInterval: (query) => { + // WS is primary for round transitions; HTTP is a rare safety net only. if (wsEnabled && socket.status === 'connected') { - const data = query.state.data; - if (data?.userMultiBet) return withLightJitter(8000); - if (data?.autoSettings?.enabled) return withLightJitter(7000); - if (data?.round?.status === 'betting') return withLightJitter(8000); - return withLightJitter(20000); + return withLightJitter(60_000, 0.05); } if (wsEnabled && (socket.status === 'reconnecting' || socket.status === 'stale')) { - return withLightJitter(2500, 0.08); + return withLightJitter(8_000, 0.08); } const status = query.state.data?.round?.status; - return withLightJitter(status === 'betting' ? 2000 : pollMs, 0.08); + return withLightJitter(status === 'betting' ? 5_000 : 15_000, 0.08); }, + refetchOnWindowFocus: false, + structuralSharing: true, }); } @@ -179,7 +197,7 @@ export function useMyCandlePredictions( ) { const pollMs = - opts?.roundStatus === 'betting' || opts?.autoEnabled ? 8000 : 20_000; + opts?.roundStatus === 'betting' || opts?.autoEnabled ? 30_000 : 60_000; @@ -214,6 +232,7 @@ export function useMyCandlePredictions( }, refetchInterval: pollMs, + refetchOnWindowFocus: false, }); diff --git a/prediqo_web/src/features/candle-predict/hooks/useGameSocket.ts b/prediqo_web/src/features/candle-predict/hooks/useGameSocket.ts index 6c6b04ae..bc107853 100644 --- a/prediqo_web/src/features/candle-predict/hooks/useGameSocket.ts +++ b/prediqo_web/src/features/candle-predict/hooks/useGameSocket.ts @@ -51,6 +51,28 @@ function mergeRound( return merged; } +/** True when UI-relevant round fields changed (ignore ticking countdown fields). */ +function roundUiChanged( + prev: GameState['round'] | null | undefined, + next: GameState['round'] | null | undefined +): boolean { + if (prev === next) return false; + if (!prev || !next) return true; + return ( + prev.id !== next.id || + prev.status !== next.status || + prev.outcome !== next.outcome || + prev.settledAt !== next.settledAt || + prev.candleOpenMs !== next.candleOpenMs || + prev.shortPool !== next.shortPool || + prev.longPool !== next.longPool || + prev.shortPoolPercent !== next.shortPoolPercent || + prev.longPoolPercent !== next.longPoolPercent || + prev.openPrice !== next.openPrice || + prev.closePrice !== next.closePrice + ); +} + function userBetForRound( userBet: GameState['userBet'], round: GameState['round'] @@ -136,6 +158,10 @@ export function useGameSocket( qc.setQueryData(key, (prev) => { if (!prev) return prev; const round = mergeRound(prev.round, msg.round); + if (!roundUiChanged(prev.round, round)) { + // Timer-only engine ticks: keep same object so the page does not re-render. + return prev; + } return { ...prev, round,