web-worker-mux

Allows many clients to send messages to a single web worker and only get their own responses.

Usage no npm install needed!

<script type="module">
  import webWorkerMux from 'https://cdn.skypack.dev/web-worker-mux';
</script>

README

Web Worker Mux Build Status

Allows many clients to send messages to a single web worker and only get their own responses.

Usage

Outside of worker (assuming the use of webpack/worker-loader):

import Worker from 'worker-loader!./worker';
import create from 'web-worker-mux';

const worker = create(Worker, {
  // optional
  onError(error) {
    console.error('[web-worker] error', error);
  }
});

export default function multiply() {
  return Promise.all([
    worker.post({ a: 2, b: 2 }),
    worker.post({ a: 3, b: 6 })
  ]);
  /*
  .then(([product1, product2]) => {
    console.log(product1, product2); // 4, 18
  })
  */
}

Inside the ./worker code:

import onMessage from 'web-worker-mux/onMessage';

onMessage(({ a, b }) => {
  return Promise.resolve(a * b);
});

Assumptions

Your client code must bring its own Promise polyfill, if needed (aka BYOP?).

License

Apache-2.0