* jQuery.Hive Plugin

* http://github.com/rwldrn

* http://pollenjs.com

* Copyright 2010, Rick Waldron

* Dual licensed under the MIT or GPL Version 2 licenses.

* v0.1.95

* (see source for other credits)

* jQuery.Hive -> The Hive is a property of jQuery

* jQuery.Hive.options -> Setup properties for jQuery.Hive.create( [options] )

* jQuery.Hive.options.count -> Set property from jQuery.Hive.create( { count: [int] } ), number of threads to create

* jQuery.Hive.options.worker -> Set string property from jQuery.Hive.create( { worker: [file-name] } ), name of worker file

* jQuery.Hive.options.receive -> Set callback from jQuery.Hive.create( { receive: [callback] } ), callback to execute when worker receives message (can be global or worker specific)

* jQuery.Hive.options.created -> Set callback from jQuery.Hive.create( { created: [callback] } ), callback to execute when workers have been created

* jQuery.Hive.options.special -> NOT IMPLEMENTED/INCOMPLETE - Set callback as a second argument to $().send()

* jQuery.Hive.options.error() -> NOT IMPLEMENTED/INCOMPLETE - Error handler

* jQuery.Hive.create( options ) -> Array, create workers, returns array of all workers

* jQuery.Hive.destroy( [id] ) -> destroy all or specified worker by id

* jQuery.Hive.get( id ).send( message, callback ) -> Send [message] to worker thread, set optional receive callback
* --> SIMPLER ALTERNATIVE: $.Hive.get(id).send( [message], function () {} )
* --> Allows for passing a jQuery.Hive.get(id) object to $() ie. $( $.Hive.get(id) ).send( [message] )

* jQuery.Hive.get( [id] ) -> Return all or specified worker by [id], [id] argument is optional
* --> $.Hive.get() returns all worker objects in the $.Hive
* --> $.Hive.get(1) returns the worker object whose ID is 1

* jQuery.( $.Hive.get( id ) ).send( message ) -> Send [message] to worker thread
* --> SIMPLER ALTERNATIVE: $.Hive.get(id).send( [message] )
* --> Allows for passing a $.Hive.get(id) object to $() ie. $( $.Hive.get(id) ).send( [message] )
Basic Hive creation:

(assumes jquery-1.4.2.js and jquery.hive.js are loaded)


$(function () {

  $.Hive.create({

    count: 100,  
    worker: 'worker.js',
    receive: function (data) {
      
      console.group('RECEIVED MESSAGE - WORKER: #' + data._from_);
        console.log( data );  
      console.groupEnd();   
      
      /*
      ------------------------------------------------------
        Or... 
        
        Populate a massive data table...
        
        Update a browser based IM client
        
        Update a feed reader app ( 1-to-1 worker to feed?)
      ------------------------------------------------------        
      */
      
    },
    created: function (hive) {
      console.log( 'Created ' + hive.length + ' workers' );
      
      /*
      ------------------------------------------------------
        Or...
        
        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');
  
  });  
  
  
});


jQuery.Hive Logo © Nick Piava 2010