Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add: better error handling, tests, and the ability to return from a b…

…ubble function
  • Loading branch information...
commit 1c109686b70d0b2feadbc6d1f422fee28e5f1ec8 1 parent aa26511
@tmpvar authored
Showing with 55 additions and 11 deletions.
  1. +11 −4 index.js
  2. +7 −2 package.json
  3. +37 −5 test.js
View
15 index.js
@@ -14,12 +14,15 @@
chain = this._chain,
errors = [],
bubbles = [],
- capture = function() {
+ capture = function(data) {
index++;
if (index>=chain.length) {
if (op) {
- op(data, function() {
+ op(data, function(err, data) {
+ if (err) {
+ errors.push(err);
+ }
// called when the core of the chain is complete
bubble(data);
});
@@ -47,14 +50,18 @@
fn((errors.length) ? errors : null, data);
} else {
var bubbleFn = bubbles.pop();
- bubbleFn(data, function done() {
+ var ret = bubbleFn(data, function done(err, data) {
+ if (err) { errors.push(err); }
bubble(data);
});
+ if (typeof ret !== 'undefined') {
+ bubble(ret);
+ }
}
};
this._where = 0;
- capture(bubble);
+ capture(data);
}
};
View
9 package.json
@@ -5,11 +5,16 @@
"version": "0.1.0",
"homepage": "http://tmpvar.com/project/filterchain",
"repository": {
- "url": ""
+ "type": "git",
+ "url": "git://github.com/tmpvar/filterchain.git"
},
"engines": {
"node": "*"
},
"dependencies": {},
- "devDependencies": {}
+ "devDependencies": {},
+ "main": "index",
+ "scripts": {
+ "test": "node test.js"
+ }
}
View
42 test.js
@@ -8,28 +8,44 @@ equal = function(expected, actual) {
}
+
// Successful pre + post execution
var basic = fc.createChain([
function(data, next, cancel) {
data.hello = 'pre-' + data.hello
next(data, function(data, done) {
data.hello += '-post';
- done();
+ done(null, data);
});
},
]);
basic.execute({ hello : 'world' }, function(errors, data) {
- console.log(errors);
- if (errors) { throw new Error(errors) }
+ equal(null, errors)
equal('pre-world-post', data.hello);
});
+
+// Return from bubbler function
+var bubbleReturn = fc.createChain([
+ function(data, next, cancel) {
+ next('hello', function(data, done) {
+ return data + ' world';
+ })
+ }
+]);
+
+bubbleReturn.execute('', function(errors, data) {
+ equal(null, errors);
+ equal('hello world', data);
+});
+
+
// Cancel request
var cancel = fc.createChain([
function(data, next, cancel) {
next(data, function(data, done) {
- done(data += 'outer');
+ done(null, data += 'outer');
});
},
function(data, next, cancel) {
@@ -42,5 +58,21 @@ var cancel = fc.createChain([
]);
cancel.execute('', function(errors, data) {
- equal(data, 'outer');
+ equal('outer', data);
+});
+
+// Call the core function after capture and before bubble
+var captureBubble = fc.createChain([
+ function (data, next, cancel) {
+ next('capture-'+ data, function(data, done) {
+ done(null, data + '-bubble');
+ });
+ }
+], function(data, fn) {
+ fn(null, data + '-core-');
+});
+
+captureBubble.execute('', function(errors, data) {
+ equal(null, errors);
+ equal('capture--core--bubble', data);
});
Please sign in to comment.
Something went wrong with that request. Please try again.