Skip to content

Commit 2476678

Browse files
fix: prevent spamming backend
1 parent e6b086c commit 2476678

File tree

3 files changed

+15
-22
lines changed

3 files changed

+15
-22
lines changed

src/api/analysis/analysis.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,6 @@ export const getAnalyzedUserGame = async (
663663
} as AnalyzedGame
664664
}
665665

666-
// Types for engine analysis storage
667666
export interface EngineAnalysisPosition {
668667
ply: number
669668
fen: string
@@ -674,7 +673,6 @@ export interface EngineAnalysisPosition {
674673
}
675674
}
676675

677-
// Store client-side engine analysis to backend
678676
export const storeEngineAnalysis = async (
679677
gameId: string,
680678
analysisData: EngineAnalysisPosition[],

src/hooks/useAnalysisController/useAnalysisController.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,26 +79,26 @@ export const useAnalysisController = (
7979
currentNode: null,
8080
})
8181

82-
// Auto-save analysis state
8382
const [lastSavedCacheKey, setLastSavedCacheKey] = useState<string>('')
8483
const autoSaveTimerRef = useRef<NodeJS.Timeout | null>(null)
8584

86-
// Auto-save analysis data every 10 seconds
8785
const saveAnalysisToBackend = useCallback(async () => {
88-
// Only save for games that have a proper ID (not custom local games)
89-
if (!game.id || game.type === 'custom-pgn' || game.type === 'custom-fen') {
86+
if (
87+
!game.id ||
88+
game.type === 'custom-pgn' ||
89+
game.type === 'custom-fen' ||
90+
game.type === 'tournament'
91+
) {
9092
return
9193
}
9294

9395
try {
9496
const analysisData = collectEngineAnalysisData(game.tree)
9597

96-
// Only save if there's actually some analysis to save
9798
if (analysisData.length === 0) {
9899
return
99100
}
100101

101-
// Check if we have meaningful analysis (decent depth or Maia data)
102102
const hasMeaningfulAnalysis = analysisData.some(
103103
(pos) => (pos.stockfish && pos.stockfish.depth >= 12) || pos.maia,
104104
)
@@ -107,10 +107,9 @@ export const useAnalysisController = (
107107
return
108108
}
109109

110-
// Generate a cache key to avoid unnecessary saves
111110
const cacheKey = generateAnalysisCacheKey(analysisData)
112111
if (cacheKey === lastSavedCacheKey) {
113-
return // No new analysis since last save
112+
return
114113
}
115114

116115
await storeEngineAnalysis(game.id, analysisData)
@@ -126,6 +125,9 @@ export const useAnalysisController = (
126125
}
127126
}, [game.id, game.type, game.tree, lastSavedCacheKey])
128127

128+
const saveAnalysisToBackendRef = useRef(saveAnalysisToBackend)
129+
saveAnalysisToBackendRef.current = saveAnalysisToBackend
130+
129131
// Setup auto-save timer
130132
useEffect(() => {
131133
// Clear existing timer
@@ -134,24 +136,19 @@ export const useAnalysisController = (
134136
}
135137

136138
// Set up new timer to save every 10 seconds
137-
autoSaveTimerRef.current = setInterval(saveAnalysisToBackend, 10000)
139+
autoSaveTimerRef.current = setInterval(() => {
140+
saveAnalysisToBackendRef.current()
141+
}, 10000)
138142

139143
// Cleanup on unmount or game change - save one last time
140144
return () => {
141145
if (autoSaveTimerRef.current) {
142146
clearInterval(autoSaveTimerRef.current)
143147
}
144148
// Final save when component unmounts or game changes
145-
saveAnalysisToBackend()
149+
saveAnalysisToBackendRef.current()
146150
}
147-
}, [saveAnalysisToBackend])
148-
149-
// Save immediately when analysis state changes (new analysis available)
150-
useEffect(() => {
151-
// Debounce the save to avoid too frequent calls
152-
const timeoutId = setTimeout(saveAnalysisToBackend, 1000)
153-
return () => clearTimeout(timeoutId)
154-
}, [analysisState, saveAnalysisToBackend])
151+
}, [game.id])
155152

156153
// Simple batch analysis functions that reuse existing analysis infrastructure
157154
const startGameAnalysis = useCallback(

src/lib/analysisStorage.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ export const collectEngineAnalysisData = (
2121
fen: node.fen,
2222
}
2323

24-
// Add Maia analysis if available
2524
if (node.analysis.maia) {
2625
position.maia = node.analysis.maia
2726
}
2827

29-
// Add Stockfish analysis if available
3028
if (node.analysis.stockfish) {
3129
position.stockfish = {
3230
depth: node.analysis.stockfish.depth,

0 commit comments

Comments
 (0)