1 /**
  2  * base for Runner & Suite: allows for a queue of functions to get executed, allowing for
  3  *   any one action to complete, including asynchronous calls, before going to the next
  4  *   action.
  5  *
  6  * @constructor
  7  * @param {jasmine.Env} env
  8  */
  9 jasmine.ActionCollection = function(env) {
 10   this.env = env;
 11   this.actions = [];
 12   this.index = 0;
 13   this.finished = false;
 14 };
 15 
 16 /**
 17  * Marks the collection as done & calls the finish callback, if there is one
 18  */
 19 jasmine.ActionCollection.prototype.finish = function() {
 20   if (this.finishCallback) {
 21     this.finishCallback();
 22   }
 23   this.finished = true;
 24 };
 25 
 26 /**
 27  * Starts executing the queue of functions/actions.
 28  */
 29 jasmine.ActionCollection.prototype.execute = function() {
 30   if (this.actions.length > 0) {
 31     this.next();
 32   }
 33 };
 34 
 35 /**
 36  * Gets the current action.
 37  */
 38 jasmine.ActionCollection.prototype.getCurrentAction = function() {
 39   return this.actions[this.index];
 40 };
 41 
 42 /**
 43  * Executes the next queued function/action. If there are no more in the queue, calls #finish.
 44  */
 45 jasmine.ActionCollection.prototype.next = function() {
 46   if (this.index >= this.actions.length) {
 47     this.finish();
 48     return;
 49   }
 50 
 51   var currentAction = this.getCurrentAction();
 52 
 53   currentAction.execute(this);
 54 
 55   if (currentAction.afterCallbacks) {
 56     for (var i = 0; i < currentAction.afterCallbacks.length; i++) {
 57       try {
 58         currentAction.afterCallbacks[i]();
 59       } catch (e) {
 60         alert(e);
 61       }
 62     }
 63   }
 64 
 65   this.waitForDone(currentAction);
 66 };
 67 
 68 jasmine.ActionCollection.prototype.waitForDone = function(action) {
 69   var self = this;
 70   var afterExecute = function afterExecute() {
 71     self.index++;
 72     self.next();
 73   };
 74 
 75   if (action.finished) {
 76     var now = new Date().getTime();
 77     if (this.env.updateInterval && now - this.env.lastUpdate > this.env.updateInterval) {
 78       this.env.lastUpdate = now;
 79       this.env.setTimeout(afterExecute, 0);
 80     } else {
 81       afterExecute();
 82     }
 83     return;
 84   }
 85 
 86   var id = this.env.setInterval(function() {
 87     if (action.finished) {
 88       self.env.clearInterval(id);
 89       afterExecute();
 90     }
 91   }, 150);
 92 };
 93