Skip to content

Commit 679a91c

Browse files
fix: merge bg analysis drill-change detection into enqueue effect
React runs useEffects in declaration order. The separate cancellation effect was firing AFTER the enqueue effect, clearing the queue that was just populated. Merged both into a single effect so drill-change reset and node enqueuing happen atomically. Also handle drill-cleared case (cancel background when no active drill). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent f57a1c8 commit 679a91c

1 file changed

Lines changed: 21 additions & 16 deletions

File tree

src/hooks/useOpeningDrillController/useOpeningDrillController.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -947,9 +947,28 @@ export const useOpeningDrillController = (
947947
// eslint-disable-next-line react-hooks/exhaustive-deps
948948
}, [])
949949

950-
// Enqueue new drill positions for background analysis
950+
// Enqueue new drill positions for background analysis.
951+
// Also handles drill-change detection (must be in the same effect to avoid
952+
// the cancellation effect clearing the queue after the enqueue effect fills it).
953+
const bgDrillIdRef = useRef<string | null>(null)
951954
useEffect(() => {
952-
if (!currentDrillGame || isAnalyzingDrill) return
955+
if (!currentDrillGame || isAnalyzingDrill) {
956+
// No active drill or post-drill analysis is running — cancel background
957+
if (bgDrillIdRef.current !== null) {
958+
bgCancelledRef.current = true
959+
bgQueueRef.current = []
960+
bgDrillIdRef.current = null
961+
}
962+
return
963+
}
964+
965+
// If drill changed, reset background state for the new drill
966+
if (currentDrillGame.id !== bgDrillIdRef.current) {
967+
bgCancelledRef.current = true
968+
bgQueueRef.current = []
969+
bgDrillIdRef.current = currentDrillGame.id
970+
bgCancelledRef.current = false
971+
}
953972

954973
const mainLine = gameTree.getMainLine()
955974
const openingEndNode = currentDrillGame.openingEndNode
@@ -984,20 +1003,6 @@ export const useOpeningDrillController = (
9841003
treeController.currentNode,
9851004
])
9861005

987-
// Cancel background analysis when a new drill starts
988-
const bgDrillIdRef = useRef<string | null>(null)
989-
useEffect(() => {
990-
const drillId = currentDrillGame?.id ?? null
991-
if (drillId !== bgDrillIdRef.current) {
992-
bgCancelledRef.current = true
993-
bgQueueRef.current = []
994-
bgDrillIdRef.current = drillId
995-
if (drillId) {
996-
bgCancelledRef.current = false
997-
}
998-
}
999-
}, [currentDrillGame?.id])
1000-
10011006
// Helper: stop background loop and wait for it to fully exit
10021007
const stopBackgroundAnalysis = useCallback(async () => {
10031008
bgCancelledRef.current = true

0 commit comments

Comments
 (0)