Web Workers Kit
Run your code easily in web workers
Features
Install
NPM -
npm i web-workers
or CDN -
<script src="https://unpkg.com/web-workers@0.6.0/dist.js"></script>
Usage
const lunar = { { const res = await return res }} const lunarMission = lunarMission
API Documentations
WebWorker
The WebWorker
constructor is used invoke/spawn a web worker with the script you pass.
And will return promises for all underlying methods of the script.
const WebWorker = const script = { console { return a + b }} const worker =
Params
- script - script to run in the worker thread
worker.call()
and worker[method]
The call()
or [method]
can be used to call underlying functions/methods which are also promise based
worker // 3 worker // 5
Params
- ...args - arguments to pass to the method
worker.message()
The method is used to add a message to the worker ⚠️ but not post/send it. The same message you can use and append later in the stage and post.
worker worker
To clear the message use message
with no params 😉
Params
- message - message to set - can be of any type - string, object, array
worker.append()
This method is used to append the passed message to the earlier set message
worker worker
Params
- message - message to append - can be of any type - string, object, array but should be matching the already set message
worker.getMessage()
getMessage()
returns the current message with worker
worker worker // 'Hello'
worker.kill()
kill()
will terminate your worker thread
⚠️ The kill method will kill the worker even if there is some execution happening
worker
worker.onerror(fn)
onerror
is an error event handler, i.e. if any error happens at the worker thread, you can listen to it right here
worker
worker.ondata(fn)
Use this method to access the message posted in the worker thread, using postMessage()
worker