Web Workers are relatively easy to use, especially now that shared objects are becoming more standard. A while back, I did a little experiment to see if I could make them even easier. The full code is at JS Bin.
The scenario is that you have many different tasks, each with their own events and scripts. They need to run in a browser, and asynchronously, so web workers would be a great choice! But wiring up all the events and decoding the payload from the events requires boilerplate (check that the domains match, then do error handling, etc), especially if there is two-way communication.
There are two components to the experiment, the primary script, and the worker script. This could be expanded to two-way communication.
DISCLAIMER: There isn’t any security implicitly built in here. Ideally you’d put a property that is shared and only known by trusted scripts to know that events are legit.
Usage (The Prestige!)
We’re going to be making a web worker (our “actor”) that computes fibonacci sequences (yes, I know that this is… sigh), and when it has computed the next number in sequence, will emit a
stepped event. When it finishes, it will emit a
stopped event. This will be controlled by the primary script, which will listen for these events.
And the content of the worker:
Notice how the events aren’t the typical string-based calls, like used in the DOM and EventEmitter? These are a form of Signals, which means we can define exactly what events something will emit, and know that we’re listening to the proper events immediately (if not, an error will be thrown complaining that the property is
Now let’s get to that point.
The Primary Script
First some bootstrapping code:
For each entry in the given object, create a constructor function. That constructor will create a new Web Worker using the given source, creates the proper signals (when received from the worker: more on that later), attaches an event listener that automatically dispatches the proper signal, and divorces the data from the worker message. This means that while you will still need to know what the worker is dispatching, you don’t need to care that it was from a worker. In addition, when the worker is ready for signal bindings, the
cb constructor argument will be called.
The Worker Script(s)
This method looks for a
signals property on the
target object, and then creates the actual signal objects. It also, upon finishing creating the signals, sends a message to the parent process describing what signals it accepts! This means that you only need to configure signals in one place (here), and yet still refer to them by name from the primary script!
This is just an experiment, but I think it’s pretty neat that instead of using strings, any events that a worker could fire are defined in one place (
fibber.signals), and if a typo is made when attempting to listen for a signal, the error is known much sooner than a silently-failing string-based event.
Here is the very basic code I used to implement a Signal in JS. For a more complete implementation, I recommend js-signals, or my own implementation k-signals, which is much smaller, but has most of the same features (it was done mostly as a learning experience).
Signals are a really interesting paradigm to me, so expect a post soon with more details of my experiments!