{"id":18221676,"url":"https://github.com/haochi/angular2-web-worker","last_synced_at":"2025-04-03T02:30:43.948Z","repository":{"id":57179784,"uuid":"49411296","full_name":"haochi/angular2-web-worker","owner":"haochi","description":"Web worker for Angular 2","archived":false,"fork":false,"pushed_at":"2017-12-23T00:44:47.000Z","size":55,"stargazers_count":50,"open_issues_count":6,"forks_count":10,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-18T04:26:08.769Z","etag":null,"topics":["angular2","typescript","web-worker"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/angular2-web-worker","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/haochi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-11T08:06:38.000Z","updated_at":"2025-03-04T08:03:11.000Z","dependencies_parsed_at":"2022-09-09T19:00:15.345Z","dependency_job_id":null,"html_url":"https://github.com/haochi/angular2-web-worker","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haochi%2Fangular2-web-worker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haochi%2Fangular2-web-worker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haochi%2Fangular2-web-worker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haochi%2Fangular2-web-worker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/haochi","download_url":"https://codeload.github.com/haochi/angular2-web-worker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246925187,"owners_count":20855850,"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":["angular2","typescript","web-worker"],"created_at":"2024-11-03T22:03:50.498Z","updated_at":"2025-04-03T02:30:43.635Z","avatar_url":"https://github.com/haochi.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# What is this?\n\n[Web worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers) service for [Angular 2](https://angular.io).\n\n# Install\n\n```shell\nnpm i angular2-web-worker\n```\n\n# API\n```javascript\nexport interface IWebWorkerService {\n    run\u003cT\u003e(workerFunction: (any) =\u003e T, data?: any): Promise\u003cT\u003e;\n    runUrl(url: string, data?: any): Promise\u003cany\u003e;\n    terminate\u003cT\u003e(promise: Promise\u003cT\u003e): Promise\u003cT\u003e;\n}\n```\n\n* `run`\n    * `workerFunction`: \n        * Must be a self-contained function. Cannot reference outside variables.\n        * You can import other libraries with [`importScripts`](https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts) though\n        *  These are okay:\n        *  \n            ```javascript   \n            run(input =\u003e input * input, 10);\n            \n            run(input =\u003e {\n                const square = num =\u003e num * num;\n                return square(input);\n            }, 10);\n            \n            const someFunction = (input) =\u003e input * input);\n            run(someFunction, 10);\n            \n            class Runner {\n                private webWorkerService = new WebWorkerService();\n                constructor() {\n                    this.webWorkerService.run(this.someFunction, 10);\n                }\n                someFunction() {\n                    return input * input;\n                }\n            }\n            ```\n        *  These will probably **NOT** work:\n        *  \n            ```javascript   \n            // this is not okay because inside the context of the web worker `this` is not the same `this` as here.\n            run(input =\u003e this.square(input), 10); \n            \n            // this is not okay because `_` doesn't exist in the web worker context (assuming tht `_` is available here to begin with)\n            run(input =\u003e {\n                return _.uniqueId() * input;\n            }, 10);\n            ```\n    * `data`: [serializable data](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)\n* `runUrl`: Basically the same as \n    * `url`:  The url you would use to create a [`Worker`](https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker) instance\n    * `data`: Same as the `run` method\n* `terminate`: Calling this will [`terminate`](https://developer.mozilla.org/en-US/docs/Web/API/Worker/terminate) the web worker, if it is still running.\n    * `promise`: The `Promise` instance returned by `run` or `runUrl`.\n\n# Example\n\nCheck out [angular2-web-worker-example](https://github.com/haochi/angular2-web-worker-example) for a sample project,\nor see [`app/app.component.ts`](app/app.component.ts) for usage with an Angular 2 application.\n\n```\nexport class AppComponent implements OnInit {\n    constructor(private _webWorkerService: WebWorkerService) {\n    }\n    \n    ngOnInit() {\n        const input = 100;\n        const promise = this._webWorkerService.run(this.someCPUHeavyFunction, input);\n        promise.then(result =\u003e console.log(result));\n    }\n    \n    someCPUHeavyFunction (input) {\n        return input * 10;\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhaochi%2Fangular2-web-worker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhaochi%2Fangular2-web-worker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhaochi%2Fangular2-web-worker/lists"}