workerize

by developit

developit /workerize

πŸ—οΈ Run a module in a Web Worker.

3.9K Stars 99 Forks Last release: over 2 years ago (0.1.7) 60 Commits 8 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

πŸ’– 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

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.