Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/component/worker
Nicer web worker API
https://github.com/component/worker
Last synced: 12 days ago
JSON representation
Nicer web worker API
- Host: GitHub
- URL: https://github.com/component/worker
- Owner: component
- Created: 2013-04-19T17:08:52.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-08-21T17:31:06.000Z (over 11 years ago)
- Last Synced: 2024-05-08T17:06:21.723Z (8 months ago)
- Language: JavaScript
- Size: 63.5 KB
- Stars: 20
- Watchers: 8
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- Changelog: History.md
Awesome Lists containing this project
README
# worker
Web worker API wrapper.
## Installation
$ component install component/worker
## API
### Worker(script)
Initialize a worker with the given `script`.
### Worker#send(msg)
Send a message to the worker.
```js
var upper = new Worker('uppercase.js');upper.on('message', function(msg){
console.log(msg.string);
});upper.send({ string: 'hello' });
upper.send({ string: 'world' });
```### Worker#send(msg, callback[, transferables])
Send a request message to the worker with the given `callback`. When
using the request/response paradigm you should pass the `e.data.id` property
back with your response so that the correct callback may be invoked:worker:
```js
onmessage = function(e) {
setTimeout(function(){
postMessage({ id: e.data.id, string: e.data.string.toUpperCase() });
}, 500);
};
```client:
```js
upper.send({ string: 'hello' }, function(msg){
console.log(msg.string);
});
```### Worker#close()
Terminate the worker.
## License
MIT