1 jasmine.Queue = function(env) {
  2   this.env = env;
  3   this.blocks = [];
  4   this.running = false;
  5   this.index = 0;
  6   this.offset = 0;
  7   this._stopped = 0;
  8 };
  9 
 10 jasmine.Queue.prototype.addBefore = function(block) {
 11   this.blocks.unshift(block);
 12 };
 13 
 14 jasmine.Queue.prototype.add = function(block) {
 15   this.blocks.push(block);
 16 };
 17 
 18 jasmine.Queue.prototype.insertNext = function(block) {
 19   this.blocks.splice((this.index + this.offset + 1), 0, block);
 20   this.offset++;
 21 };
 22 
 23 jasmine.Queue.prototype.start = function(onComplete) {
 24   this.running = true;
 25   this.onComplete = onComplete;
 26   this.next_();
 27 };
 28 
 29 jasmine.Queue.prototype._start = function() {
 30   var self = this;
 31   this._stopped--;
 32   if(this._stopped == 0){
 33     setTimeout(function(){self.next_();},0);
 34   }
 35 };
 36 
 37 jasmine.Queue.prototype._stop = function() {
 38   this._stopped++;
 39 };
 40 
 41 jasmine.Queue.prototype.isRunning = function() {
 42   return this.running;
 43 };
 44 
 45 jasmine.Queue.LOOP_DONT_RECURSE = true;
 46 
 47 jasmine.Queue.prototype.next_ = function() {
 48   var self = this;
 49   var goAgain = true;
 50 
 51   // debug("stopped",this._stopped,self.index,self.blocks.length);
 52   while (goAgain && this._stopped <= 0) {
 53     goAgain = false;
 54     
 55     if (self.index < self.blocks.length) {
 56       var calledSynchronously = true;
 57       var completedSynchronously = false;
 58 
 59       var onComplete = function () {
 60         if (jasmine.Queue.LOOP_DONT_RECURSE && calledSynchronously) {
 61           completedSynchronously = true;
 62           return;
 63         }
 64 
 65         if( self.blocks[self.index]._anticipate !== undefined ) {
 66         }
 67 
 68         self.offset = 0;
 69         self.index++;
 70 
 71         var now = new Date().getTime();
 72         if (self.env.updateInterval && now - self.env.lastUpdate > self.env.updateInterval) {
 73           self.env.lastUpdate = now;
 74           self.env.setTimeout(function() {
 75             self.next_();
 76           }, 0);
 77         } else {
 78           if (jasmine.Queue.LOOP_DONT_RECURSE && completedSynchronously) {
 79             goAgain = true;
 80           } else {
 81             self.next_();
 82           }
 83         }
 84       };
 85       self.blocks[self.index].execute(onComplete);
 86 
 87       calledSynchronously = false;
 88       if (completedSynchronously) {
 89         onComplete();
 90       }
 91       
 92     } else {
 93       self.running = false;
 94       if (self.onComplete) {
 95         self.onComplete();
 96       }
 97     }
 98   }
 99 };
100 
101 jasmine.Queue.prototype.results = function() {
102   var results = new jasmine.NestedResults();
103   for (var i = 0; i < this.blocks.length; i++) {
104     if (this.blocks[i].results) {
105       results.addResult(this.blocks[i].results());
106     }
107   }
108   return results;
109 };
110 
111 
112