{"id":13493040,"url":"https://github.com/pshihn/workly","last_synced_at":"2025-05-15T02:07:54.684Z","repository":{"id":45796417,"uuid":"122542548","full_name":"pshihn/workly","owner":"pshihn","description":"A really simple way to move a function or class to a web worker. 🏋️‍♀️→ 😄","archived":false,"fork":false,"pushed_at":"2022-07-20T02:30:10.000Z","size":63,"stargazers_count":1880,"open_issues_count":8,"forks_count":51,"subscribers_count":23,"default_branch":"master","last_synced_at":"2025-04-14T02:59:14.168Z","etag":null,"topics":["developer-tools","javascript","thread","web-worker","web-workers","webworker"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/pshihn.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}},"created_at":"2018-02-22T22:23:19.000Z","updated_at":"2025-04-03T03:30:44.000Z","dependencies_parsed_at":"2022-07-17T00:46:09.988Z","dependency_job_id":null,"html_url":"https://github.com/pshihn/workly","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pshihn%2Fworkly","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pshihn%2Fworkly/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pshihn%2Fworkly/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pshihn%2Fworkly/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pshihn","download_url":"https://codeload.github.com/pshihn/workly/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254259383,"owners_count":22040820,"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":["developer-tools","javascript","thread","web-worker","web-workers","webworker"],"created_at":"2024-07-31T19:01:11.662Z","updated_at":"2025-05-15T02:07:54.658Z","avatar_url":"https://github.com/pshihn.png","language":"JavaScript","readme":"# Workly    🏋️‍♀️→ 😄 \n\n* A really simple way to move a stand-alone \u003cb\u003efunction/class to a worker thread\u003c/b\u003e.\n* Or, **expose an object or function in a worker** to the main thread.\n* All calls are made asynchronous. Works great with async/await.\n* Only 1kB gzipped.\n\n## Install\n\nDownload the latest from [dist folder](https://github.com/pshihn/workly/tree/master/dist)\n\nor from npm:\n```\nnpm install --save workly\n```\n\n## Usage\n\nMoving a function to a worker is really simple.\n```js\nfunction busyAdd(a, b) {\n  let st = Date.now();\n  while (true) {\n    if ((Date.now() - st) \u003e 2000) break;\n  }\n  return a + b;\n}\n\n(async () =\u003e {\n  let workerAdd = workly.proxy(busyAdd);\n  console.log(await workerAdd(23, 16)); // 39\n  // the busyAdd is executed in a worker so\n  // the UI does not get blocked\n})();\n```\n\nOr, in fact a Class\n\n```js\nclass Adder {\n  constructor() {\n    this.count = 0;\n  }\n  add(a, b) {\n    this.count++;\n    return a + b;\n  }\n}\n\n(async () =\u003e {\n  let WAdder = workly.proxy(Adder);\n  let a = await new WAdder(); // instance created/running in worker\n  console.log(await a.count); // 0\n  console.log(await a.add(23, 16)); // 39\n  console.log(await a.count); // 1\n})();\n```\n\n### Custom workers\nThe above examples only work when the class/function is not dependent on the containing scope, i.e. other libraries or global objects. But, you can create a custom worker.js file and move the code in there. In the worker, you can expose your object/function/class using \u003ci\u003eworkly.expose\u003c/i\u003e method.\n\nIn this example, the function depends on moment.js\n\n\u003cb\u003eworker.js\u003c/b\u003e\n```js\nimportScripts('https://cdn.jsdelivr.net/npm/moment@2.20.1/moment.min.js', '../dist/workly.js');\nfunction friendlyTime(value) {\n  return moment(value).calendar(null, {\n    sameDay: function (now) {\n      if (now - this \u003c 1000 * 60) {\n        return \"[Just now]\";\n      } else if (now - this \u003c 1000 * 60 * 60) {\n        return \"[\" + Math.round((now - this) / (1000 * 60)) + \" mins ago]\";\n      } else {\n        return '[Today at] LT'\n      }\n    }\n  });\n}\nworkly.expose(friendlyTime);\n```\n\u003cb\u003emain.js\u003c/b\u003e\n```js\n(async () =\u003e {\n  let w = workly.proxy(\"./worker.js\");\n  let now = Date.now();\n  console.log(now);\n  console.log(await w(now));\n  console.log(await w(now - (24 * 60 * 60 * 1000)));\n  console.log(await w(now - (4 * 24 * 60 * 60 * 1000)));\n})();\n```\n\n### Caveats\n* If you're not using a custom worker, the function/class being pushed to the worker cannot depend on the containing scope.\n* Since workers do not have access to DOM, DOM manipulation is not supported. \n* Objects passed into functions are not passed by reference, so if the function in the worker updates the passed in object, it will not affect the object in the main scope. \n\n### Examples\nSee the [examples folder](https://github.com/pshihn/workly/tree/master/examples)\n\n### License\n[MIT License](https://github.com/pshihn/workly/blob/master/LICENSE) (c) [Preet Shihn](https://twitter.com/preetster)\n\n### You may also be interested in\n[windtalk](https://github.com/pshihn/windtalk) - Simplest way to communicate between windows or iframes. Work with objects/functions defined in another window or iframe.\n\n","funding_links":[],"categories":["JavaScript","Web Worker"],"sub_categories":["Runner"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpshihn%2Fworkly","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpshihn%2Fworkly","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpshihn%2Fworkly/lists"}