-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate Stockfish analysis to use async generator instead of ca…
…llback methods
- Loading branch information
1 parent
1c91d49
commit 9d1ee18
Showing
7 changed files
with
153 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,11 +137,6 @@ class Maia { | |
legalMoves, | ||
) | ||
|
||
console.log({ | ||
policy, | ||
value, | ||
}) | ||
|
||
return { | ||
policy, | ||
value, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,34 @@ | ||
import { useMemo } from 'react' | ||
|
||
import { useMemo, useRef, useCallback } from 'react' | ||
import Engine from './engine' | ||
import { StockfishEvaluation } from 'src/types' | ||
|
||
export const useStockfishEngine = ( | ||
callback: (message: StockfishEvaluation, fen: string) => void, | ||
) => { | ||
const engine = useMemo(() => new Engine(callback), []) | ||
return engine | ||
export const useStockfishEngine = () => { | ||
const engineRef = useRef<Engine | null>(null) | ||
|
||
if (!engineRef.current) { | ||
engineRef.current = new Engine() | ||
} | ||
|
||
const streamEvaluations = useCallback( | ||
(fen: string, legalMoveCount: number) => { | ||
if (!engineRef.current) { | ||
console.error('Engine not initialized') | ||
return null | ||
} | ||
return engineRef.current.streamEvaluations(fen, legalMoveCount) | ||
}, | ||
[], | ||
) | ||
|
||
const stopEvaluation = useCallback(() => { | ||
engineRef.current?.stopEvaluation() | ||
}, []) | ||
|
||
return useMemo( | ||
() => ({ | ||
streamEvaluations, | ||
stopEvaluation, | ||
}), | ||
[streamEvaluations, stopEvaluation], | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters