decoder.js
function( callback ) {
		var self = this;
		if (!callback) callback = function(){};

		// If there are no queued requests, fire callback as-is
		if (this.queuedRequests.length === 0) {
			callback( null, this.database );
			return;
		}

		// First make sure that there are no bundles pending loading
		var pendingLoading = false;
		for (var i=0; i<this.queuedRequests.length; ++i) {
			if (this.queuedRequests[i].status === PBUND_REQUESTED) {
				pendingLoading = true;
				break;
			}
		}

		////////////////////////////////////////////////////////
		// Iteration 1 - PBUND_REQUESTED -> PBUND_LOADED
		// ----------------------------------------------------
		// Download all bundles in pending state.
		////////////////////////////////////////////////////////

		if (pendingLoading) {

			// Prepare the callbacks for when this is finished
			var state = { 'counter': 0 }
			var continue_callback = (function() {
				// When reached 0, continue loading
				if (--this.counter === 0)
					self.__process( callback );
			}).bind(state);

			// Place all requests in parallel
			var triggeredError = false;
			for (var i=0; i<this.queuedRequests.length; ++i) {
				var req = this.queuedRequests[i], part = this.progressManager.part();
				if (req.status === PBUND_REQUESTED) {

					// Download bundle from URL(s)
					state.counter++;
					req.buffer = downloadArrayBuffers(req.url, part,
						(function(req) {
							return function( err ) {

								// Handle errors
								if (err) {
									if (triggeredError) return;
									var errMsg = "Error downloading bundle: "+err;
									if (req.callback) req.callback( errMsg, null);
									if (callback) callback(errMsg, null);
									triggeredError = true;
									return;
								}

								// Keep buffer and mark as loaded
								req.status = PBUND_LOADED;
								// Continue
								continue_callback();
							}
						})(req)
					);

				}
			}

			// Do not continue, we ar asynchronous
			return;
		}

		////////////////////////////////////////////////////////
		// Iteration 2 - PBUND_LOADED -> PBUND_PARSED
		// ----------------------------------------------------
		// Parse all loaded bundles (synchronous)
		////////////////////////////////////////////////////////

		for (var i=0; i<this.queuedRequests.length; ++i) {
			var req = this.queuedRequests[i];
			if (req.status === PBUND_LOADED) {
				// try {

				// Create bundle from sparse or compact format
				var bundle;
				if (req.buffer.length === 1) {
					bundle = new BinaryBundle( req.buffer[0], self.profile );
				} else {
					bundle = new BinaryBundle( req.buffer, self.profile );
				}

				// Parse bundle
				parseBundle( bundle, self.database );

				// Trigger bundle callback
				if (req.callback) req.callback( null, req.bundle );

				// } catch (e) {

				// 	// Update bundle status
				// 	req.status = PBUND_ERROR;

				// 	// Fire error callbacks and exit
				// 	var errMsg = "Error parsing bundle: "+e.toString();
				// 	if (req.callback) req.callback( errMsg, null);
				// 	if (callback) callback(errMsg, null);
				// 	return;

				// }
			}
		}

		// We are ready
		this.queuedRequests = [];
		callback( null, this.database );

	}
function( callback ) {
		var self = this;
		if (!callback) callback = function(){};

		// If there are no queued requests, fire callback as-is
		if (this.queuedRequests.length === 0) {
			callback( null, this.database );
			return;
		}

		// First make sure that there are no bundles pending loading
		var pendingLoading = false;
		for (var i=0; i<this.queuedRequests.length; ++i) {
			if (this.queuedRequests[i].status === PBUND_REQUESTED) {
				pendingLoading = true;
				break;
			}
		}

		////////////////////////////////////////////////////////
		// Iteration 1 - PBUND_REQUESTED -> PBUND_LOADED
		// ----------------------------------------------------
		// Download all bundles in pending state.
		////////////////////////////////////////////////////////

		if (pendingLoading) {

			// Prepare the callbacks for when this is finished
			var state = { 'counter': 0 }
			var continue_callback = (function() {
				// When reached 0, continue loading
				if (--this.counter === 0)
					self.__process( callback );
			}).bind(state);

			// Place all requests in parallel
			var triggeredError = false;
			for (var i=0; i<this.queuedRequests.length; ++i) {
				var req = this.queuedRequests[i], part = this.progressManager.part();
				if (req.status === PBUND_REQUESTED) {

					// Download bundle from URL(s)
					state.counter++;
					req.buffer = downloadArrayBuffers(req.url, part,
						(function(req) {
							return function( err ) {

								// Handle errors
								if (err) {
									if (triggeredError) return;
									var errMsg = "Error downloading bundle: "+err;
									if (req.callback) req.callback( errMsg, null);
									if (callback) callback(errMsg, null);
									triggeredError = true;
									return;
								}

								// Keep buffer and mark as loaded
								req.status = PBUND_LOADED;
								// Continue
								continue_callback();
							}
						})(req)
					);

				}
			}

			// Do not continue, we ar asynchronous
			return;
		}

		////////////////////////////////////////////////////////
		// Iteration 2 - PBUND_LOADED -> PBUND_PARSED
		// ----------------------------------------------------
		// Parse all loaded bundles (synchronous)
		////////////////////////////////////////////////////////

		for (var i=0; i<this.queuedRequests.length; ++i) {
			var req = this.queuedRequests[i];
			if (req.status === PBUND_LOADED) {
				// try {

				// Create bundle from sparse or compact format
				var bundle;
				if (req.buffer.length === 1) {
					bundle = new BinaryBundle( req.buffer[0], self.profile );
				} else {
					bundle = new BinaryBundle( req.buffer, self.profile );
				}

				// Parse bundle
				parseBundle( bundle, self.database );

				// Trigger bundle callback
				if (req.callback) req.callback( null, req.bundle );

				// } catch (e) {

				// 	// Update bundle status
				// 	req.status = PBUND_ERROR;

				// 	// Fire error callbacks and exit
				// 	var errMsg = "Error parsing bundle: "+e.toString();
				// 	if (req.callback) req.callback( errMsg, null);
				// 	if (callback) callback(errMsg, null);
				// 	return;

				// }
			}
		}

		// We are ready
		this.queuedRequests = [];
		callback( null, this.database );

	}
function ( callback ) { var self = this ; if ( ! callback ) callback = function ( ) { } ; // If there are no queued requests, fire callback as-is if ( this . queuedRequests . length === 0 ) { callback ( null , this . database ) ; return ; } // First make sure that there are no bundles pending loading var pendingLoading = false ; for ( var i = 0 ; i < this . queuedRequests . length ; ++ i ) { if ( this . queuedRequests [ i ] . status === PBUND_REQUESTED ) { pendingLoading = true ; break ; } } //////////////////////////////////////////////////////// // Iteration 1 - PBUND_REQUESTED -> PBUND_LOADED // ---------------------------------------------------- // Download all bundles in pending state. //////////////////////////////////////////////////////// if ( pendingLoading ) { // Prepare the callbacks for when this is finished var state = { 'counter' : 0 } var continue_callback = ( function ( ) { // When reached 0, continue loading if ( -- this . counter === 0 ) self . __process ( callback ) ; } ) . bind ( state ) ; // Place all requests in parallel var triggeredError = false ; for ( var i = 0 ; i < this . queuedRequests . length ; ++ i ) { var req = this . queuedRequests [ i ] , part = this . progressManager . part ( ) ; if ( req . status === PBUND_REQUESTED ) { // Download bundle from URL(s) state . counter ++ ; req . buffer = downloadArrayBuffers ( req . url , part , ( function ( req ) { return function ( err ) { // Handle errors if ( err ) { if ( triggeredError ) return ; var errMsg = "Error downloading bundle: " + err ; if ( req . callback ) req . callback ( errMsg , null ) ; if ( callback ) callback ( errMsg , null ) ; triggeredError = true ; return ; } // Keep buffer and mark as loaded req . status = PBUND_LOADED ; // Continue continue_callback ( ) ; } } ) ( req ) ) ; } } // Do not continue, we ar asynchronous return ; } //////////////////////////////////////////////////////// // Iteration 2 - PBUND_LOADED -> PBUND_PARSED // ---------------------------------------------------- // Parse all loaded bundles (synchronous) //////////////////////////////////////////////////////// for ( var i = 0 ; i < this . queuedRequests . length ; ++ i ) { var req = this . queuedRequests [ i ] ; if ( req . status === PBUND_LOADED ) { // try { // Create bundle from sparse or compact format var bundle ; if ( req . buffer . length === 1 ) { bundle = new BinaryBundle ( req . buffer [ 0 ] , self . profile ) ; } else { bundle = new BinaryBundle ( req . buffer , self . profile ) ; } // Parse bundle parseBundle ( bundle , self . database ) ; // Trigger bundle callback if ( req . callback ) req . callback ( null , req . bundle ) ; // } catch (e) { // 	// Update bundle status // 	req.status = PBUND_ERROR; // 	// Fire error callbacks and exit // 	var errMsg = "Error parsing bundle: "+e.toString(); // 	if (req.callback) req.callback( errMsg, null); // 	if (callback) callback(errMsg, null); // 	return; // } } } // We are ready this . queuedRequests = [ ] ; callback ( null , this . database ) ; }
Parse the stack of bundles currently loaded
Parse the stack of bundles currently loaded