{"id":19848177,"url":"https://github.com/mys1024/worker-fn","last_synced_at":"2025-05-01T22:30:27.519Z","repository":{"id":223238742,"uuid":"759693063","full_name":"mys1024/worker-fn","owner":"mys1024","description":"worker-fn hides the complexity of communication between the JavaScript main thread and Worker threads, making it easy to call the functions defined in workers.","archived":false,"fork":false,"pushed_at":"2024-05-05T17:23:40.000Z","size":385,"stargazers_count":23,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-24T02:40:33.534Z","etag":null,"topics":["frontend","function","jsr","npm","package","web","webworker"],"latest_commit_sha":null,"homepage":"","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/mys1024.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":"2024-02-19T06:41:10.000Z","updated_at":"2025-03-12T14:18:32.000Z","dependencies_parsed_at":"2024-02-19T07:25:26.400Z","dependency_job_id":"d4722720-9dcf-4f6d-98a7-9fb7e87a35a0","html_url":"https://github.com/mys1024/worker-fn","commit_stats":{"total_commits":120,"total_committers":3,"mean_commits":40.0,"dds":0.01666666666666672,"last_synced_commit":"43824bba4820bf26fd2b7f39ad802a8d4044f07a"},"previous_names":["mys1024/worker-fn"],"tags_count":27,"template":false,"template_full_name":"mys1024/starter-node","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mys1024%2Fworker-fn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mys1024%2Fworker-fn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mys1024%2Fworker-fn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mys1024%2Fworker-fn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mys1024","download_url":"https://codeload.github.com/mys1024/worker-fn/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251954655,"owners_count":21670845,"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":["frontend","function","jsr","npm","package","web","webworker"],"created_at":"2024-11-12T13:16:25.081Z","updated_at":"2025-05-01T22:30:27.164Z","avatar_url":"https://github.com/mys1024.png","language":"TypeScript","readme":"\u003cdiv align=\"center\"\u003e\n\n# worker-fn\n\n[![jsr-version](https://img.shields.io/jsr/v/@mys/worker-fn?style=flat-square\u0026color=%23f7df1e)](https://jsr.io/@mys/worker-fn)\n[![npm-version](https://img.shields.io/npm/v/worker-fn?style=flat-square\u0026color=%23cb3837)](https://www.npmjs.com/package/worker-fn)\n[![npm-minzip](https://img.shields.io/bundlephobia/minzip/worker-fn?style=flat-square\u0026label=minzip)](https://bundlephobia.com/package/worker-fn)\n[![docs](https://img.shields.io/badge/docs-reference-blue?style=flat-square)](https://jsr.io/@mys/worker-fn/doc?style=flat-square)\n[![stars](https://img.shields.io/github/stars/mys1024/worker-fn?style=flat-square)](https://github.com/mys1024/worker-fn)\n[![license](https://img.shields.io/github/license/mys1024/worker-fn?\u0026style=flat-square)](./LICENSE)\n\n[![workflow-ci](https://img.shields.io/github/actions/workflow/status/mys1024/worker-fn/ci.yml?label=ci\u0026style=flat-square)](https://github.com/mys1024/worker-fn/actions/workflows/ci.yml)\n[![workflow-release](https://img.shields.io/github/actions/workflow/status/mys1024/worker-fn/release.yml?label=release\u0026style=flat-square)](https://github.com/mys1024/worker-fn/actions/workflows/release.yml)\n\nEnglish | [中文文档](./README_zh.md)\n\n\u003c/div\u003e\n\n`worker-fn` hides the complexity of communication between the JavaScript main\nthread and [Worker](https://developer.mozilla.org/docs/Web/API/Web_Workers_API)\nthreads, making it easy to call the functions defined in workers.\n\n`worker-fn` allows you to create **proxy functions** in the main thread that\ncall corresponding **worker functions** defined in worker threads by sending\nmessages. These proxy functions maintain the same function signatures as the\ncorresponding worker functions (except that the return values of the proxy\nfunctions are wrapped in Promises).\n\n![concepts](./docs/concepts.png)\n\n## Usage\n\n### Basic\n\n`math.worker.js`:\n\n```javascript\nimport { defineWorkerFn } from \"worker-fn\";\n\nfunction add(a, b) {\n  return a + b;\n}\n\nfunction fib(n) {\n  return n \u003c= 2 ? 1 : fib(n - 1) + fib(n - 2);\n}\n\ndefineWorkerFn(\"add\", add);\ndefineWorkerFn(\"fib\", fib);\n```\n\n`math.js`:\n\n```javascript\nimport { useWorkerFn } from \"worker-fn\";\n\nconst worker = new Worker(new URL(\"./math.worker.js\", import.meta.url), {\n  type: \"module\",\n});\n\nconst add = useWorkerFn(\"add\", worker);\nconst fib = useWorkerFn(\"fib\", worker);\n\nconsole.log(await add(1, 2)); // 3\nconsole.log(await fib(5)); // 5\n```\n\n### Using `defineWorkerFns()` and `useWorkerFns()`\n\n\u003cdetails\u003e\n\n\u003csummary\u003eExample\u003c/summary\u003e\n\n`math.worker.js`:\n\n```javascript\nimport { defineWorkerFns } from \"worker-fn\";\n\nconst fns = {\n  add(a, b) {\n    return a + b;\n  },\n  fib(n) {\n    return n \u003c= 2 ? 1 : fns.fib(n - 1) + fns.fib(n - 2);\n  },\n};\n\ndefineWorkerFns(fns);\n```\n\n`math.js`:\n\n```javascript\nimport { useWorkerFns } from \"worker-fn\";\n\nconst worker = new Worker(new URL(\"./math.worker.js\", import.meta.url), {\n  type: \"module\",\n});\n\nconst { add, fib } = useWorkerFns(worker);\n\nconsole.log(await add(1, 2)); // 3\nconsole.log(await fib(5)); // 5\n```\n\n\u003c/details\u003e\n\n### Using with TypeScript\n\n\u003cdetails\u003e\n\n\u003csummary\u003eExample\u003c/summary\u003e\n\n`math.worker.ts`:\n\n```typescript\nimport { defineWorkerFn } from \"worker-fn\";\n\nfunction add(a: number, b: number) {\n  return a + b;\n}\n\nfunction fib(n: number): number {\n  return n \u003c= 2 ? 1 : fib(n - 1) + fib(n - 2);\n}\n\ndefineWorkerFn(\"add\", add);\ndefineWorkerFn(\"fib\", fib);\n\nexport type Add = typeof add;\nexport type Fib = typeof fib;\n```\n\n`math.ts`:\n\n```typescript\nimport { useWorkerFn } from \"worker-fn\";\nimport type { Add, Fib } from \"./math.worker.ts\";\n\nconst worker = new Worker(new URL(\"./math.worker.ts\", import.meta.url), {\n  type: \"module\",\n});\n\nconst add = useWorkerFn\u003cAdd\u003e(\"add\", worker);\nconst fib = useWorkerFn\u003cFib\u003e(\"fib\", worker);\n\nconsole.log(await add(1, 2)); // 3\nconsole.log(await fib(5)); // 5\n```\n\n\u003c/details\u003e\n\n### Using in Node.js with `node:worker_threads`\n\n\u003cdetails\u003e\n\n\u003csummary\u003eExample\u003c/summary\u003e\n\n`math.worker.js`:\n\n```javascript\nimport { parentPort } from \"node:worker_threads\";\nimport { defineWorkerFn } from \"worker-fn\";\n\nfunction add(a, b) {\n  return a + b;\n}\n\nfunction fib(n) {\n  return n \u003c= 2 ? 1 : fib(n - 1) + fib(n - 2);\n}\n\ndefineWorkerFn(\"add\", add, { port: parentPort });\ndefineWorkerFn(\"fib\", fib, { port: parentPort });\n```\n\n`math.js`:\n\n```javascript\nimport { Worker } from \"node:worker_threads\";\nimport { useWorkerFn } from \"worker-fn\";\n\nconst worker = new Worker(new URL(\"./math.worker.js\", import.meta.url));\n\nconst add = useWorkerFn(\"add\", worker);\nconst fib = useWorkerFn(\"fib\", worker);\n\nconsole.log(await add(1, 2)); // 3\nconsole.log(await fib(5)); // 5\n```\n\n\u003c/details\u003e\n\n## Importing from JSR\n\n`worker-fn` is published on both [npm](https://www.npmjs.com/package/worker-fn)\nand [JSR](https://jsr.io/@mys/worker-fn). If you want to import `worker-fn` from\nJSR, please refer to\n[this document](https://jsr.io/docs/introduction#using-jsr-packages).\n\n## License\n\n[MIT](./LICENSE) License \u0026copy; 2024-PRESENT\n[mys1024](https://github.com/mys1024)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmys1024%2Fworker-fn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmys1024%2Fworker-fn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmys1024%2Fworker-fn/lists"}