##The problem:## Webworkers comunicate with the main app via message events. Sometimes this can be annoying if you need certain results from a task done.
##The solution:## WebWorkWrap will help you by adding a callback function to the task, that will fire whenever the task is finished (this is controlled by you in the worker).
##How to use?##
###I. Prepare the worker:###
- Include the script:
-
Place worker code into task init method in worker.js:
Task.prototype.init = function() { // worker body // your code goes here // to access data, use this.data this.complete(this.data); };
###II. Use the worker###
-
Create instnace of the worker in your app:
var myWorker = new WebWorkWrap('workers/worker.js');
-
Set the options (optional):
myWorker.setOptions({ option: 'value' });
-
Do a task:
myWorker.doTask({ key: 'value' }, function(event, data){ console.log(event, data) });
-
Terminate the worker:
myWorker.terminate();