@@ -31,6 +31,7 @@ export const useEngineAnalysis = (
3131 maia : { batchEvaluate : EngineHooks [ 'maia' ] [ 'batchEvaluate' ] } ,
3232 streamEvaluations : EngineHooks [ 'streamEvaluations' ] ,
3333 stopEvaluation : EngineHooks [ 'stopEvaluation' ] ,
34+ isStockfishReady : ( ) => boolean ,
3435 currentMaiaModel : string ,
3536 setAnalysisState : React . Dispatch < React . SetStateAction < number > > ,
3637) => {
@@ -63,15 +64,28 @@ export const useEngineAnalysis = (
6364 const board = new Chess ( currentNode . fen )
6465 const nodeFen = currentNode . fen
6566
66- ; ( async ( ) => {
67+ const attemptMaiaAnalysis = async ( ) => {
6768 if (
6869 ! currentNode ||
69- maiaStatus !== 'ready' ||
7070 currentNode . analysis . maia ||
7171 inProgressAnalyses . has ( nodeFen )
7272 )
7373 return
7474
75+ // Add retry logic for Maia initialization
76+ let retries = 0
77+ const maxRetries = 30 // 3 seconds with 100ms intervals
78+
79+ while ( retries < maxRetries && maiaStatus !== 'ready' ) {
80+ await new Promise ( resolve => setTimeout ( resolve , 100 ) )
81+ retries ++
82+ }
83+
84+ if ( maiaStatus !== 'ready' ) {
85+ console . warn ( 'Maia not ready after waiting, skipping analysis' )
86+ return
87+ }
88+
7589 inProgressAnalyses . add ( nodeFen )
7690
7791 try {
@@ -110,7 +124,9 @@ export const useEngineAnalysis = (
110124 } finally {
111125 inProgressAnalyses . delete ( nodeFen )
112126 }
113- } ) ( )
127+ }
128+
129+ attemptMaiaAnalysis ( )
114130 } , [
115131 maiaStatus ,
116132 currentNode ,
@@ -129,31 +145,51 @@ export const useEngineAnalysis = (
129145 )
130146 return
131147
132- const evaluationStream = streamEvaluations (
133- board . fen ( ) ,
134- board . moves ( ) . length ,
135- )
148+ // Add retry logic for Stockfish initialization
149+ const attemptStockfishAnalysis = async ( ) => {
150+ // Wait up to 3 seconds for Stockfish to be ready
151+ let retries = 0
152+ const maxRetries = 30 // 3 seconds with 100ms intervals
153+
154+ while ( retries < maxRetries && ! isStockfishReady ( ) ) {
155+ await new Promise ( resolve => setTimeout ( resolve , 100 ) )
156+ retries ++
157+ }
158+
159+ if ( ! isStockfishReady ( ) ) {
160+ console . warn ( 'Stockfish not ready after waiting, skipping analysis' )
161+ return
162+ }
163+
164+ const evaluationStream = streamEvaluations (
165+ board . fen ( ) ,
166+ board . moves ( ) . length ,
167+ )
136168
137- if ( evaluationStream ) {
138- ; ( async ( ) => {
139- for await ( const evaluation of evaluationStream ) {
140- if ( ! currentNode ) {
141- stopEvaluation ( )
142- break
169+ if ( evaluationStream ) {
170+ ; ( async ( ) => {
171+ for await ( const evaluation of evaluationStream ) {
172+ if ( ! currentNode ) {
173+ stopEvaluation ( )
174+ break
175+ }
176+ currentNode . addStockfishAnalysis ( evaluation , currentMaiaModel )
177+ setAnalysisState ( ( state ) => state + 1 )
143178 }
144- currentNode . addStockfishAnalysis ( evaluation , currentMaiaModel )
145- setAnalysisState ( ( state ) => state + 1 )
146- }
147- } ) ( )
179+ } ) ( )
180+ }
148181 }
149182
183+ attemptStockfishAnalysis ( )
184+
150185 return ( ) => {
151186 stopEvaluation ( )
152187 }
153188 } , [
154189 currentNode ,
155190 streamEvaluations ,
156191 stopEvaluation ,
192+ isStockfishReady ,
157193 currentMaiaModel ,
158194 setAnalysisState ,
159195 ] )
0 commit comments