JavaScript Async - Advanced Worker Threads |
Written by Ian Elliot | ||||
Monday, 16 July 2018 | ||||
Page 3 of 3
In the Worker thread we can use the ArrayBuffer in the standard way:
And you will see the message:
indicating that the ArrayBuffer is now available in the Worker thread. The Worker can pass the ArrayBuffer back to the UI thread:
After the data has been transferred back to the UI thread you once again will see that the length of the ArrayBuffer is zero. In the UI thread the data can be retrieved in the usual way:
In this case you will see that the event.data is an ArrayBuffer of eight bytes and the original arrayBuf is still zero – that is the original transferred data is not restored. You can, of course, restore the reference to the original data:
Now it looks as if the original data has been handed back by the Worker thread. The big problem with transferring data is that the thread that owned it originally doesn't get the use of it, not even a copy, while the other thread is using it. This doesn't matter as long as the initial owner is generating the data for the first time. For example, if a Worker thread is downloading a resource it can transfer it to the UI thread to be used with no problems. Compare this to a Worker thread that is modifying rather than originating the data. If the UI thread passes the Worker thread a bitmap to process, what does the UI thread show while the worker has ownership? At the time of writing support for ImageBitmap is restricted to Firefox and Chrome and is best avoided. Shared and Service WorkersThere are other types of Worker object that you can use, but these are more specialized and not as well supported. In particular, Safari dropped support to both Shared and Service workers. A SharedWorker is another thread that can be accessed from multiple browsing contexts – windows, iframes or other workers. You create a shared worker using:
Communication is via postMessage as per a standard Worker thread, but now we have to use a port object to make the connection. You have to start the port and then you use its postMessage method:
The connection from the SharedWorker back to the UI thread is performed via the onconnect event:
Once you have the port object you can send messages using it:
Apart from the need to use a port object to send messages, the only other difference is that a single instance of the Worker code serves all of the other threads making use of it. To take advantage of a Shared Worker's abilities you really need to store the port object as different threads connect to it. By keeping a list of ports, the SharedWorker can pass data from one thread to another and hence one window or iFrame to another. Notice that the one origin rule applies to SharedWorkers and all code and windows have to have come from the same source domain. SharedWorkers are interesting, but until Apple decides to support them they are probably not a good gamble. ServiceWorkers on the other hand might be. They provide facilities that make the offline experience of using a web app much more like that of a native app. As such some see the ServiceWorker as the future of the web. At first Apple withdrew support for ServiceWorkers in Safari, but it is currently under consideration. The final chapter is devoted to using ServiceWorkers. Summary
Now Available as a Book:JavaScript AsyncYou can buy it from: Amazon Contents
Also by Ian Elliot To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info <ASIN:1871962560> <ASIN:1871962579> <ASIN:1871962528> <ASIN:1871962501> |
||||
Last Updated ( Monday, 16 July 2018 ) |