rwaldron / jquery-hive

jQuery Plugin for creating and managing web workers across implementations. Includes Hive.Pollen.js - the thread-safe utility library for connecting worker threads to the Hive.
http://weblog.bocoup.com/javascript-web-workers-from-basics-to-jquery-hive-part-i
203 stars 24 forks source link

Enter The jQuery.Hive

jQuery.Hive.js API

Feed Workers With Pollen

jQuery.Hive.Pollen.js API

Basic Client Setup

Assumes >= jQuery 1.4 and jQuery.Hive.js are loaded

$(function () {

  $.Hive.create({

    worker: 'worker.js',
    receive: function (data) {

      console.group('RECEIVED MESSAGE - WORKER: #' + data._from_);
        console.log( data );  
      console.groupEnd();   

      /*
      ------------------------------------------------------
        Possible uses:

        Populate a massive data table...

        Update a browser based IM client

        Update a feed reader app ( 1-to-1 worker to feed?)

        Handle audio data from typed array

        Handle pixel array
      ------------------------------------------------------        
      */

    },
    created: function (hive) {

      /*
      ------------------------------------------------------
        Possible uses:

        Impress the hell out of your friends by 
        executing code after all the workers are created
      ------------------------------------------------------  
      */        
    }
  });

  /*
  ------------------------------------------------------
    This contradicts what I noted above, but  it's for 
    illustration purposes, so I'm ok with that.
  ------------------------------------------------------  
  */        

  $( $.Hive.get(1) ).send({ 

    "message" : { 
      "a" : "a-value",
      "b" : "b-value",
      "c" : "c-value"
    }      

  });

  /*
  ------------------------------------------------------
    Alternative syntax
  ------------------------------------------------------      
  */
  $.Hive.get(1).send({ 

    "message" : { 
      "a" : "a-value",
      "b" : "b-value",
      "c" : "c-value"
    }      

  });  

  /*
  ------------------------------------------------------
    Specify an additional callback
  ------------------------------------------------------      
  */
  $.Hive.get(1).send({ 

    "message" : { 
      "a" : "a-value",
      "b" : "b-value",
      "c" : "c-value"
    }      

  }, function (data) {

    console.log('This is from a task specific message receipt callback');

  });  

});

Basic Worker Example

importScripts('jquery.hive.pollen.js');

$(function (data) {

  // `this` equals WorkerGlobalScope

  $.ajax.get({  
    url: 'get-data-from-the-server.php',  
    dataType:'json', 
    data: $.param(data.message), 
    success: function(jsonObj) { 

      //  Assume its a list of companies with some contact data.

      $.send( 
        $.unique( 
          $.filter(jsonObj, function (obj) { 

            //  If not passed in the data property above, we could filter here.
            //  Not the most efficient way, the example is really to illustrate Pollen's syntax

            if ( $.inStr(obj.company, data.company) ) { 
              return true; 
            } 
            return false;

          })
        )
      );

      // OR...

      $.send( 
        $.query(

          //  Get filtered data with a JSONPath query
          "?company='"+data.company+"'", 
          jsonObj

        )
      );
    } 
  });      

});