@@ -76,8 +76,12 @@ function GWorker(worker)
7676 func = this ,
7777 num_workers ;
7878
79+ // Store ready state of worker library
7980 this . ready = true ;
8081
82+ // Queue to handle requests made while jQuery is loading
83+ this . queue = [ ] ;
84+
8185 // Setup GWORKER namespace
8286 if ( typeof GWORKER === 'undefined' ) {
8387 GWORKER = { } ;
@@ -91,40 +95,62 @@ function GWorker(worker)
9195 this . worker_id = num_workers ;
9296 this . worker_url = worker ;
9397
98+ // Load jQuery if required
9499 if ( typeof $ !== 'object' ) {
95100 // Load jQuery
96101 this . loadJQuery ( function ( ) {
97102 func . loadWorker ( ) ;
98103 } ) ;
99104 }
100105
101-
102106 // Declare onmessage
103107 this . onmessage = { } ;
104108
105- // Setup postmessage function
109+ // Setup postMessage function(s) for workers. These are the callback functions
110+ // that the workers will trigger when they have completed their job
106111 GWORKER . workers [ this . worker_id ] . postMessage = function ( message ) { func . returnMessage ( message ) ; } ;
107112 GWORKER . workers [ this . worker_id ] . postmessage = function ( message ) { func . returnMessage ( message ) ; } ;
108113
109114}
110115
111116
117+ /*
118+ * Prototyped functions
119+ */
112120GWorker . prototype = {
113121
122+ /*
123+ * postMessage function to send request to worker
124+ *
125+ * message - Date to send to worker, can be of any format
126+ */
114127 postMessage : function ( message ) {
115- if ( ! this . ready ) {
116- return ;
117- }
128+
129+ if ( ! this . ready ) {
130+ this . queue . push ( message ) ;
131+ return ;
132+ }
118133
119134 self = GWORKER . workers [ this . worker_id ] ;
120135 GWORKER . workers [ this . worker_id ] . onmessage ( { 'data' : message } ) ;
121136 self = GWORKER . self ;
122137 } ,
123138
139+ /*
140+ * returnMessage function to receive and process data
141+ * returned by the worker
142+ *
143+ * message - Data returned by worker
144+ */
124145 returnMessage : function ( message ) {
125146 this . onmessage ( { 'data' : message } ) ;
126147 } ,
127148
149+ /*
150+ * Load the jQuery library on demand
151+ *
152+ * callback - Callback to be triggered on completion
153+ */
128154 loadJQuery : function ( callback ) {
129155
130156 this . ready = false ;
@@ -141,20 +167,42 @@ GWorker.prototype = {
141167 jquery_lib . onload = callback ;
142168 } ,
143169
170+ /*
171+ * Load a worker
172+ */
144173 loadWorker : function ( ) {
145174 this . ready = true ;
146175
147176 var func = this ;
148177
149178 // Load the worker file using the jQuery ajax library
179+ // TODO : This should be asynchronous for performance reasons
150180 self = GWORKER . workers [ this . worker_id ] ;
151181 $ . ajax ( {
152182 url : func . worker_url ,
153183 dataType : 'script' ,
154184 async : false
155185 } ) ;
156186 self = GWORKER . self ;
157- }
187+
188+ this . processQueue ( ) ;
189+ } ,
190+
191+ /*
192+ * Process queue of messages that have accumulated while jQuery is loading
193+ */
194+ processQueue : function ( ) {
195+ var queue = this . queue ,
196+ len = queue . length ,
197+ i ;
198+
199+ for ( i = 0 ; i < len ; i ++ ) {
200+ this . postMessage ( queue [ i ] ) ;
201+ }
202+
203+ // Reset queue
204+ this . queue = [ ] ;
205+ }
158206} ;
159207
160208
0 commit comments