Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Rough implementation of as a callback.

  • Loading branch information...
commit 648784e443b09a6ed7432b646dd5936529a1a1f1 1 parent 7b99b80
@xavi- authored
Showing with 44 additions and 32 deletions.
  1. +23 −18 index.js
  2. +1 −1  test/basic-checks.js
  3. +9 −6 test/val.js
  4. +11 −7 test/valArray.js
View
41 index.js
@@ -43,15 +43,20 @@ function errInfo(step, paramIdx, paramName) {
return { name: step, paramIdx: paramIdx, paramName: paramName };
}
function StepObj(params, jumpTo, name) {
- this._params = params;
- this._jumpTo = jumpTo;
- this.name = name;
+ var step = function() { return step.val.apply(step, arguments); };
+ step._params = params;
+ step._jumpTo = jumpTo;
+ step._name = name;
+
+ step.__proto__ = StepObj.prototype;
+
+ return step;
}
StepObj.prototype = {
val: function(name) {
var self = this, paramIdx = this._params.nextIdx();
return function(err, val) {
- if(err) { return self._params.error(err, errInfo(self.name, paramIdx, name)); }
+ if(err) { return self._params.error(err, errInfo(self._name, paramIdx, name)); }
self._params.done(paramIdx, val);
}
@@ -60,7 +65,7 @@ StepObj.prototype = {
name = (name || "array");
var self = this, paramIdx = this._params.nextIdx();
var arrayVals = new ParamList(function(err) {
- if(err) { return self._params.error(err, errInfo(self.name, paramIdx, err.step.name)); }
+ if(err) { return self._params.error(err, errInfo(self._name, paramIdx, err.step.name)); }
self._params.done(paramIdx, arrayVals.vals.slice(1));
});
@@ -68,21 +73,21 @@ StepObj.prototype = {
// Handles arrays of zero length
process.nextTick(function() { arrayVals.checkPending(); });
- return {
- val: function(valName) {
- valName = (valName || name + "(" + valIdx + ")");
- var valIdx = arrayVals.nextIdx();
+ var arrayCB = function(valName) {
+ valName = (valName || name + "(" + valIdx + ")");
+ var valIdx = arrayVals.nextIdx();
- return function(err, val) {
- if(err) { return arrayVals.error(err, errInfo("", 0, valName)); }
+ return function(err, val) {
+ if(err) { return arrayVals.error(err, errInfo("", 0, valName)); }
- arrayVals.done(valIdx, val);
- };
- },
- syncVal: function(val, valName) {
- this.val(valName)(null, val);
- }
+ arrayVals.done(valIdx, val);
+ };
};
+ arrayCB.val = arrayCB;
+ arrayCB.syncVal = function(val, valName) {
+ this.val(valName)(null, val);
+ }
+ return arrayCB;
},
syncVal: function(val, name) {
this.val(name)(null, val);
@@ -92,7 +97,7 @@ StepObj.prototype = {
var chunks = [];
emitter.on('data', function (chunk) { chunks.push(chunk); });
- emitter.on('error', function(err) { self._params.error(err, errInfo(self.name, paramIdx, name)); });
+ emitter.on('error', function(err) { self._params.error(err, errInfo(self._name, paramIdx, name)); });
emitter.on('end', function() { self._params.done(paramIdx, chunks); });
},
jumpTo: function(func, args) {
View
2  test/basic-checks.js
@@ -2,7 +2,7 @@ var assert = require("assert");
module.exports = {
save: function(stepObj, args) {
- stepObj.data[stepObj.name] = { when: Date.now(), args: Array.prototype.slice.call(args) };
+ stepObj.data[stepObj._name] = { when: Date.now(), args: Array.prototype.slice.call(args) };
},
coverage: function(names) {
return function(data) {
View
15 test/val.js
@@ -13,13 +13,14 @@ vows.describe("Test `this.val`").addBatch({
check.save(this, arguments);
fs.readFile(__filename, 'utf8', this.val());
+ fs.readFile(__filename, 'utf8', this());
},
- function capitalize(err, text) {
+ function capitalize(err, text1, text2) {
if(err) { throw err; }
check.save(this, arguments);
- this.syncVal(text.toUpperCase());
+ this.syncVal(text1.toUpperCase());
},
function showIt(err, newText) {
if(err) { throw err; }
@@ -36,7 +37,9 @@ vows.describe("Test `this.val`").addBatch({
"steps called in order": check.order([ "readSelf", "capitalize", "showIt" ]),
"check file loaded": function(data) {
var testText = fs.readFileSync(__filename, 'utf8');
- assert.equal(testText, data["capitalize"].args[1], "Text Loaded");
+ assert.equal(testText, data["capitalize"].args[1], "Text1 Loaded");
+ assert.equal(testText, data["capitalize"].args[2], "Text2 Loaded");
+ assert.equal(data["capitalize"].args[1], data["capitalize"].args[2], "Text1 does not equal Text2");
},
"check text is capitalize": function(data) {
assert.equal(data["capitalize"].args[1].toUpperCase(), data["showIt"].args[1], "Text Uppercased");
@@ -48,7 +51,7 @@ vows.describe("Test `this.val`").addBatch({
function calls() {
check.save(this, arguments);
- var p1 = this.val(), p2 = this.val();
+ var p1 = this.val(), p2 = this();
setTimeout(function() { p1(null, 1); }, 0);
setTimeout(function() { p2(null, 2); }, 0);
},
@@ -76,7 +79,7 @@ vows.describe("Test `this.val`").addBatch({
function calls() {
check.save(this, arguments);
- var p1 = this.val(), p2 = this.val();
+ var p1 = this.val(), p2 = this();
p1(null, 1);
p2(null, 2);
},
@@ -107,7 +110,7 @@ vows.describe("Test `this.val`").addBatch({
var p1 = this.val();
setTimeout(function() { p1(null, 1); }, 10);
this.syncVal(2);
- var p3 = this.val();
+ var p3 = this();
setTimeout(function() { p3(null, 3); }, 0);
},
function results(err, one, two, three) {
View
18 test/valArray.js
@@ -17,14 +17,16 @@ vows.describe("Test `this.valArray`").addBatch({
check.save(this, arguments);
- var files = this.valArray();
+ var files1 = this.valArray();
+ var files2 = this.valArray();
results.forEach(function (filename) {
if (/\.js$/.test(filename)) {
- fs.readFile(__dirname + "/" + filename, 'utf8', files.val());
+ fs.readFile(__dirname + "/" + filename, 'utf8', files1.val());
+ fs.readFile(__dirname + "/" + filename, 'utf8', files2.val());
}
});
},
- function showAll(err, files) {
+ function showAll(err, files1, files2) {
if(err) { throw err; }
check.save(this, arguments);
@@ -45,6 +47,8 @@ vows.describe("Test `this.valArray`").addBatch({
assert.deepEqual(data["readFiles"].args[1], dirListing);
assert.deepEqual(data["showAll"].args[1], dirResults);
+ assert.deepEqual(data["showAll"].args[2], dirResults);
+ assert.deepEqual(data["showAll"].args[1], data["showAll"].args[2]);
}
},
"test empty valArray": {
@@ -78,7 +82,7 @@ vows.describe("Test `this.valArray`").addBatch({
check.save(this, arguments);
var group = this.valArray();
- var p1 = group.val(), p2 = group.val();
+ var p1 = group.val(), p2 = group();
p1(null, 1);
p2(null, 2);
},
@@ -109,7 +113,7 @@ vows.describe("Test `this.valArray`").addBatch({
var p1 = group.val();
setTimeout(function() { p1(null, 1); }, 10);
group.syncVal(2);
- var p3 = group.val();
+ var p3 = group();
setTimeout(function() { p3(null, 3); }, 0);
},
function results(err, arr) {
@@ -142,11 +146,11 @@ vows.describe("Test `this.valArray`").addBatch({
var g1p1 = group1.val();
setTimeout(function() { g1p1(null, 1); }, 10);
group1.syncVal(2);
- var g1p3 = group1.val();
+ var g1p3 = group1();
setTimeout(function() { g1p3(null, 3); }, 0);
var group2 = this.valArray();
- var g2p1 = group2.val(), g2p2 = group2.val(), g2p3 = group2.val();
+ var g2p1 = group2.val(), g2p2 = group2(), g2p3 = group2.val();
setTimeout(function() { g2p1(null, "a"); }, 0);
setTimeout(function() { g2p2(null, "b"); }, 0);
setTimeout(function() { g2p3(null, "c"); }, 0);
Please sign in to comment.
Something went wrong with that request. Please try again.