/// /** Runs the classifier off the main thread so a slow or broken custom * algorithm cannot freeze the page. One request, one response. */ import { runAlgorithm } from './execute.ts'; import type { Detection, TrainingArrays } from './types.ts'; export interface ClfRequest { id: number; source: string; x: Float32Array; fs: number; training: TrainingArrays; } export interface ClfResponse { id: number; ok: boolean; detections?: Detection[]; logs: string[]; error?: string; } self.onmessage = (e: MessageEvent) => { const { id, source, x, fs, training } = e.data; try { const out = runAlgorithm(source, x, fs, training); const resp: ClfResponse = { id, ok: true, ...out }; self.postMessage(resp); } catch (err) { const resp: ClfResponse = { id, ok: false, logs: [], error: err instanceof Error ? err.message : String(err), }; self.postMessage(resp); } };