Skip to content

Commit

Permalink
ceval module isolated declaration fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Carbrex committed Jul 17, 2024
1 parent 0f40175 commit 8926a23
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 73 deletions.
62 changes: 31 additions & 31 deletions ui/ceval/src/ctrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ export default class CevalCtrl {
engines: Engines;
storedPv: Prop<number> = storedIntProp('ceval.multipv', 1);
storedMovetime: Prop<number> = storedIntProp('ceval.search-ms', 8000); // may be 'Infinity'
allowed = toggle(true);
allowed: Toggle = toggle(true);
enabled: Toggle;
download?: { bytes: number; total: number };
hovering = prop<Hovering | null>(null);
pvBoard = prop<PvBoard | null>(null);
isDeeper = toggle(false);
hovering: Prop<Hovering | null> = prop<Hovering | null>(null);
pvBoard: Prop<PvBoard | null> = prop<PvBoard | null>(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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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) },
Expand All @@ -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), {
Expand All @@ -219,7 +219,7 @@ export default class CevalCtrl {
);
}

get maxThreads() {
get maxThreads(): number {
return (
this.engines.external?.maxThreads ??
(fewerCores()
Expand All @@ -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) {
Expand All @@ -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);
}
Expand Down
18 changes: 9 additions & 9 deletions ui/ceval/src/engines/engines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, WithMake> {
type Hash = string;
type Variant = [VariantKey, Hash];
const variantMap = (v: VariantKey): string => (v === 'threeCheck' ? '3check' : v.toLowerCase());
Expand Down Expand Up @@ -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<boolean> {
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;
Expand All @@ -266,7 +266,7 @@ export class Engines {
return true;
}

updateCevalCtrl(ctrl: CevalCtrl) {
updateCevalCtrl(ctrl: CevalCtrl): void {
this.ctrl = ctrl;
}

Expand Down
16 changes: 8 additions & 8 deletions ui/ceval/src/engines/externalEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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();
}
}
12 changes: 6 additions & 6 deletions ui/ceval/src/engines/simpleEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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;
}
Expand Down
18 changes: 9 additions & 9 deletions ui/ceval/src/engines/stockfishWebEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -23,11 +23,11 @@ export class StockfishWebEngine implements CevalEngine {
});
}

getInfo() {
getInfo(): BrowserEngineInfo {
return this.info;
}

async boot() {
async boot(): Promise<void> {
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) => {
Expand Down Expand Up @@ -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
Expand All @@ -103,7 +103,7 @@ export class StockfishWebEngine implements CevalEngine {
};
}

getState() {
getState(): CevalState {
return this.failed
? CevalState.Failed
: !this.module
Expand All @@ -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;
};
Expand Down
Loading

0 comments on commit 8926a23

Please sign in to comment.