Skip to content

Commit

Permalink
Create a named jump function.
Browse files Browse the repository at this point in the history
Add a function `step.jump` to make explicit the action performed when
passing in a function in the cadence. Using an existing function is too
clever, I believe.

See: https://github.com/bigeasy/cadence/blob/master/diary.md#jumping-the-parent

Closes #102.

File                             Raw         Min         Zip     Min/Zip
index.js                 10926/10.67  3535/03.45  3128/03.05  1332/01.30
  • Loading branch information
flatheadmill committed Jun 17, 2013
1 parent a46858f commit 36b5ed2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
34 changes: 16 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,6 @@ function cadence () {
return;
}

// If our first argument is a function, we check to see if it is a jump
// instruction. If the function is a member of the current cadence, we
// will invoke that function with the results of this step.

// Search for the function in the current cadence.
if (vargs.length == 1 && typeof vargs[0] == "function") {
for (i = invocations[0].args[0].length - 1;
i > -1 && invocations[0].args[0][i] !== vargs[0]; i--) {}
}

// If we find the function in the current cadence, we set the index of
// next step function to execute; then remove the function argument and
// proceed.
if (~i) {
invocations[0].args[1] = i;
vargs.shift();
}

var callback = { errors: [], results: [] };
var fixup;
if (fixup = (vargs[0] === async)) {
Expand Down Expand Up @@ -122,6 +104,22 @@ function cadence () {
return createHandler.apply({ event: true }, [0, []].concat(__slice.call(arguments)))
}

async.jump = function (label) {
// If our first argument is a function, we check to see if it is a jump
// instruction. If the function is a member of the current cadence, we
// will invoke that function with the results of this step.

// Search for the function in the current cadence. If we find the function
// in the current cadence, we set the index of next step function to
// execute; then remove the function argument and proceed.
for (var i = 0, I = invocations[0].args[0].length; i < I; i++) {
if (invocations[0].args[0][i] === label) {
invocations[0].args[1] = i;
return;
}
}
}

// Create a sub-cadence.
function createCadence (invocation, callback) {
var index = 0;
Expand Down
5 changes: 3 additions & 2 deletions t/cadence/jump.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ require('proof')(1, function (equal) {
}, inc = function (count) {
step()(null, count + 1);
}, function (count) {
if (count != 10) step(inc)(null, count);
else return count;
if (count != 10) step.jump(inc);
return count;
});
})(function (error, result) {
if (error) throw error;
equal(result, 10, "jump");
});
});

0 comments on commit 36b5ed2

Please sign in to comment.