¶QueueRunner.js |
|
getJasmineRequireObj().QueueRunner = function(j$) {
function once(fn) {
var called = false;
return function() {
if (!called) {
called = true;
fn();
}
};
}
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; };
this.enforceTimeout = attrs.enforceTimeout || function() { return false; };
this.userContext = {};
this.timer = attrs.timeout || {setTimeout: setTimeout, clearTimeout: clearTimeout};
}
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) {
return attemptAsync(fn);
} else {
attemptSync(fn);
}
}
var runnerDone = iterativeIndex >= length;
if (runnerDone) {
this.clearStack(this.onComplete);
}
function attemptSync(fn) {
try {
fn.call(self.userContext);
} catch (e) {
handleException(e);
}
}
function attemptAsync(fn) {
var clearTimeout = function () {
Function.prototype.apply.apply(self.timer.clearTimeout, [j$.getGlobal(), [timeoutId]]);
},
next = once(function () {
clearTimeout(timeoutId);
self.run(fns, iterativeIndex + 1);
}),
timeoutId;
if (self.enforceTimeout()) {
timeoutId = Function.prototype.apply.apply(self.timer.setTimeout, [j$.getGlobal(), [function() {
self.onException(new Error('Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.'));
next();
}, j$.DEFAULT_TIMEOUT_INTERVAL]]);
}
try {
fn.call(self.userContext, next);
} catch (e) {
handleException(e);
next();
}
}
function handleException(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;
};
|