{"id":29227036,"url":"https://github.com/rwv/worker-pool","last_synced_at":"2025-07-03T09:09:03.659Z","repository":{"id":301317096,"uuid":"1008864004","full_name":"rwv/worker-pool","owner":"rwv","description":"👷 A Web Worker Pool to reduce latency","archived":false,"fork":false,"pushed_at":"2025-06-26T08:34:57.000Z","size":0,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-06-26T08:46:17.528Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rwv.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-06-26T07:58:02.000Z","updated_at":"2025-06-26T08:34:52.000Z","dependencies_parsed_at":"2025-06-26T08:46:20.117Z","dependency_job_id":"9b8a66a3-9501-4d10-9809-a9e6dcb1a09e","html_url":"https://github.com/rwv/worker-pool","commit_stats":null,"previous_names":["rwv/worker-pool"],"tags_count":1,"template":false,"template_full_name":"rwv/npm-pkg-template","purl":"pkg:github/rwv/worker-pool","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rwv%2Fworker-pool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rwv%2Fworker-pool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rwv%2Fworker-pool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rwv%2Fworker-pool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rwv","download_url":"https://codeload.github.com/rwv/worker-pool/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rwv%2Fworker-pool/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263296638,"owners_count":23444498,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-07-03T09:09:03.071Z","updated_at":"2025-07-03T09:09:03.637Z","avatar_url":"https://github.com/rwv.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @seedgou/worker-pool\n\n🔄 A lightweight and efficient Web Worker pool implementation for TypeScript/JavaScript applications.\n\n## Features\n\n- **Object Pool Pattern**: Reuses Web Workers to avoid the overhead of creating and destroying them\n- **Configurable Pool Size**: Set initial and maximum worker counts\n- **TypeScript Support**: Full TypeScript definitions included\n- **Zero Dependencies**: Lightweight with no external runtime dependencies\n- **Memory Efficient**: Automatically terminates excess workers when pool reaches capacity\n\n## Installation\n\n```bash\nnpm install @seedgou/worker-pool\n```\n\n```bash\npnpm add @seedgou/worker-pool\n```\n\n```bash\nyarn add @seedgou/worker-pool\n```\n\n## Quick Start\n\n```typescript\nimport { WorkerPool } from \"@seedgou/worker-pool\";\n\n// Create a worker pool\nconst pool = new WorkerPool(Worker, {\n  initialWorkers: 2,\n  maxWorkers: 5,\n});\n\n// Get a worker from the pool\nconst worker = pool.get();\n\n// Use the worker\nworker.postMessage({ type: \"process\", data: \"Hello World\" });\n\n// Return the worker to the pool when done\npool.return(worker);\n```\n\n## API Reference\n\n### `WorkerPool`\n\nThe main class for managing a pool of Web Workers.\n\n#### Constructor\n\n```typescript\nnew WorkerPool(workerConstructor, options?)\n```\n\n**Parameters:**\n\n- `workerConstructor`: Function that creates new Worker instances (typically the `Worker` class)\n- `options` (optional): Configuration object\n  - `initialWorkers` (optional): Number of workers to create initially (default: 0)\n  - `maxWorkers` (optional): Maximum number of workers to keep in the pool (default: Infinity)\n\n#### Methods\n\n##### `get(): Worker`\n\nGets a worker from the pool. If the pool is empty, creates and returns a new worker.\n\n**Returns:** A Worker instance ready for use\n\n##### `return(worker: Worker): void`\n\nReturns a worker to the pool for reuse. If the pool has reached its maximum capacity, the worker will be terminated instead.\n\n**Parameters:**\n\n- `worker`: The worker to return to the pool\n\n## Usage Examples\n\n### Basic Usage\n\n```typescript\nimport { WorkerPool } from \"@seedgou/worker-pool\";\n\n// Create a simple worker pool\nconst pool = new WorkerPool(Worker);\n\n// Get a worker and use it\nconst worker = pool.get();\nworker.postMessage(\"Hello from main thread\");\n\nworker.onmessage = (event) =\u003e {\n  console.log(\"Received:\", event.data);\n  // Return worker to pool when done\n  pool.return(worker);\n};\n```\n\n### With Configuration\n\n```typescript\nimport { WorkerPool } from \"@seedgou/worker-pool\";\n\n// Create a pool with initial workers and size limit\nconst pool = new WorkerPool(Worker, {\n  initialWorkers: 3, // Start with 3 workers\n  maxWorkers: 10, // Maximum 10 workers in pool\n});\n\n// The pool starts with 3 workers ready to use\nconst worker1 = pool.get(); // Gets one of the initial workers\nconst worker2 = pool.get(); // Gets another initial worker\nconst worker3 = pool.get(); // Gets the third initial worker\nconst worker4 = pool.get(); // Creates a new worker (pool was empty)\n\n// Return workers when done\npool.return(worker1);\npool.return(worker2);\npool.return(worker3);\npool.return(worker4);\n```\n\n### Processing Multiple Tasks\n\n```typescript\nimport { WorkerPool } from \"@seedgou/worker-pool\";\n\nconst pool = new WorkerPool(Worker, { maxWorkers: 4 });\n\nasync function processTasks(tasks: string[]) {\n  const promises = tasks.map(async (task) =\u003e {\n    const worker = pool.get();\n\n    return new Promise((resolve, reject) =\u003e {\n      worker.onmessage = (event) =\u003e {\n        resolve(event.data);\n        pool.return(worker);\n      };\n\n      worker.onerror = (error) =\u003e {\n        reject(error);\n        pool.return(worker);\n      };\n\n      worker.postMessage(task);\n    });\n  });\n\n  return Promise.all(promises);\n}\n\n// Usage\nconst tasks = [\"task1\", \"task2\", \"task3\", \"task4\", \"task5\"];\nprocessTasks(tasks).then((results) =\u003e {\n  console.log(\"All tasks completed:\", results);\n});\n```\n\n## Why Use a Worker Pool?\n\nWeb Workers are expensive to create and destroy. A worker pool provides several benefits:\n\n1. **Performance**: Reusing workers eliminates the overhead of worker creation/destruction\n2. **Memory Efficiency**: Limits the number of concurrent workers\n3. **Resource Management**: Automatically handles worker lifecycle\n4. **Scalability**: Better performance under high load\n\n## Development\n\n### Prerequisites\n\n- Node.js 18+\n- pnpm (recommended) or npm\n\n### Setup\n\n```bash\n# Install dependencies\npnpm install\n\n# Build the project\npnpm build\n\n# Run tests\npnpm test\n\n# Run tests with coverage\npnpm coverage\n\n# Lint code\npnpm lint\n\n# Format code\npnpm format\n```\n\n### Scripts\n\n- `build`: Compile TypeScript to JavaScript\n- `test`: Run tests with Vitest\n- `coverage`: Run tests with coverage report\n- `lint`: Lint and fix code\n- `lint-check`: Check linting without fixing\n- `format`: Format code with Prettier\n- `format-check`: Check formatting without fixing\n- `type-check`: Run TypeScript type checking\n\n## License\n\nMIT License - see [LICENSE](LICENSE) file for details.\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## Repository\n\n- **GitHub**: https://github.com/rwv/worker-pool\n- **Issues**: https://github.com/rwv/worker-pool/issues\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frwv%2Fworker-pool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frwv%2Fworker-pool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frwv%2Fworker-pool/lists"}