///
/**
* Runs one algorithm on one dataset off the main thread, so a slow or
* broken candidate cannot freeze the page. One request, one response;
* the runner keeps a single worker and feeds it sequentially, which also
* makes per-algorithm timing honest.
*/
import { runAlgorithm } from './execute.ts';
import type { TrainingArrays } from './training.ts';
import type { Detection } from './types.ts';
export interface AlgoRequest {
id: number;
source: string;
x: Float32Array;
fs: number;
training: TrainingArrays;
}
export interface AlgoResponse {
id: number;
ok: boolean;
detections?: Detection[];
logs: string[];
elapsedMs: number;
error?: string;
}
self.onmessage = (e: MessageEvent) => {
const { id, source, x, fs, training } = e.data;
try {
const out = runAlgorithm(source, x, fs, training);
const resp: AlgoResponse = { id, ok: true, ...out };
self.postMessage(resp);
} catch (err) {
const resp: AlgoResponse = {
id, ok: false, logs: [], elapsedMs: 0,
error: err instanceof Error ? err.message : String(err),
};
self.postMessage(resp);
}
};