Files
jasmine/src/core/QueueRunner.js
Davis W. Frank and Sheel Choksi 09fe7b0540 Have QueueRunner run specs iteratively if possible, fallback to recursion for async specs
This prevents the stack from growing as large for the normal cases and is giving a significant speedup for the performance suite
2013-07-02 16:37:38 -07:00

53 lines
1.4 KiB
JavaScript

getJasmineRequireObj().QueueRunner = function() {
function QueueRunner(attrs) {
this.fns = attrs.fns || [];
this.onComplete = attrs.onComplete || function() {};
this.clearStack = attrs.clearStack || function(fn) {fn();};
this.onException = attrs.onException || function() {};
this.catchException = attrs.catchException || function() { return true; };
}
QueueRunner.prototype.execute = function() {
this.run(this.fns, 0);
};
QueueRunner.prototype.run = function(fns, recursiveIndex) {
var length = fns.length,
self = this,
iterativeIndex;
for(iterativeIndex = recursiveIndex; iterativeIndex < length; iterativeIndex++) {
var fn = fns[iterativeIndex];
if (fn.length > 0) {
attempt(function() { fn.call(self, function() {
self.clearStack(function() { self.run(fns, iterativeIndex + 1); });
});
});
return;
} else {
attempt(function() { fn.call(self); });
}
}
if (iterativeIndex >= length) {
this.onComplete();
}
function attempt(fn) {
try {
fn();
} catch (e) {
self.onException(e);
if (!self.catchException(e)) {
//TODO: set a var when we catch an exception and
//use a finally block to close the loop in a nice way..
throw e;
}
}
}
};
return QueueRunner;
};