Activate your free membership today | Log-in

Monday, July 27th, 2009

Adding your own scripts to Web Workers

Category: JavaScript

Over in Geneva, they have posted on coupling PHP and Workers.

They build on the great intro from John and tweak it to use simple PHP on the backend to do things such as making sure that your favourite library has been loaded into each Worker, so you can use it in your script.

JAVASCRIPT:
  1.  
  2. //  Recieve data FROM the client with postMessage()
  3. onmessage = function(event){
  4.   if ( event.data === 'load' ) {
  5.     postMessage( '-----BEGIN TRANSMISSION-----');
  6.     postMessage( {'server_objects': this} );
  7.  
  8.     $.ajax.get({
  9.       url: 'xhr_test_content.txt',
  10.       callback: function(response) {
  11.         var text = response.text;
  12.  
  13.         postMessage( 'AJAX GET RETURNED: ' + text );
  14.       }
  15.     });   
  16.     postMessage( '-----END TRANSMISSION-------');
  17.   }
  18. };
  19.  

The Worker implementations are doing well these days. For awhile Safari didn't have importScripts but that changed before Safari 4. Chrome had some weird side effects too recently, and Malte Ubl posted on some strangeness that we ran into with our Bespin Worker Facade.

We tested for:

JAVASCRIPT:
  1.  
  2. if (typeof Worker == "undefined") { 
  3.  

But that didn't work. Malte explains:

For some reason the statement above is actually true in Chrome 2, even though as stated above support for the Worker API has not been implemented.

I then tried to instantiate the Worker object. All this does is to throw an exception with the message "Worker is not enabled". This looks like an unfinished implementation that was only partially removed or something in that direction.

This code handles the special case:

JAVASCRIPT:
  1.  
  2. var noWorker = typeof Worker == "undefined" ? true : false
  3.  if(!noWorker) { 
  4.   try { new Worker("test.js") } 
  5.   catch(e) { 
  6.       e = e + "" 
  7.       if(e.indexOf("Worker is not enabled") != -1) { 
  8.           noWorker = true
  9.       } 
  10.   } 
  11. } 
  12.  

The bug is filed. The good news is that workers are looking pretty good these days!

Posted by Dion Almaer at 6:03 am
3 Comments

+++--
3.1 rating from 22 votes

3 Comments »

Comments feed TrackBack URI

It’s usually better to avoid double negatives. Also, true/false in that ternary is really redundant : )

var hasWorker = typeof Worker != “undefined”;
if (!hasWorker) { … }

Comment by kangax — July 27, 2009

I implemented web workers in Passpack 7 to speed up some jobs when Google Gears is not installed.
I simply check it with:

if (window.Worker) …

It works well with all browsers.

Comment by Sullof — July 27, 2009

January 1999 Motorola 328cAlthough plus size mother of the bride dressesthe first flip phone in should be sooner, but this clamshell phone is just an integrated microphonemother of bride dresses is not in the usual sense we are now referred to flip.Column Strapless Satin Tulle Wedding Dress Most of the time we are actually referring to folding clamshell handsets, but Motorola’s first cheap prom dressesclamshell phone is the famous “palm-sized” – 328c. The phone is the beginning of in the domestic market and ghetto prom dressescan be received in English and Chinese text, but it does not support sending text messages in Chinese, which is sorry. Column Satin Organdie Lace Wedding DressHowever, clear call quality and handsome appearance are popular at the time this phone place.wedding dress

Comment by wuwei — November 10, 2009

Leave a comment

You must be logged in to post a comment.