Simple Web Worker
An utility to simplify the use of web workers
See full README, with examples, here.
Why
Create and use Web Workers can be cumbersome sometimes. This plugin aims to facilitate the use of Web Workers.
How to install and use
yarn add simple-web-worker
// or
npm install simple-web-worker --save
Then:
import SWorker from "simple-web-worker";
Obviously, you don’t have to call it SWorker
. You are free to use the name you want!
API
SWorker.run(func, [args]?)
Where:
- func is the function to be runned in worker
- [args] is an optional array of arguments that will be used by func
This method creates a disposable web worker, runs and returns the result of given function and closes the worker.
This method works like Promise.resolve()
, but in another thread.
SWorker.create([actions]?)
Where:
- [actions] is an optional array of objects with two fields,
message
andfunc
. Essentially, it is a messages-actions map.
If [actions] is omitted or
undefined
, the created <worker> will have no registered actions, so you’ll have to use the methodregister
before you can use the <worker>.
If you plan to reuse a <worker>, you should use this method. It creates a reusable <worker> (not a real Web Worker, more on this ahead) with determined actions to be runned through its postMessage()
or postAll()
methods.
<worker>.postMessage(message, [args]?)
Where:
- <worker> is a worker created with
SWorker.create([actions])
- message is one of the messages in [actions]
- [args] is an optional array of arguments that will be used by the function registered with message
When the function does not expect any arguments or the expected arguments have default values, [args] can be omitted safely.
When the expected arguments do not have default values, [args] should be provided.
This method works like Promise.resolve()
, but in another thread.
<worker>.postAll([message1,… || {message: message1, args: [args1]},… || [args1],…]?)
Where:
- <worker> is a worker created with
SWorker.create([actions])
- The argument is an optional array which accepts one of the following:
- message1,… - strings containing one or more of the messages in [actions]
- {message: message1, args: [args1]},… - objects containing two fields,
message
(a message from actions) andargs
(the arguments to be used by the correspondent function)- [args1],… - arrays of arguments to be used by the registered actions.
If [message1,…] is
undefined
or no argument is given, <worker> will run all registered actions without arguments.
If [{message: message1, args: [args1]},…] or [[args1],…] is used, you should use []
(an empty array) as [args] for the functions that does not expect arguments, or if the respective argument of your function has a default value and you want it to be used. If you use [null]
this will be the value assumed by function argument.
When using [[args1],…], you MUST input the same number of arguments as registered actions, even if some action doesn’t accept any arguments! In that case use a []
, as stated above. See examples below.
If [{message: message1, args: [args1]},…] is used, every object must contain the fields message
and args
.
This method works like Promise.all(), but in another thread.
<worker>.register(action || [actions])
Where:
- <worker> is a worker created with
SWorker.create([actions])
- action is an object with two fields,
message
andfunc
- [actions] is an array of objects, and each object is an action, as defined above
You can use action or [actions], but not both at the same time.
<worker>.unregister(message || [messages])
Where:
- <worker> is a worker created with
SWorker.create([actions])
- message is one of the messages in [actions]
- [messages] is an array containing one or more messages, and each message is a message, as defined above
You can use message or [messages], but not both at the same time.
Closing workers?
You may be thinking: “How do I terminate those reusable workers if there’s no close()
or terminate()
methods?”
Well, when you create a reusable worker, you don’t receive a real Web Worker.
Instead, you get an object which holds the given messages-actions map, and when you call postMessage()
or postAll()
it will, under the hood, call run()
with the correspondent functions.
So, to “terminate” a “worker” when it is not needed anymore, you can just do:
worker = null; // after create and use the worker