ng-webworker
demo and more instructions: http://mattslocum.github.io/ng-webworker/
Installation for Testing
npm install
Run Tests
grunt karma
Build
grunt uglify
Using ng-webworker
Basic Usage
Include the module
angular ;
Create a basic worker
// function that will become a worker { // the return value becomes the resolve of the promise return num * 2;} var myWorker = Webworker;
call the worker function
myWorker;
Create an advanced worker
Async (notification) promises
Lets say you want the notification support for webworkers for things like progress bars. There are times you do not want the return value to resolve the function. Maybe you are doing api requests or some other async tasks. An api of two functions is injected into the web worker. If an error is thrown, it will reject.
- complete - This will resolve the promise
- notify - Send a notification of data via the promise
// function that will become a worker { // api to send a promise notification ; // api to resolve the promise. Note: according to the $q spec, // a promise cannot be used once it has been resolved or rejected. ;} // mark this worker as one that supports async notificationsvar myWorker = Webworker; // uses the native $q style notification: https://docs.angularjs.org/api/ng/service/$qmyWorker
Extra config
Global config
angular;
Instance Config
If you want callback style functions on top of the promise or as an alternative style, you can pass callbacks into the config block. These callbacks only work if async is true. When async is false it uses basic resolves when the function returns.
var myWorker = Webworker;
IE workarounds
IE strikes again. The way ng-webworker can take a function and turn it into a webworker is by transforming your function into a Blob and executing that blob in a web worker like you would an independant file. Unfortunatly, IE treats blobs as cross domain. The solution is to have a worker shell file that is loaded as a separate file. Your function is strigified and then messaged over to the worker file and evaled to make it behave just like the blobs did.