worker-component

Web worker API wrapper

worker

Web worker API wrapper.

$ component install component/worker
var Worker = require('worker');
var worker = new Worker('work.js');
 
worker.on('message', function(msge){
  if (!msg.progress) return worker.off('message');
  console.log(msg.progress);
});
 
worker.send({ id: 1 });

Initialize a worker with the given script.

Send a message to the worker.

worker.send({ string: 'hello' });
worker.send({ string: 'world' });

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:

onmessage = function(e) {
  setTimeout(function(){
    postMessage({ id: e.data.id, value: e.data.value.toUpperCase() });
  }, 500);
};

Terminate the worker.

MIT