diff --git a/src/hooks/useEngine.ts b/src/hooks/useEngine.ts index 23ac970..6bd2d44 100644 --- a/src/hooks/useEngine.ts +++ b/src/hooks/useEngine.ts @@ -26,7 +26,7 @@ export const useEngine = ( return newEngine; }); }); - }, [engineName]); + }, [engineName, workersNb]); return engine; }; diff --git a/src/lib/lichess.ts b/src/lib/lichess.ts index babeb8d..389a319 100644 --- a/src/lib/lichess.ts +++ b/src/lib/lichess.ts @@ -13,15 +13,7 @@ export const getLichessEval = async ( multiPv = 1 ): Promise => { try { - const controller = new AbortController(); - const timeoutId = setTimeout(() => controller.abort("timeout"), 200); - const res = await fetch( - `https://lichess.org/api/cloud-eval?fen=${fen}&multiPv=${multiPv}`, - { method: "GET", signal: controller.signal } - ); - clearTimeout(timeoutId); - - const data: LichessResponse = await res.json(); + const data = await fetchLichessEval(fen, multiPv); if ("error" in data) { if (data.error === LichessError.NotFound) { @@ -53,9 +45,7 @@ export const getLichessEval = async ( lines: linesToKeep, }; } catch (error) { - if (!isAbortError(error)) { - logErrorToSentry(error, { fen, multiPv }); - } + logErrorToSentry(error, { fen, multiPv }); return { bestMove: "", @@ -64,11 +54,6 @@ export const getLichessEval = async ( } }; -const isAbortError = (error: unknown): boolean => - error === "timeout" || - ((error instanceof Error || error instanceof DOMException) && - (error.name === "AbortError" || error.message === "timeout")); - export const getLichessUserRecentGames = async ( username: string ): Promise => { @@ -87,3 +72,21 @@ export const getLichessUserRecentGames = async ( return games; }; + +const fetchLichessEval = async ( + fen: string, + multiPv: number +): Promise> => { + try { + const res = await fetch( + `https://lichess.org/api/cloud-eval?fen=${fen}&multiPv=${multiPv}`, + { method: "GET", signal: AbortSignal.timeout(200) } + ); + + return res.json(); + } catch (error) { + console.error(error); + + return { error: LichessError.NotFound }; + } +};