{"id":21553197,"url":"https://github.com/bbvaengineering/ember-web-workers","last_synced_at":"2025-07-07T07:33:26.551Z","repository":{"id":19509187,"uuid":"87195769","full_name":"BBVAEngineering/ember-web-workers","owner":"BBVAEngineering","description":"Service to communicate your application with browser web workers","archived":false,"fork":false,"pushed_at":"2022-12-10T16:16:35.000Z","size":7845,"stargazers_count":44,"open_issues_count":49,"forks_count":10,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-07T23:08:09.912Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/BBVAEngineering.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-04T14:20:42.000Z","updated_at":"2024-06-15T11:28:59.000Z","dependencies_parsed_at":"2023-01-13T20:25:02.312Z","dependency_job_id":null,"html_url":"https://github.com/BBVAEngineering/ember-web-workers","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/BBVAEngineering%2Fember-web-workers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BBVAEngineering%2Fember-web-workers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BBVAEngineering%2Fember-web-workers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BBVAEngineering%2Fember-web-workers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BBVAEngineering","download_url":"https://codeload.github.com/BBVAEngineering/ember-web-workers/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248182010,"owners_count":21060891,"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":"2024-11-24T07:09:47.577Z","updated_at":"2025-04-10T07:50:29.539Z","avatar_url":"https://github.com/BBVAEngineering.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ember-web-workers\n\n[![Build Status](https://travis-ci.org/BBVAEngineering/ember-web-workers.svg?branch=master)](https://travis-ci.org/BBVAEngineering/ember-web-workers)\n[![GitHub version](https://badge.fury.io/gh/BBVAEngineering%2Fember-web-workers.svg)](https://badge.fury.io/gh/BBVAEngineering%2Fember-web-workers)\n[![NPM version](https://badge.fury.io/js/ember-web-workers.svg)](https://badge.fury.io/js/ember-web-workers)\n[![Dependency Status](https://david-dm.org/BBVAEngineering/ember-web-workers.svg)](https://david-dm.org/BBVAEngineering/ember-web-workers)\n[![codecov](https://codecov.io/gh/BBVAEngineering/ember-web-workers/branch/master/graph/badge.svg)](https://codecov.io/gh/BBVAEngineering/ember-web-workers)\n[![Greenkeeper badge](https://badges.greenkeeper.io/BBVAEngineering/ember-web-workers.svg)](https://greenkeeper.io/)\n[![Ember Observer Score](https://emberobserver.com/badges/ember-web-workers.svg)](https://emberobserver.com/addons/ember-web-workers)\n\n## Information\n\n[![NPM](https://nodei.co/npm/ember-web-workers.png?downloads=true\u0026downloadRank=true)](https://nodei.co/npm/ember-web-workers/)\n\nService to communicate your application with browser web workers.\n\n* Ember.js v3.12 or above\n* Ember CLI v2.13 or above\n* Node.js v10 or above\n\nThis addon can be installed with `ember-cli`:\n\n* `ember install ember-web-workers`\n\n![ember-web-workers](http://i.imgur.com/93lfb8t.gif)\n\n## Usage\n\n* Create the web worker file in `public` (follow this blueprint):\n\n```javascript\n// public/assets/web-workers/test.js\n\n// Wait for the initial message event.\nself.addEventListener('message', function(e) {\n  var data = e.data;\n  var port = e.ports[0];\n\n  // Do your stuff here.\n  if (port) {\n    // Message sended throught a worker created with 'open' method.\n    port.postMessage({ foo: 'foo' });\n  } else {\n    // Message sended throught a worker created with 'send' or 'on' method.\n    postMessage({ bar: 'bar' });\n  }\n}, false);\n\n// Ping the Ember service to say that everything is ok.\npostMessage(true);\n```\n\n* Import the service in your application:\n\n```javascript\n// Some Ember context.\n{\n  worker: Ember.inject.service()\n}\n```\n\n* Use the methods to communicate with the web worker:\n\n#### `postMessage`\n\nMethod used to make a request and wait for a response. This method\nreturns a promise that will be resolved after the worker responses.\n\nWhen promise resolves the worker will be terminated.\n\n**Arguments**:\n\n  * `worker`: the name of the worker to create (used to create the file path `dist/assets/web-workers/${name}.js`).\n  * `data`: transferable object (`true` will be ignored, def. for ping).\n\n```javascript\n// Some Ember context.\n{\n  foo() {\n    return this.get('worker').postMessage('test', { foo: 'bar' }).then((response) =\u003e {\n        // response =\u003e { bar: 'bar' }\n      }, (error) =\u003e {\n        // error contains the message thrown by the worker.\n      });\n  }\n}\n```\n\n#### `terminate`\n\nUsing this method a pending promise can be cancelled, this will terminate the worker\nassociated and the promise will be rejected.\n\nIf promise is not provided, it will kill all the active workers.\n\n**Arguments**:\n\n  * `promise`: the promise returned by the `send` function (optional).\n\n```javascript\n// Some Ember context.\n{\n  foo() {\n    const worker = this.get('worker');\n    const promise = worker.postMessage('test', { foo: 'bar' });\n\n    worker.terminate(promise);\n  }\n}\n```\n\n#### `on`/`off`\n\nMethods used to subscribe to a worker events.\nThe worker will be alive until the event is detached.\n\n**Arguments**:\n\n  * `worker`: the name of the worker to create (used to create the file path `dist/assets/web-workers/${name}.js`).\n  * `callback`: callback to be executed each time worker sends a message (`postMessage`). `callback` is optional for `off` method.\n  \t- If `callback` is not\tpassed for `off` method, then all the instantiated worker with the passed name will be terminated\n  \t- If `callback` is passed, then the corresponding worker associated with the callback will only be terminated.\n\n```javascript\n// Some Ember context.\n\nfunction callback(data) {\n  console.log(data.foo, data.bar); // 'foo bar'\n}\n\n{\n  foo() {\n    const worker = this.get('worker');\n\n    return worker.on('test', callback).then(() =\u003e {\n        // Worker has been created.\n        // Terminate it after 5 seconds.\n        setTimeout(() =\u003e {\n          worker.off('test', callback);\n        }, 5000);\n      }, (error) =\u003e {\n        // Worker error, it has been terminated.\n      });\n  }\n}\n```\n\n#### `open`\n\nThis method creates a new worker and stablish a communication allowing to keep it alive\nto send `1..n` messages until terminates.\n\n**Arguments**:\n\n  * `worker`: the name of the worker to create (used to create the file path `dist/assets/web-workers/${name}.js`).\n\n**Promise argument** (object):\n\n  * `postMessage`: Alias to send a message to the worker.\n  * `terminate`: Close the connection and terminate the worker\n\n```javascript\n// Some Ember context.\n\n{\n  foo() {\n    const worker = this.get('worker');\n\n    return worker.open('test').then((stream) =\u003e {\n        const data1 = stream.send({ foo: 'foo' });\n        const data2 = stream.send({ bar: 'bar' });\n\n        // Wait responses.\n        return Ember.RSVP.all([data1, data2]).then(() =\u003e {\n          // Kill the worker.\n          stream.terminate();\n\n          // Do something with the worker responses.\n          return data1.foo + data2.bar;\n        });\n      }, (error) =\u003e {\n        // Worker error, it has been terminated.\n      });\n  }\n}\n```\n\n#### Handling errors\n\nTo reject the promise an error must be thrown inside the worker:\n\n```javascript\n// Worker context.\n\nthrow new Error('foo');\n\n```\n\n```javascript\n\n// Some Ember context.\n\n{\n  foo() {\n    return this.get('worker').postMessage('test').catch((error) =\u003e {\n        console.error(error); // Unhandled error: foo\n      });\n  }\n}\n```\n\n## Contribute\n\nIf you want to contribute to this addon, please read the [CONTRIBUTING.md](CONTRIBUTING.md).\n\n## Versioning\n\nWe use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/BBVAEngineering/ember-wait-for-render/tags).\n\n\n## Authors\n\nSee the list of [contributors](https://github.com/BBVAEngineering/ember-wait-for-render/graphs/contributors) who participated in this project.\n\nContributing\n------------------------------------------------------------------------------\n\nSee the [Contributing](CONTRIBUTING.md) guide for details.\n\n\nLicense\n------------------------------------------------------------------------------\n\nThis project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbbvaengineering%2Fember-web-workers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbbvaengineering%2Fember-web-workers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbbvaengineering%2Fember-web-workers/lists"}