      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  * @param {function} pollValue The poll function to try and get the value every
     23  * 1 second if the value is not set.
     24  */
     25 minplayer.async.prototype.get = function(callback, pollValue) {
     26
     27   // If the value is set, then immediately call the callback, otherwise, just
     28   // add it to the queue when the variable is set.
     29   if (this.value !== null) {
     30     callback(this.value);
     31   }
     32   else {
     33
     34     // Add this callback to the queue.
     35     this.queue.push(callback);
     36   }
     37 };
     38
     39 /**
     40  * Sets the value of an asynchronous value.
     41  *
     42  * @param {void} val The value to set.
     43  */
     44 minplayer.async.prototype.set = function(val) {
     45
     46   // Set the value.
     47   this.value = val;
     48
     49   // Get the callback queue length.
     50   var i = this.queue.length;
     51
     52   // Iterate through all the callbacks and call them.
     53   if (i) {
     54     while (i--) {
     55       this.queue[i](val);
     56     }
     57
     58     // Reset the queue.
     59     this.queue = [];
     60   }
     61 };
     62