event-worker
Minimalistic event/promified driven web worker abstraction.
Install
npm
npm install event-worker --save
CDN
https://cdn.jsdelivr.net/npm/event-worker@1.3.0/index.min.js
Usage
Basic example
In your main thread (main.js):
const EventWorker = const worker = 'path/to/my/worker.js' { const user = await worker /* { id: '30242', name: 'neil', lastname: 'tyson degrasse' } *///...
And then in your web worker (worker.js) you can listen for that event and respond back with the requested data:
const EventWorker = const worker = worker { let user = await ifuser return user user = await return user}
Workload splitting
If you want to keep your main thread running smoothly dividing the work load of expensive computational task between multiple web workers becomes easier.
From main thread (main.js):
const EventWorker = const workerPath = 'path/to/my/worker.js' const workerPool = workerPath workerPath workerPath const sum = a + b const multiplyBy2InOtherThread = worker async await Promiseall workerPool // 6
From worker (worker.js):
const worker = worker
Bidirectional communication
You can listen for events triggered by your workers.
From main thread (main.js):
//... worker //..
From worker (worker.js):
//...const res = await worker res // => 'Good job worker!!'
Inlining code
Instead of having a separate file for your worker, you can wrap your code inside a function and pass it as an argument to the constructor of EventWorker. This is a good option when prototyping.
From main (main.js):
const worker = async { let res = await mainThread console // Hello worker! } worker
Caveat
When you inline functions it is easy to get confused by the execution context. If you try to access a variable that is outside the scope of the inline function it will fail.
const favoriteAnimal = 'chiguire' const worker = async { // This will get executed in a worker. mainThread }
Error Handling
Error handling works the same as you would expect from a promise executed in the same thread:
From main thread (main.js):
const EventWorker = const worker = 'path/to/my/worker.js' worker
From worker (worker.js):
const worker = //throwing errorsworker // throwing async errorsworker
Instead of embedding event-worker into your worker file with a module bundler, you can use the built in function importScripts
:
const worker = // ...
EventWorker reference is injected into the global scope once it's loaded.
API
EventWorker
new EventWorker(source) Creates a new instance
-
source
string | function | undefined
-
If a string is passed: It will assume it is the worker source file path.
-
If a function is passed it will get converted into a string an then transformed into a worker.
-
If nothing (undefined) is passed it will assume that the environment is the worker.
-
Promise
emit(eventName, data) Emits a event.
-
eventName
String
-
data
Any
EventWorker
on(eventName, callback) Registers for an event.
-
eventName
-
callback
function(object) => Promise<any>
Gets executed when eventName is emited.
- object
-
object.payload
any
Data sent from the event emitter to the listener.
-
- object
void
terminate() Immediately terminates the Worker. This does not offer the worker an opportunity to finish its operations; it is simply stopped at once.
Changelog
2017-11-04
- Removed
"resolve"
and"reject"
function properties in the"on"
callback.
Contributing
All contributions are welcome.
License
MIT © Alvaro Bernal