Miss any of our Open RFC calls?Watch the recordings here! »

single-file-worker

1.0.0 • Public • Published

single-file-worker.js

npm npm license npm download total npm download by month Bower Bower

Dependency Status devDependency Status Travis Build Status AppVeyor Build Status Code Climate

This helps to make library that uses WebWorker/fork as one simple file.

Install

npm:

npm install single-file-worker

bower:

bower install single-file-worker

Usage

fork (node.js / electron / nw.js):

var SingleFileWorker = require('single-file-worker');
 
function workerRoutine() {
  process.on('message', function(message) {process.send(message + 1)});
}
 
function clientRoutine(workerMaker) {
  var worker = workerMaker();
  worker.on('message', function(message) {
    console.log(message); // 2
  });
  worker.send(1);
}
 
SingleFileWorker.fork(workerRoutine, clientRoutine, SingleFileWorker.scriptFilenameFromError(new Error()));

WebWorker (browsers / electron / nw.js):

<script src="single-file-worker.js"></script>
<script>
var workerCode = "self.addEventListener('message', function(event) {self.postMessage(event.data + 1)});";
 
var runWorker;
 
function clientRoutine(workerMaker) {
  var worker = workerMaker();
  worker.addEventListener('message', function(event) {
    console.log(event.data); // 2
  });
  worker.postMessage(1);
}
 
SingleFileWorker.webworker(workerCode, clientRoutine);
</script> 

License

This is released under MIT License.

Keywords

none

Install

npm i single-file-worker

DownloadsWeekly Downloads

1

Version

1.0.0

License

MIT

Last publish

Collaborators

  • avatar