Skip to content

Commit

Permalink
proxy composition
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanong committed Jul 6, 2015
1 parent f7ea673 commit 3b1584e
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 115 deletions.
6 changes: 5 additions & 1 deletion index.js
@@ -1,3 +1,4 @@
var composition = require('composition');

/**
* Expose compositor.
Expand All @@ -11,11 +12,14 @@ module.exports = compose;
* of all those which are passed.
*
* @param {Array} middleware
* @param {Boolean} experimental
* @return {Function}
* @api public
*/

function compose(middleware){
function compose(middleware, experimental){
if (experimental) return composition(middleware);

return function *(next){
if (!next) next = noop();

Expand Down
10 changes: 6 additions & 4 deletions package.json
Expand Up @@ -11,13 +11,15 @@
"files": [
"index.js"
],
"dependencies": {},
"dependencies": {
"composition": "^2.1.2"
},
"devDependencies": {
"co": "^4.5.4",
"istanbul-harmony": "0",
"co": "3",
"koa": "0",
"mocha": "1",
"should": "2",
"koa": "0"
"should": "2"
},
"scripts": {
"test": "mocha --harmony --require should --reporter spec",
Expand Down
216 changes: 106 additions & 110 deletions test/test.js
Expand Up @@ -7,148 +7,144 @@ function wait(ms) {
}
}

describe('Koa Compose', function(){
it('should work', function(done){
var arr = [];
var stack = [];

stack.push(function *(next){
arr.push(1);
yield wait(1);
yield next;
yield wait(1);
arr.push(6);
})

stack.push(function *(next){
arr.push(2);
yield wait(1);
yield next;
yield wait(1);
arr.push(5);
})

stack.push(function *(next){
arr.push(3);
yield wait(1);
yield next;
yield wait(1);
arr.push(4);
})

co(compose(stack))(function(err){
if (err) throw err;

arr.should.eql([1, 2, 3, 4, 5, 6]);
done();
})
})

it('should work with 0 middleware', function(done){
co(compose([]))(done);
})

it('should work within a generator', function(done){
var arr = [];

co(function *(){
arr.push(0);

[
true,
false
].forEach(function (experimental) {
describe('Koa Compose', function(){
it('should work', function(done){
var arr = [];
var stack = [];

stack.push(function* (next){
stack.push(function *(next){
arr.push(1);
yield wait(1);
yield next;
arr.push(4);
});
yield wait(1);
arr.push(6);
})

stack.push(function *(next){
arr.push(2);
yield wait(1);
yield next;
arr.push(3);
});
yield wait(1);
arr.push(5);
})

yield compose(stack)
stack.push(function *(next){
arr.push(3);
yield wait(1);
yield next;
yield wait(1);
arr.push(4);
})

arr.push(5);
})(function(err){
if (err) throw err;
return co(compose(stack, experimental)).then(function(arr){
arr.should.eql([1, 2, 3, 4, 5, 6]);
})
})

arr.should.eql([0, 1, 2, 3, 4, 5]);
done();
it('should work with 0 middleware', function(){
return co(compose([], experimental));
})
})

it('should work when yielding at the end of the stack', function(done) {
var stack = [];
it('should work within a generator', function(){
var arr = [];

stack.push(function *(next){
yield next;
});
return co(function *(){
arr.push(0);

co(compose(stack))(done);
})
var stack = [];

it('should work when yielding at the end of the stack with yield*', function(done) {
var stack = [];
stack.push(function* (next){
arr.push(1);
yield next;
arr.push(4);
});

stack.push(function *(next){
yield* next;
});
stack.push(function *(next){
arr.push(2);
yield next;
arr.push(3);
});

co(compose(stack))(done);
})
yield compose(stack, experimental)

arr.push(5);
}).then(function(arr){
arr.should.eql([0, 1, 2, 3, 4, 5]);
})
})

it('should keep the context', function(done){
var ctx = {};
it('should work when yielding at the end of the stack', function() {
var stack = [];

var stack = [];
stack.push(function *(next){
yield next;
});

stack.push(function *(next){
yield next
this.should.equal(ctx);
return co(compose(stack, experimental));
})

stack.push(function *(next){
yield next
this.should.equal(ctx);
})
it('should work when yielding at the end of the stack with yield*', function() {
var stack = [];

stack.push(function *(next){
yield next
this.should.equal(ctx);
stack.push(function *(next){
yield* next;
});

return co(compose(stack, experimental));
})

co(compose(stack)).call(ctx, done);
})
it('should keep the context', function(){
var ctx = {};

it('should catch downstream errors', function(done){
var arr = [];
var stack = [];
var stack = [];

stack.push(function *(next){
arr.push(1);
try {
arr.push(6);
yield next;
arr.push(7);
} catch (err) {
arr.push(2);
}
arr.push(3);
})
stack.push(function *(next){
yield next
this.should.equal(ctx);
})

stack.push(function *(next){
yield next
this.should.equal(ctx);
})

stack.push(function *(next){
arr.push(4);
throw new Error();
arr.push(5);
stack.push(function *(next){
yield next
this.should.equal(ctx);
})

return co.call(ctx, compose(stack, experimental));
})

co(compose(stack))(function(err){
if (err) throw err;
it('should catch downstream errors', function(){
var arr = [];
var stack = [];

stack.push(function *(next){
arr.push(1);
try {
arr.push(6);
yield next;
arr.push(7);
} catch (err) {
arr.push(2);
}
arr.push(3);
})

stack.push(function *(next){
arr.push(4);
throw new Error();
arr.push(5);
})

arr.should.eql([1, 6, 4, 2, 3]);
done();
return co(compose(stack, experimental)).then(function(arr){
arr.should.eql([1, 6, 4, 2, 3]);
})
})
})
})

0 comments on commit 3b1584e

Please sign in to comment.