|
7 | 7 | // This function takes sets of files to load asynchronously. Each set will be loaded after |
8 | 8 | // the previous set has completed loading. That is, each require and it's dependencies in a |
9 | 9 | // set will be loaded asynchronously, but each set will be run in serial. |
10 | | - asyncLoad: function( seq ) { |
| 10 | + asyncLoad: function( seq, baseUrl ) { |
11 | 11 | require({ |
12 | | - baseUrl: "../../../js" |
| 12 | + baseUrl: ( baseUrl || "../../../js" ) |
13 | 13 | }); |
14 | 14 |
|
15 | 15 | function loadSeq( seq, i ){ |
|
211 | 211 | fn( timedOut ); |
212 | 212 | }, |
213 | 213 |
|
| 214 | +// detailedEventCascade: call a function and expect a series of events to be triggered (or not to be triggered), and guard |
| 215 | +// with a timeout against getting stood up. Record the result (timed out / was triggered) for each event, and the order |
| 216 | +// in which the event arrived wrt. any other events expected. |
| 217 | +// seq : [ |
| 218 | +// fn(result), |
| 219 | +// { key: { |
| 220 | +// src: event source (is jQuery object or function returning jQuery object), |
| 221 | +// event: event name (is string), |
| 222 | +// NB: It's a good idea to namespace your events, because the handler will be removed |
| 223 | +// based on the name you give here if a timeout occurs before the event fires. |
| 224 | +// userData1: value, |
| 225 | +// ... |
| 226 | +// userDatan: value |
| 227 | +// }, |
| 228 | +// ... |
| 229 | +// ] |
| 230 | +// ... |
| 231 | +// ] |
| 232 | +// result: { |
| 233 | +// key: { |
| 234 | +// idx: order in which the event fired |
| 235 | +// src: event source (is jQuery object), |
| 236 | +// event: event name (is string) |
| 237 | +// timedOut: timed out (is boolean) |
| 238 | +// userData1: value, |
| 239 | +// ... |
| 240 | +// userDatan: value |
| 241 | +// } |
| 242 | +// ... |
| 243 | +// } |
| 244 | + detailedEventCascade: function( seq, result ) { |
| 245 | + // grab one step from the sequence |
| 246 | + var fn = seq.shift(), |
| 247 | + events = seq.shift(), |
| 248 | + self = this, |
| 249 | + derefSrc = function( src ) { |
| 250 | + return ( $.isFunction( src ) ? src() : src ); |
| 251 | + }; |
| 252 | + |
| 253 | + // we're done |
| 254 | + if ( fn === undefined ) { |
| 255 | + return; |
| 256 | + } |
| 257 | + |
| 258 | + // Attach handlers to the various objects which are to be checked for correct event generation |
| 259 | + if ( events ) { |
| 260 | + var newResult = {}, |
| 261 | + nEventsDone = 0, |
| 262 | + nEvents = 0, |
| 263 | + // set a failsafe timer in case one of the events never happens |
| 264 | + warnTimer = setTimeout( function() { |
| 265 | + $.each( events, function( key, event ) { |
| 266 | + if ( newResult[ key ] === undefined ) { |
| 267 | + // clean up the unused handler |
| 268 | + derefSrc( event.src ).unbind( event.event ); |
| 269 | + newResult[ key ] = $.extend( {}, event, { timedOut: true } ); |
| 270 | + } |
| 271 | + }); |
| 272 | + |
| 273 | + // Move on to the next step |
| 274 | + self.detailedEventCascade( seq, newResult ); |
| 275 | + }, 20000); |
| 276 | + |
| 277 | + function recordResult( key, event, result ) { |
| 278 | + // Record the result |
| 279 | + newResult[ key ] = $.extend( {}, event, result ); |
| 280 | + // Increment the number of received responses |
| 281 | + nEventsDone++; |
| 282 | + if ( nEventsDone === nEvents ) { |
| 283 | + // clear the timeout and move on to the next step when all events have been received |
| 284 | + clearTimeout( warnTimer ); |
| 285 | + setTimeout( function() { |
| 286 | + self.detailedEventCascade( seq, newResult ); |
| 287 | + }, 0); |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 291 | + $.each( events, function( key, event ) { |
| 292 | + // Count the events so that we may know how many responses to expect |
| 293 | + nEvents++; |
| 294 | + // If it's an event |
| 295 | + if ( event.src ) { |
| 296 | + // Hook up to the event |
| 297 | + derefSrc( event.src ).one( event.event, function() { |
| 298 | + recordResult( key, event, { timedOut: false, idx: nEventsDone } ); |
| 299 | + }); |
| 300 | + } |
| 301 | + // If it's a timeout |
| 302 | + else { |
| 303 | + setTimeout( function() { |
| 304 | + recordResult( key, event, { timedOut: true, idx: -1 } ); |
| 305 | + }, event.length ); |
| 306 | + } |
| 307 | + }); |
| 308 | + } |
| 309 | + |
| 310 | + // Call the function with the result of the events |
| 311 | + fn( result ); |
| 312 | + }, |
| 313 | + |
214 | 314 | deferredSequence: function(fns) { |
215 | 315 | var fn = fns.shift(), |
216 | 316 | deferred = $.Deferred(), |
|
0 commit comments