@@ -84,8 +84,6 @@ export class UciEngine {
8484 private async setMultiPv ( multiPv : number ) {
8585 if ( multiPv === this . multiPv ) return ;
8686
87- this . throwErrorIfNotReady ( ) ;
88-
8987 if ( multiPv < 2 || multiPv > 6 ) {
9088 throw new Error ( `Invalid MultiPV value : ${ multiPv } ` ) ;
9189 }
@@ -101,8 +99,6 @@ export class UciEngine {
10199 private async setElo ( elo : number ) {
102100 if ( elo === this . elo ) return ;
103101
104- this . throwErrorIfNotReady ( ) ;
105-
106102 if ( elo < 1320 || elo > 3190 ) {
107103 throw new Error ( `Invalid Elo value : ${ elo } ` ) ;
108104 }
@@ -142,12 +138,12 @@ export class UciEngine {
142138
143139 private terminateWorker ( worker : EngineWorker ) {
144140 console . log ( `Terminating worker from ${ this . enginePath } ` ) ;
145- worker . uci ( "quit" ) ;
146- worker . terminate ?.( ) ;
147141 worker . isReady = false ;
142+ worker . uci ( "quit" ) ;
143+ worker . terminate ( ) ;
148144 }
149145
150- public async stopSearch ( ) : Promise < void > {
146+ public async stopAllCurrentJobs ( ) : Promise < void > {
151147 this . workerQueue = [ ] ;
152148 await this . sendCommandsToEachWorker ( [ "stop" , "isready" ] , "readyok" ) ;
153149
@@ -255,10 +251,10 @@ export class UciEngine {
255251 workersNb = 1 ,
256252 } : EvaluateGameParams ) : Promise < GameEval > {
257253 this . throwErrorIfNotReady ( ) ;
258- setEvaluationProgress ?.( 1 ) ;
259- await this . setMultiPv ( multiPv ) ;
260254 this . isReady = false ;
255+ setEvaluationProgress ?.( 1 ) ;
261256
257+ await this . setMultiPv ( multiPv ) ;
262258 await this . sendCommandsToEachWorker ( [ "ucinewgame" , "isready" ] , "readyok" ) ;
263259 this . setWorkersNb ( workersNb ) ;
264260
@@ -308,7 +304,9 @@ export class UciEngine {
308304 updateEval ( i , result ) ;
309305 } )
310306 ) ;
307+
311308 await this . setWorkersNb ( 1 ) ;
309+ this . isReady = true ;
312310
313311 const positionsWithClassification = getMovesClassification (
314312 positions ,
@@ -322,7 +320,6 @@ export class UciEngine {
322320 playersRatings ?. black
323321 ) ;
324322
325- this . isReady = true ;
326323 return {
327324 positions : positionsWithClassification ,
328325 estimatedElo,
@@ -369,7 +366,7 @@ export class UciEngine {
369366
370367 const lichessEvalPromise = getLichessEval ( fen , multiPv ) ;
371368
372- await this . stopSearch ( ) ;
369+ await this . stopAllCurrentJobs ( ) ;
373370 await this . setMultiPv ( multiPv ) ;
374371
375372 const onNewMessage = ( messages : string [ ] ) => {
@@ -404,6 +401,8 @@ export class UciEngine {
404401 depth = 16
405402 ) : Promise < string | undefined > {
406403 this . throwErrorIfNotReady ( ) ;
404+
405+ await this . stopAllCurrentJobs ( ) ;
407406 await this . setElo ( elo ) ;
408407
409408 console . log ( `Evaluating position: ${ fen } ` ) ;
0 commit comments