// Web Worker that runs the heavy ML pipelines off the main thread. // Each call to ml-client spawns a fresh worker; this file is loaded once // per worker instance and torn down when the worker is terminated. // // On iOS / Android, the WebContent process can be killed under memory // pressure. Putting the model here means a model OOM kills the worker // (which we already throw away after each op) — the page itself stays // alive much more often. import { removeBackground } from './segmentation.js'; import { autoSegment } from './auto-segment.js'; self.onmessage = async (e) => { const { type, payload } = e.data || {}; const onProgress = (info) => { try { self.postMessage({ kind: 'progress', payload: info }); } catch (_) {} }; try { let result; if (type === 'remove-bg') { result = await removeBackground(payload.blob, onProgress); } else if (type === 'auto-segment') { result = await autoSegment(payload.blob, onProgress); } else { throw new Error(`Unknown task: ${type}`); } self.postMessage({ kind: 'result', payload: result }); } catch (err) { self.postMessage({ kind: 'error', payload: { message: err && err.message ? err.message : String(err) }, }); } };