JavaScript and Maps (in that order)

Making Web Workers with Communistjs

JavaScript workers are weird, unintuitive, and have inconsistent (but broad) cross browser support.  Good thing I wrote a library which smooths over most of those issues. 

A useful thing you can do with a worker is to download a file and parse it as JSON especially if you have a large JSON file doing this in a worker is helpful as it prevents that from slowing down the things your doing up front in the DOM thread, a typical demo of this will do something like:

Communistjs exposes a function called cw and to do the exact same thing you’d write 

well that one doesn’t do exactly the same thing, it gives the same results but faster, because the first version waits for the worker to be created and then sends a message then closes the worker. While with communist, when it creates the worker since it knows it’s only going to be called once it sticks the url right into the new worker so that it immediately executes when the worker is created, the version that is exactly the same is here. Also in practise you’d just want to return the promise, which is what I’m going to do with the rest of the examples giving a final single use worker of:

Now that I’ve shown you how to make a single use worker NEVER EVER DO THIS.

It takes much more time to create a worker then it takes to do any of the other parts of this so as soon as there is a chance that you will need to use something more then once your going to want to reuse the worker.  There is a bias in demo, tests, and perfs towards using workers for things you only do once and I am a victim of this myself as early on this library was geared towards that but as soon as I started making apps with workers I realized that’s not what I wanted. I wanted a worker that I could create once and call many times and something like this was what I made first try see if you can find why this is bad:

The issue is that all of the data will go to the most reacent callback what you need to do is something like this

So now we are saving the callback and sending a token to the worker which tells us which response it is. This is getting fairly complex now and there is a bit of boiler plate, the way you’d do this with communist is:

We don’t even need a constructor. This is where communistjs comes in handy because even this is simpler then you will want to be doing in your app.  In your app your probably going to not only download and parse JSON but also do something else which takes a long time like … I don’t know find the hypotenuse in a right triangle…for an array of number pairs…only some of which are pairs of numbers, kids these days are into that right?.

I will leave it as an exercise for the reader to figure out how to do this in a regular worker (read I made this library so I wouldn’t have to) but the following script uses communist to create an object that has 2 methods
  • ajax: which is the previous ajax thing
  • findC: which calculates the triangle thing.

That is all it takes. use as follows:

it’s that simple, and we haven’t even touched the setup for

  • promises
  • promises that aren’t slow by using a setImmediate shim instead of setTimeout
  • using transferable objects without IE exploding
  • queues

I will actually be writing another blog post that focuses on queues and will probably mention transferable objects.  But the bottom line I’m going for here is that workers don’t have to be hard especially with communistjs, because the only thing it cares about, are workers.