{"id":25774498,"url":"https://github.com/ericc-ch/worker-plus","last_synced_at":"2026-04-12T23:05:17.931Z","repository":{"id":277585874,"uuid":"932620429","full_name":"ericc-ch/worker-plus","owner":"ericc-ch","description":"Type-safe abstraction over Web Workers and Node.js Worker Threads with pool support","archived":false,"fork":false,"pushed_at":"2025-02-14T18:15:40.000Z","size":87,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-14T19:23:25.520Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ericc-ch.png","metadata":{"files":{"readme":"README.md","changelog":null,"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}},"created_at":"2025-02-14T08:06:27.000Z","updated_at":"2025-02-14T18:43:06.000Z","dependencies_parsed_at":"2025-02-14T19:33:36.431Z","dependency_job_id":null,"html_url":"https://github.com/ericc-ch/worker-plus","commit_stats":null,"previous_names":["ericc-ch/simple-worker"],"tags_count":2,"template":false,"template_full_name":"ericc-ch/starter-ts","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericc-ch%2Fworker-plus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericc-ch%2Fworker-plus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericc-ch%2Fworker-plus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericc-ch%2Fworker-plus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ericc-ch","download_url":"https://codeload.github.com/ericc-ch/worker-plus/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240983716,"owners_count":19888742,"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-02-27T05:30:10.100Z","updated_at":"2026-04-12T23:05:17.872Z","avatar_url":"https://github.com/ericc-ch.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# worker-plus\n\nA lightweight and efficient abstraction over Web Workers and Node.js Worker Threads, with built-in worker pool support.\n\n## Installation\n\n```bash\npnpm add worker-plus\n```\n\n## Usage\n\n### Basic Worker Usage\n\n#### Worker File (worker.ts)\n\n```typescript\nimport { expose } from \"worker-plus\";\n\nfunction heavyComputation(n: number) {\n  let result = 0;\n  for (let i = 0; i \u003c n; i++) {\n    result += Math.sin(i);\n  }\n  return result;\n}\n\nexpose(heavyComputation);\n```\n\n#### Main File (main.ts)\n\n```typescript\nimport { create } from \"worker-plus/node\";\nimport { Worker } from \"worker_threads\"; // or \"worker\" for web\n\n// For Node.js\nconst worker = new Worker(new URL(\"./worker.ts\", import.meta.url));\n// For Web\n// const worker = new Worker(new URL(\"./worker.ts\", import.meta.url), { type: \"module\" })\n\nconst instance = create\u003c(n: number) =\u003e number\u003e(worker);\n\nasync function run() {\n  const result = await instance.execute(1000000);\n  console.log(result);\n\n  // Clean up when done\n  instance.terminate();\n}\n\nrun();\n```\n\n### Worker Pool Usage\n\nWhen you need to process multiple tasks in parallel:\n\n```typescript\nimport { WorkerPool } from \"worker-plus/node\";\nimport { Worker } from \"worker_threads\"; // or \"worker\" for web\n\nasync function main() {\n  const pool = await WorkerPool.create({\n    createWorker: async () =\u003e {\n      const worker = new Worker(new URL(\"./worker.ts\", import.meta.url));\n      return create\u003c(n: number) =\u003e number\u003e(worker);\n    },\n    size: 4, // number of workers in the pool\n  });\n\n  // Execute tasks in parallel\n  const results = await Promise.all([\n    pool.execute(1000000),\n    pool.execute(2000000),\n    pool.execute(3000000),\n    pool.execute(4000000),\n  ]);\n\n  console.log(results);\n\n  // Clean up\n  pool.terminate();\n}\n\nmain();\n```\n\n## API Reference\n\n### `create(worker)`\n\nCreates a worker instance with type-safe request-response handling.\n\n- `worker`: Worker instance (Web Worker or Node.js Worker Thread)\n- Returns: `WorkerInstance\u003cF\u003e` where F is the type of the exposed function\n\n### `expose(fn)`\n\nExposes a function to handle messages in the worker thread.\n\n- `fn`: The function to expose to the main thread\n- Returns: `void`\n\n### `WorkerPool`\n\nManages a pool of workers for parallel processing.\n\n#### Static Methods\n\n- `create(options)`: Creates a new worker pool\n  - `options.createWorker`: Function that returns a new worker instance\n  - `options.size`: Number of workers in the pool (default: 1)\n\n#### Instance Methods\n\n- `execute(...params)`: Executes a task on the next available worker\n- `terminate()`: Terminates all workers in the pool\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fericc-ch%2Fworker-plus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fericc-ch%2Fworker-plus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fericc-ch%2Fworker-plus/lists"}