Skip to content

Commit

Permalink
Fix loop through errors, catch function this.
Browse files Browse the repository at this point in the history
Closes #126.
Closes #125.

File                             Raw         Min         Zip     Min/Zip
index.js                 16644/16.25  5181/05.06  4607/04.50  1881/01.84
  • Loading branch information
flatheadmill committed Jul 8, 2013
1 parent f5b71bd commit 754eade
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
3 changes: 3 additions & 0 deletions diary.md
Original file line number Diff line number Diff line change
Expand Up @@ -2622,3 +2622,6 @@ It was supposed to be about control flow, jumping, but it's become to be all
about parallelism.
## Next Change Log
* Unable to loop through exception handlers. #126.
* Catch functions are not invoked with the correct `this`. #125.
26 changes: 17 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function cadence () {
var vargs = __slice.call(arguments, 0),
callback = function (error) { if (error) throw error };
if (vargs.length) callback = vargs.pop();
march.call(this, null, steps, [async].concat(vargs), function (errors, finalizers) {
march.call(this, {}, steps, [async].concat(vargs), function (errors, finalizers) {
var vargs = [null].concat(__slice.call(arguments, 2));
finalize(finalizers, 0, errors, function (errors) {
if (errors.length) {
Expand Down Expand Up @@ -183,7 +183,7 @@ function cadence () {
// in the current cadence, we set the index of next step function to
// execute; then remove the function argument and proceed.
var invocation = invocations[0];
while (invocation) {
while (invocation.args) {
for (var i = 0, I = invocation.args[0].steps.length; i < I; i++) {
if (invocation.args[0].steps[i] === label) {
invocation.args[1] = i;
Expand Down Expand Up @@ -354,15 +354,23 @@ function cadence () {
var callbacks = previous.callbacks, args = [], arg, step, result, hold;

if (previous.errors.length) {
// TODO: The finalizer juggling here is ugly. Come up with a consistent
// finalizer pattern.
var catcher = cadence.catchers[index - 1];
if (catcher) {
cadence.catchers[index - 1] = null;
cadence.steps[index - 1] = catcher;
previous.__args = [ previous.errors, previous.errors[0] ];
previous.callbacks.length = 1;
invoke(cadence, index - 1, previous, callback);
march.call(previous.self, previous, [ catcher ], [ previous.errors, previous.errors[0] ], function (errors, finalizers) {
previous.errors = [];
__push.apply(previous.finalizers, finalizers);
if (errors.length) {
arguments[1] = previous.finalizers;
callback.apply(this, __slice.call(arguments));
} else {
previous.__args = __slice.call(arguments, 2);
invoke.call(previous.self, cadence, index, previous, callback);
}
});
} else {
var finalizers = previous.finalizers.splice(0, previous.finalizers.length);
var finalizers = previous.finalizers.splice(0, previous.finalizers.length);
callback(previous.errors, finalizers);
}
return;
Expand Down Expand Up @@ -423,7 +431,7 @@ function cadence () {
try {
result = step.apply(this, args);
} catch (errors) {
if (errors === previous.errors) {
if (errors === previous.caller.errors) {
invocations[0].errors.uncaught = errors.uncaught;
} else {
errors = [ errors ];
Expand Down
6 changes: 4 additions & 2 deletions t/cadence/errors.t.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

require('proof')(18, function (equal, ok) {
require('proof')(19, function (equal, ok) {
var fs = require('fs')
, cadence = require('../..')
, errors = []
Expand All @@ -12,11 +12,13 @@ require('proof')(18, function (equal, ok) {
equal(error.message, "thrown", "intercepted throw");
});

var self = {};
cadence([function (step) {
step()(new Error("handled"));
}, function (errors) {
ok(self === this);
equal(errors[0].message, "handled", "intercepted passed along");
}])();
}]).call(self);

cadence([function (step) {
step()();
Expand Down

0 comments on commit 754eade

Please sign in to comment.