Adding your own scripts to Web Workers

In: web resources

27 Jul 2009

 Powered by Max Banner Ads 

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!

Comment Form

You must be logged in to post a comment.

About this blog

This blog delivers stylish and dynamic news for designers and web-developers on all subjects of design, ranging from: CSS, Ajax, Javascript, web design, graphics, typography, advertising & much more. Our goal is to help you communicate effectively on the web with an engaging website or functional interface.

  • Bemo: Try to put yourself in his shell. How scary do you think it is to be so, soo very tiny in a big big [...]
  • Solar Granulation: You let the forest overwhelm your view of the question. As a development platform - it depends. [...]
  • blibityblabity: Ed O'neil is the only guy I can think to add to the lists already mentioned. O ya, and Rosey Gr [...]
  • francisterence: Because of the troubles with phone-in competitions. They hope to resume it soon. [...]
  • Spicy: http://vectormagic.com/ You have to upload your image to this site but the software does a very g [...]

New offer:


 Powered by Max Banner Ads