1/// <reference lib="webworker" />
2/**
3 * Runs one algorithm on one dataset off the main thread, so a slow or
4 * broken candidate cannot freeze the page. One request, one response;
5 * the runner keeps a single worker and feeds it sequentially, which also
6 * makes per-algorithm timing honest.
7 */
8import { runAlgorithm } from './execute.ts';
9import type { TrainingArrays } from './training.ts';
10import type { Detection } from './types.ts';
12export interface AlgoRequest {
13 id: number;
14 source: string;
15 x: Float32Array;
16 fs: number;
17 training: TrainingArrays;
18}
20export interface AlgoResponse {
21 id: number;
22 ok: boolean;
23 detections?: Detection[];
24 logs: string[];
25 elapsedMs: number;
26 error?: string;
27}
29self.onmessage = (e: MessageEvent<AlgoRequest>) => {
30 const { id, source, x, fs, training } = e.data;
31 try {
32 const out = runAlgorithm(source, x, fs, training);
33 const resp: AlgoResponse = { id, ok: true, ...out };
34 self.postMessage(resp);
35 } catch (err) {
36 const resp: AlgoResponse = {
37 id, ok: false, logs: [], elapsedMs: 0,
38 error: err instanceof Error ? err.message : String(err),
39 };
40 self.postMessage(resp);
41 }
42};