dwf/rva: async working with .waits() syntax; simple case

This commit is contained in:
pivotal
2008-12-01 14:24:13 -08:00
parent a4979fe851
commit 3e02023d9f
5 changed files with 89 additions and 53 deletions

View File

@@ -3,9 +3,10 @@
// Object.create instead of new Object
if (typeof Object.create !== 'function') {
Object.create = function (o) {
var F = function () {};
F.prototype = o;
return new F();
var F = function () {
};
F.prototype = o;
return new F();
};
}
@@ -41,13 +42,13 @@ Matchers.method('report', function (result, failing_message) {
Matchers.method('should_equal', function (expected) {
return this.report((this.actual === expected),
'Expected ' + expected + ' but got ' + this.actual + '.');
'Expected ' + expected + ' but got ' + this.actual + '.');
});
Matchers.method('should_not_equal', function (expected) {
return this.report((this.actual !== expected),
'Expected ' + expected + ' to not equal ' + this.actual + ', but it does.');
'Expected ' + expected + ' to not equal ' + this.actual + ', but it does.');
});
/*
@@ -72,24 +73,58 @@ var it = function (description, func) {
return that;
}
var queuedFunction = function(func, timeout, spec) {
var that = {
func: func,
next: function () {spec.finish()},
execute: function () {
if (timeout > 0) {
setTimeout(function () {
that.func();
that.next();
}, timeout);
} else {
that.func();
that.next();
}
}
}
return that;
}
var it_async = function (description) {
var that = {
description: description,
queue: [],
currentTimeout: 0,
waits: function (timeout) {
that.currentTimeout = timeout;
return that;
},
resetTimeout: function() {
that.currentTimeout = 0;
},
finish: function() {
that.done = true;
},
done: false,
execute: function () {
for(i = 0; i < that.queue.length; i++) {
that.queue[i]();
if (that.queue[0]) {
that.queue[0].execute();
}
}
};
};
var addToQueue = function(func) {
that.queue.push(func);
currentFunction = queuedFunction(func, that.currentTimeout, that);
that.queue.push(currentFunction);
if (that.previousFunction) {
that.previousFunction.next = function () {
currentFunction.execute();
}
}
that.resetTimeout();
that.previousFunction = currentFunction;
return that;
}