From 8926a23f5c790872a3c46ce98845ea4f981a16e4 Mon Sep 17 00:00:00 2001 From: Carbrex <95964955+Carbrex@users.noreply.github.com> Date: Wed, 17 Jul 2024 22:19:04 +0530 Subject: [PATCH] ceval module isolated declaration fixes --- ui/ceval/src/ctrl.ts | 62 +++++++++++----------- ui/ceval/src/engines/engines.ts | 18 +++---- ui/ceval/src/engines/externalEngine.ts | 16 +++--- ui/ceval/src/engines/simpleEngine.ts | 12 ++--- ui/ceval/src/engines/stockfishWebEngine.ts | 18 +++---- ui/ceval/src/engines/threadedEngine.ts | 14 ++--- ui/ceval/src/util.ts | 6 ++- ui/ceval/tsconfig.json | 3 +- 8 files changed, 76 insertions(+), 73 deletions(-) diff --git a/ui/ceval/src/ctrl.ts b/ui/ceval/src/ctrl.ts index 22faa6cc95646..91cc8191d0ae7 100644 --- a/ui/ceval/src/ctrl.ts +++ b/ui/ceval/src/ctrl.ts @@ -29,16 +29,16 @@ export default class CevalCtrl { engines: Engines; storedPv: Prop = storedIntProp('ceval.multipv', 1); storedMovetime: Prop = storedIntProp('ceval.search-ms', 8000); // may be 'Infinity' - allowed = toggle(true); + allowed: Toggle = toggle(true); enabled: Toggle; download?: { bytes: number; total: number }; - hovering = prop(null); - pvBoard = prop(null); - isDeeper = toggle(false); + hovering: Prop = prop(null); + pvBoard: Prop = prop(null); + isDeeper: Toggle = toggle(false); curEval: Tree.LocalEval | null = null; lastStarted: Started | false = false; // last started object (for going deeper even if stopped) - showEnginePrefs = toggle(false); + showEnginePrefs: Toggle = toggle(false); customSearch?: Search; private worker: CevalEngine | undefined; @@ -48,7 +48,7 @@ export default class CevalCtrl { this.engines = new Engines(this); } - configure(opts: CevalOpts) { + configure(opts: CevalOpts): void { this.opts = opts; this.possible = this.opts.possible; this.rules = lichessRules(this.opts.variant.key); @@ -64,7 +64,7 @@ export default class CevalCtrl { } } - onEmit = throttle(200, (ev: Tree.LocalEval, work: Work) => { + onEmit: (ev: Tree.LocalEval, work: Work) => void = throttle(200, (ev: Tree.LocalEval, work: Work) => { this.sortPvsInPlace(ev.pvs, work.ply % 2 === (work.threatMode ? 1 : 0) ? 'white' : 'black'); this.curEval = ev; this.opts.emit(ev, work); @@ -146,26 +146,26 @@ export default class CevalCtrl { }; }; - goDeeper = () => { + goDeeper = (): void => { if (!this.lastStarted) return; this.isDeeper(true); this.doStart(this.lastStarted.path, this.lastStarted.steps, this.lastStarted.threatMode); }; - stop = () => { + stop = (): void => { this.worker?.stop(); }; - start = (path: string, steps: Step[], threatMode?: boolean) => { + start = (path: string, steps: Step[], threatMode?: boolean): void => { this.isDeeper(false); this.doStart(path, steps, !!threatMode); }; - get state() { + get state(): CevalState { return this.worker?.getState() ?? CevalState.Initial; } - get search() { + get search(): Search { const s = { multiPv: this.storedPv(), by: { movetime: Math.min(this.storedMovetime(), this.engines.maxMovetime) }, @@ -175,41 +175,41 @@ export default class CevalCtrl { return s; } - get safeMovetime() { + get safeMovetime(): number { return Math.min(this.storedMovetime(), this.engines.maxMovetime); } - get isInfinite() { + get isInfinite(): boolean { return this.safeMovetime === Number.POSITIVE_INFINITY; } - get canGoDeeper() { + get canGoDeeper(): boolean { return this.state !== CevalState.Computing && (this.curEval?.depth ?? 0) < 99; } - get isComputing() { + get isComputing(): boolean { return this.state === CevalState.Computing; } - get isCacheable() { + get isCacheable(): boolean { return this.engines.active?.tech === 'NNUE'; } - get showingCloud() { + get showingCloud(): boolean { if (!this.lastStarted) return false; const curr = this.lastStarted.steps[this.lastStarted.steps.length - 1]; return !!curr.ceval?.cloud; } - setThreads = (threads: number) => site.storage.set('ceval.threads', threads.toString()); + setThreads = (threads: number): void => site.storage.set('ceval.threads', threads.toString()); - get threads() { + get threads(): number { const stored = site.storage.get('ceval.threads'); const desired = stored ? parseInt(stored) : this.recommendedThreads; return clamp(desired, { min: this.engines.active?.minThreads ?? 1, max: this.maxThreads }); } - get recommendedThreads() { + get recommendedThreads(): number { return ( this.engines.external?.maxThreads ?? clamp(navigator.hardwareConcurrency - (navigator.hardwareConcurrency % 2 ? 0 : 1), { @@ -219,7 +219,7 @@ export default class CevalCtrl { ); } - get maxThreads() { + get maxThreads(): number { return ( this.engines.external?.maxThreads ?? (fewerCores() @@ -228,33 +228,33 @@ export default class CevalCtrl { ); } - setHashSize = (hash: number) => site.storage.set('ceval.hash-size', hash.toString()); + setHashSize = (hash: number): void => site.storage.set('ceval.hash-size', hash.toString()); - get hashSize() { + get hashSize(): number { const stored = site.storage.get('ceval.hash-size'); return Math.min(this.maxHash, stored ? parseInt(stored, 10) : 16); } - get maxHash() { + get maxHash(): number { return this.engines.active?.maxHash ?? 16; } - selectEngine = (id: string) => { + selectEngine = (id: string): void => { this.engines.select(id); this.opts.onSelectEngine?.(); }; - setHovering = (fen: FEN, uci?: Uci) => { + setHovering = (fen: FEN, uci?: Uci): void => { this.hovering(uci ? { fen, uci } : null); this.opts.setAutoShapes(); }; - setPvBoard = (pvBoard: PvBoard | null) => { + setPvBoard = (pvBoard: PvBoard | null): void => { this.pvBoard(pvBoard); this.opts.redraw(); }; - toggle = () => { + toggle = (): void => { if (!this.possible || !this.allowed()) return; this.stop(); if (!this.enabled() && !document.hidden) { @@ -268,12 +268,12 @@ export default class CevalCtrl { } }; - destroy = () => { + destroy = (): void => { this.worker?.destroy(); this.worker = undefined; }; - engineFailed(msg: string) { + engineFailed(msg: string): void { if (msg.includes('Blocking on the main thread')) return; // mostly harmless showEngineError(this.engines.active?.name ?? 'Engine', msg); } diff --git a/ui/ceval/src/engines/engines.ts b/ui/ceval/src/engines/engines.ts index 55fe19b6d5b22..e67bea7f5a429 100644 --- a/ui/ceval/src/engines/engines.ts +++ b/ui/ceval/src/engines/engines.ts @@ -30,13 +30,13 @@ export class Engines { this.selectProp = storedStringProp('ceval.engine', this.localEngines[0].id); } - status = (status: { download?: { bytes: number; total: number }; error?: string } = {}) => { + status = (status: { download?: { bytes: number; total: number }; error?: string } = {}): void => { if (this.ctrl.enabled()) this.ctrl.download = status.download; if (status.error) this.ctrl.engineFailed(status.error); this.ctrl.opts.redraw(); }; - makeEngineMap() { + makeEngineMap(): Map { type Hash = string; type Variant = [VariantKey, Hash]; const variantMap = (v: VariantKey): string => (v === 'threeCheck' ? '3check' : v.toLowerCase()); @@ -235,29 +235,29 @@ export class Engines { ); } - get active() { + get active(): EngineInfo | undefined { return this._active ?? this.activate(); } - activate() { + activate(): EngineInfo | undefined { this._active = this.getEngine({ id: this.selectProp(), variant: this.ctrl.opts.variant.key }); return this._active; } - select(id: string) { + select(id: string): void { this.selectProp(id); this.activate(); } - get external() { + get external(): EngineInfo | undefined { return this.active && 'endpoint' in this.active ? this.active : undefined; } - get maxMovetime() { + get maxMovetime(): number { return this.external ? 30 * 1000 : Number.POSITIVE_INFINITY; // broker timeouts prevent long search } - async deleteExternal(id: string) { + async deleteExternal(id: string): Promise { if (this.externalEngines.every(e => e.id !== id)) return false; const r = await fetch(`/api/external-engine/${id}`, { method: 'DELETE', headers: xhrHeader }); if (!r.ok) return false; @@ -266,7 +266,7 @@ export class Engines { return true; } - updateCevalCtrl(ctrl: CevalCtrl) { + updateCevalCtrl(ctrl: CevalCtrl): void { this.ctrl = ctrl; } diff --git a/ui/ceval/src/engines/externalEngine.ts b/ui/ceval/src/engines/externalEngine.ts index b265725aea165..9519c9764d58a 100644 --- a/ui/ceval/src/engines/externalEngine.ts +++ b/ui/ceval/src/engines/externalEngine.ts @@ -22,24 +22,24 @@ export class ExternalEngine implements CevalEngine { constructor( private opts: ExternalEngineInfo, - private status?: EngineNotifier, + private status?: EngineNotifier | undefined, ) {} - getState() { + getState(): CevalState { return this.state; } - getInfo() { + getInfo(): ExternalEngineInfo { return this.opts; } - start(work: Work) { + start(work: Work): void { this.stop(); this.state = CevalState.Loading; this.process(work); } - process = throttle(700, work => { + process: (work: Work) => void = throttle(700, (work: Work) => { this.req = new AbortController(); this.analyse(work, this.req.signal); }); @@ -95,15 +95,15 @@ export class ExternalEngine implements CevalEngine { } } - stop() { + stop(): void { this.req?.abort(); } - engineName() { + engineName(): string { return this.opts.name; } - destroy() { + destroy(): void { this.stop(); } } diff --git a/ui/ceval/src/engines/simpleEngine.ts b/ui/ceval/src/engines/simpleEngine.ts index ca123e7507444..ab59c3c7ad0fa 100644 --- a/ui/ceval/src/engines/simpleEngine.ts +++ b/ui/ceval/src/engines/simpleEngine.ts @@ -11,11 +11,11 @@ export class SimpleEngine implements CevalEngine { this.url = `${info.assets.root}/${info.assets.js}`; } - getInfo() { + getInfo(): BrowserEngineInfo { return this.info; } - getState() { + getState(): CevalState { return !this.worker ? CevalState.Initial : this.failed @@ -27,7 +27,7 @@ export class SimpleEngine implements CevalEngine { : CevalState.Idle; } - start(work: Work) { + start(work: Work): void { this.protocol.compute(work); if (!this.worker) { @@ -45,15 +45,15 @@ export class SimpleEngine implements CevalEngine { } } - stop() { + stop(): void { this.protocol.compute(undefined); } - engineName() { + engineName(): string | undefined { return this.protocol.engineName; } - destroy() { + destroy(): void { this.worker?.terminate(); this.worker = undefined; } diff --git a/ui/ceval/src/engines/stockfishWebEngine.ts b/ui/ceval/src/engines/stockfishWebEngine.ts index 17554a11c6e49..1949920227499 100644 --- a/ui/ceval/src/engines/stockfishWebEngine.ts +++ b/ui/ceval/src/engines/stockfishWebEngine.ts @@ -12,7 +12,7 @@ export class StockfishWebEngine implements CevalEngine { constructor( readonly info: BrowserEngineInfo, - readonly status?: EngineNotifier, + readonly status?: EngineNotifier | undefined, readonly variantMap?: (v: VariantKey) => string, ) { this.protocol = new Protocol(variantMap); @@ -23,11 +23,11 @@ export class StockfishWebEngine implements CevalEngine { }); } - getInfo() { + getInfo(): BrowserEngineInfo { return this.info; } - async boot() { + async boot(): Promise { const [version, root, js] = [this.info.assets.version, this.info.assets.root, this.info.assets.js]; const makeModule = await import(site.asset.url(`${root}/${js}`, { version, documentOrigin: true })); const module: StockfishWeb = await new Promise((resolve, reject) => { @@ -89,7 +89,7 @@ export class StockfishWebEngine implements CevalEngine { } makeErrorHandler(module: StockfishWeb) { - return (msg: string) => { + return (msg: string): void => { site.log(this.info.assets.js, msg); if (msg.startsWith('BAD_NNUE') && this.store) { // if we got this from IDB, we must remove it. but wait for getModels::store.put to finish first @@ -103,7 +103,7 @@ export class StockfishWebEngine implements CevalEngine { }; } - getState() { + getState(): CevalState { return this.failed ? CevalState.Failed : !this.module @@ -113,10 +113,10 @@ export class StockfishWebEngine implements CevalEngine { : CevalState.Idle; } - start = (work?: Work) => this.protocol.compute(work); - stop = () => this.protocol.compute(undefined); - engineName = () => this.protocol.engineName; - destroy = () => { + start = (work?: Work): void => this.protocol.compute(work); + stop = (): void => this.protocol.compute(undefined); + engineName = (): string | undefined => this.protocol.engineName; + destroy = (): void => { this.module?.uci('quit'); this.module = undefined; }; diff --git a/ui/ceval/src/engines/threadedEngine.ts b/ui/ceval/src/engines/threadedEngine.ts index 0485bd8f85c04..659b6b2f913bf 100644 --- a/ui/ceval/src/engines/threadedEngine.ts +++ b/ui/ceval/src/engines/threadedEngine.ts @@ -32,21 +32,21 @@ export class ThreadedEngine implements CevalEngine { constructor( readonly info: BrowserEngineInfo, - readonly status?: EngineNotifier, + readonly status?: EngineNotifier | undefined, readonly variantMap?: (v: string) => string, ) {} - onError = (err: Error) => { + onError = (err: Error): void => { console.error(err); this.failed = err; this.status?.({ error: String(err) }); }; - getInfo() { + getInfo(): BrowserEngineInfo { return this.info; } - getState() { + getState(): CevalState { return !this.protocol ? CevalState.Initial : this.failed @@ -115,7 +115,7 @@ export class ThreadedEngine implements CevalEngine { this.module = sf; } - async start(work: Work) { + async start(work: Work): Promise { if (!this.protocol) { this.protocol = new Protocol(this.variantMap); this.boot().catch(this.onError); @@ -123,11 +123,11 @@ export class ThreadedEngine implements CevalEngine { this.protocol.compute(work); } - stop() { + stop(): void { this.protocol.compute(undefined); } - destroy() { + destroy(): void { this.module?.postMessage('quit'); this.module = undefined; } diff --git a/ui/ceval/src/util.ts b/ui/ceval/src/util.ts index 884bfc5848f78..b34a042b79a50 100644 --- a/ui/ceval/src/util.ts +++ b/ui/ceval/src/util.ts @@ -19,7 +19,9 @@ export function sanIrreversible(variant: VariantKey, san: string): boolean { return variant === 'threeCheck' && san.includes('+'); } -export const fewerCores = memoize(() => isMobile() || navigator.userAgent.includes('CrOS')); +export const fewerCores: () => boolean = memoize( + () => isMobile() || navigator.userAgent.includes('CrOS'), +); export const sharedWasmMemory = (lo: number, hi = 32767): WebAssembly.Memory => { let shrink = 4; // 32767 -> 24576 -> 16384 -> 12288 -> 8192 -> 6144 -> etc @@ -34,7 +36,7 @@ export const sharedWasmMemory = (lo: number, hi = 32767): WebAssembly.Memory => } }; -export function showEngineError(engine: string, error: string) { +export function showEngineError(engine: string, error: string): void { console.log(error); domDialog({ class: 'engine-error', diff --git a/ui/ceval/tsconfig.json b/ui/ceval/tsconfig.json index c13e3e41a2d2a..a9f020667df5a 100644 --- a/ui/ceval/tsconfig.json +++ b/ui/ceval/tsconfig.json @@ -5,7 +5,8 @@ "outDir": "./dist", "rootDir": "src", "types": ["lichess", "web"], - "composite": true + "composite": true, + "isolatedDeclarations": true }, "references": [{ "path": "../common/tsconfig.json" }] }