1 /** The minplayer namespace. */ 2 var minplayer = minplayer || {}; 3 4 /** 5 * @constructor 6 * @class This class keeps track of asynchronous get requests for certain 7 * variables within the player. 8 */ 9 minplayer.async = function() { 10 11 /** The final value of this asynchronous variable. */ 12 this.value = null; 13 14 /** The queue of callbacks to call when this value is determined. */ 15 this.queue = []; 16 }; 17 18 /** 19 * Retrieve the value of this variable. 20 * 21 * @param {function} callback The function to call when the value is determined. 22 * 1 second if the value is not set. 23 */ 24 minplayer.async.prototype.get = function(callback) { 25 26 // If the value is set, then immediately call the callback, otherwise, just 27 // add it to the queue when the variable is set. 28 if (this.value !== null) { 29 callback(this.value); 30 } 31 else { 32 33 // Add this callback to the queue. 34 this.queue.push(callback); 35 } 36 }; 37 38 /** 39 * Sets the value of an asynchronous value. 40 * 41 * @param {void} val The value to set. 42 */ 43 minplayer.async.prototype.set = function(val) { 44 45 // Set the value. 46 this.value = val; 47 48 // Get the callback queue length. 49 var i = this.queue.length; 50 51 // Iterate through all the callbacks and call them. 52 if (i) { 53 while (i--) { 54 this.queue[i](val); 55 } 56 57 // Reset the queue. 58 this.queue = []; 59 } 60 }; 61