worker-emitter

Play with web worker like event.

Usage no npm install needed!

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

README

Worker Emitter

Play with web worker like event.

Usage

Install

npm install worker-emitter

In worker:

import { register } from "worker-emitter";

const worker = register();

worker.on('event-name', async (data) => {
  const name = await handler(data);
  return name; // <- name is 'WorkerEmitter'
});

In main thread:

import { WorkerEmitter } from "worker-emitter";

const worker = new Worker('path/to/worker.js')

const workerEmitter = new WorkerEmitter(worker);

workerEmitter.emit('event-name', { foo: 'bar' }).then(res => {
  console.log(res); // <- get 'WorkerEmitter'
});

License

MIT licensed