🏗️ Run a module in a Web Worker.
Latest commit 683631f May 29, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Pass worker as context, `this` not set inside arrow fn May 11, 2018
.editorconfig Initial commit! Jan 9, 2018
.gitignore Initial commit! Jan 9, 2018
.travis.yml chore: add default Travis config May 17, 2018
README.md 800 bytes Jan 19, 2018
demo.html Add demo page. Jan 19, 2018
loader.js add convenience/help proxy for workerize-loader Jan 17, 2018
package.json 0.1.7 Feb 1, 2018

README.md

💖 Using Webpack? You want workerize-loader ➡️

workerize

Workerize npm travis

Moves a module into a Web Worker, automatically reflecting exported functions as asynchronous proxies.

  • Bundles a tiny, purpose-built RPC implementation into your app
  • If exported module methods are already async, signature is unchanged
  • Supports synchronous and asynchronous worker functions
  • Works beautifully with async/await
  • Just 800 bytes of gzipped ES3

Install

npm install --save workerize

Usage

Pass either a function or a string containing code.

worker.js:

let worker = workerize(`
	export function add(a, b) {
		// block for half a second to demonstrate asynchronicity
		let start = Date.now();
		while (Date.now()-start < 500);
		return a + b;
	}
`);

(async () => {
	console.log('3 + 9 = ', await worker.add(3, 9));
	console.log('1 + 2 = ', await worker.add(1, 2));
})();

License

MIT License © Jason Miller