Build a Web Worker pool that distributes CPU-intensive tasks across threads. Task queue, load balancing, and progress reporting.
## Task Web Worker pool for distributing CPU-intensive work across browser threads. ## Requirements - TypeScript - No external dependencies - Works in all modern browsers ## API ```typescript const pool = new WorkerPool({ workerUrl: "/workers/processor.js", poolSize: navigator.hardwareConcurrency || 4, taskTimeout: 30_000, // 30s max per task }); // Submit tasks const result = await pool.exec("processImage", { imageData: arrayBuffer, filters: ["sharpen", "resize"], }); // Submit with progress const result = await pool.exec("trainModel", data, { onProgress: (pct) => setProgress(pct), // 0-100 }); // Batch processing const results = await pool.map(items, "processItem"); // Cleanup pool.terminate(); ``` ## Implementation Notes 1. Round-robin task assignment with busy tracking 2. Transfer ArrayBuffers (zero-copy) for large data 3. Structured clone for regular objects 4. Timeout handling: kill worker and spawn replacement 5. Error isolation: one worker crash doesn't affect others 6. Idle timeout: terminate unused workers after 60s, respawn on demand 7. Progress: worker posts incremental messages, pool forwards to caller
No gallery images yet.