Skip to content

Commit 51d6274

Browse files
fix: fix legacy analysis controller to work with stockfish streaming
1 parent d46cf61 commit 51d6274

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

src/hooks/useLegacyAnalysisController/useLegacyAnalysisController.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,7 @@ export const useLegacyAnalysisController = (
3232
) => {
3333
const controller = useGameController(game, initialIndex, initialOrientation)
3434

35-
const parseStockfishEvaluation = (
36-
message: StockfishEvaluation,
37-
moveIndex: number,
38-
) => {
39-
setStockfishEvaluations((prev) => {
40-
const newEvaluations = [...prev]
41-
newEvaluations[moveIndex] = message
42-
43-
return newEvaluations
44-
})
45-
}
46-
47-
const engine = useStockfishEngine(parseStockfishEvaluation)
35+
const { streamEvaluations, stopEvaluation } = useStockfishEngine()
4836
const [currentMove, setCurrentMove] = useState<null | [string, string]>(null)
4937
const [stockfishEvaluations, setStockfishEvaluations] = useState<
5038
StockfishEvaluation[]
@@ -57,15 +45,37 @@ export const useLegacyAnalysisController = (
5745

5846
useEffect(() => {
5947
if (game.type === 'tournament') return
60-
if (stockfishEvaluations[controller.currentIndex]?.depth == 18) return
6148

6249
const board = new Chess(game.moves[controller.currentIndex].board)
63-
engine.evaluatePosition(
50+
if (stockfishEvaluations[controller.currentIndex]?.depth == 18) return
51+
52+
const evaluationStream = streamEvaluations(
6453
board.fen(),
6554
board.moves().length,
66-
controller.currentIndex,
6755
)
68-
}, [controller.currentIndex, game.moves, game.type, engine])
56+
57+
if (evaluationStream) {
58+
;(async () => {
59+
for await (const evaluation of evaluationStream) {
60+
setStockfishEvaluations((prev) => {
61+
const newEvaluations = [...prev]
62+
newEvaluations[controller.currentIndex] = evaluation
63+
return newEvaluations
64+
})
65+
}
66+
})()
67+
}
68+
69+
return () => {
70+
stopEvaluation()
71+
}
72+
}, [
73+
controller.currentIndex,
74+
game.moves,
75+
game.type,
76+
streamEvaluations,
77+
stopEvaluation,
78+
])
6979

7080
const moves = useMemo(() => {
7181
const moveMap = new Map<string, string[]>()

0 commit comments

Comments
 (0)