{"id":17713734,"url":"https://github.com/duart38/thread","last_synced_at":"2025-04-30T22:24:13.180Z","repository":{"id":41437532,"uuid":"284460190","full_name":"duart38/Thread","owner":"duart38","description":"type safe multi-threading made easier","archived":false,"fork":false,"pushed_at":"2024-03-02T11:33:48.000Z","size":114,"stargazers_count":47,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-17T04:55:27.198Z","etag":null,"topics":["deno","module","multithreading","thread","threading","typesafe","typescript","webworker","webworkers","worker"],"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/duart38.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"github":["duart38"]}},"created_at":"2020-08-02T12:47:16.000Z","updated_at":"2024-10-07T12:09:27.000Z","dependencies_parsed_at":"2024-10-25T12:46:46.245Z","dependency_job_id":"191bd4fd-c6ea-4d3e-a038-32f12712be57","html_url":"https://github.com/duart38/Thread","commit_stats":{"total_commits":113,"total_committers":5,"mean_commits":22.6,"dds":"0.23893805309734517","last_synced_commit":"eb528b637c9fbeacdfb361bff755bfc5a470ea1e"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duart38%2FThread","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duart38%2FThread/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duart38%2FThread/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duart38%2FThread/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/duart38","download_url":"https://codeload.github.com/duart38/Thread/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251790464,"owners_count":21644222,"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":["deno","module","multithreading","thread","threading","typesafe","typescript","webworker","webworkers","worker"],"created_at":"2024-10-25T10:22:21.440Z","updated_at":"2025-04-30T22:24:13.160Z","avatar_url":"https://github.com/duart38.png","language":"TypeScript","readme":"![alt text](https://github.com/duart38/Thread/blob/master/th_logo.png?raw=true \"Logo Title Text 1\")\n\n\n[![nest badge](https://nest.land/badge.svg)](https://nest.land/package/Thread)\n![alt text](https://img.shields.io/github/license/duart38/Thread?color=blue \"License\")\n![alt text](https://img.shields.io/github/v/release/duart38/Thread?color=red \"Release\")\n![alt text](https://img.shields.io/github/workflow/status/duart38/Thread/Test%20module?label=Tests \"Tests\")\n\n\n\n1. This module allows you to write **Web Worker** code inline with the rest of your code\n2. This module is also somewhat type safe\n3. Allows you to Thread already existing functions\n5. Allows module imports inside the worker\n\n## Examples\n\u003e See examples folder for more examples\n\n```typescript\nlet thread = new Thread\u003cnumber\u003e((e: MessageEvent)=\u003e{\n    console.log('Worker: Message received from main script');\n    const result = e.data[0] * e.data[1];\n    if (isNaN(result)) {\n      return 0;\n    } else {\n      console.log('Worker: Posting message back to main script');\n      return(result);\n    }\n}, \"module\");\n\nthread.onMessage((e)=\u003e{\n    console.log(`back from thread: ${e}`)\n})\nthread.postMessage([10, 12])\n```\nInstead of using the workers postMessage() method we return value from withing our method\n\n**Here's a few more examples**\n```typescript\nfunction someFunction(e: MessageEvent){\n  return 0;\n}\n\nnew Thread((e: MessageEvent)=\u003e{return 0}, \"module\"); // inline Thread with return type of number\nnew Thread(someFunction, \"module\"); // thread an already existing function\nnew Thread(someFunction, \"module\", ['import Something from \"../some.bundle.js\";']); // thread with custom importing\n```\n\n**Async support**\n```TypeScript\nconst thread = new Thread\u003cstring, number\u003e(async (_) =\u003e {\n  console.log(\"Worker: Message received from main script\");\n  // Some async logic...\n  await new Promise((ir) =\u003e setTimeout(ir, 2000));\n  return \"DONE\";\n}, \"module\");\n\nthread.onMessage((e) =\u003e {\n  console.log(`recived back from thread: ${e}`);\n});\n\nthread.postMessage(0);\n```\n\n## API\n\n### Standard API\n| Method / variable                  \t| Description                                                                                                                \t|\n|------------------------------------\t|----------------------------------------------------------------------------------------------------------------------------\n| worker                             \t| The Worker.                                                                                                                \t|\n| stopped                            \t| Tells if the worker has been stopped                                                                                       \t|\n| postMessage(msg)                   \t| Sends data to the Thread                                                                                                   \t|\n| stop()                             \t| calls terminate on the worker.                                                                                             \t|\n| remove()                           \t| Removes the current worker file from the temporary folder. NOTE: Can be used while the program is running (calls stop()..) \t|\n| onMessage(callback: (e: T) =\u003evoid) \t| Bind to the worker to receive messages                                                                                     \t|\n","funding_links":["https://github.com/sponsors/duart38"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fduart38%2Fthread","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fduart38%2Fthread","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fduart38%2Fthread/lists"}