Skip to content

Commit c696ddf

Browse files
committed
Add support for a wrapper array in context
1 parent 8c7e940 commit c696ddf

File tree

3 files changed

+95
-6
lines changed

3 files changed

+95
-6
lines changed

index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const Promise = require('any-promise')
4+
const callMiddleware = require('koa-call-middleware')
45

56
/**
67
* Expose compositor.
@@ -37,12 +38,19 @@ function compose (middleware) {
3738
function dispatch (i) {
3839
if (i <= index) return Promise.reject(new Error('next() called multiple times'))
3940
index = i
40-
const fn = middleware[i] || next
41-
if (!fn) return Promise.resolve()
41+
let fn = middleware[i]
4242
try {
43-
return Promise.resolve(fn(context, function next () {
44-
return dispatch(i + 1)
45-
}))
43+
if (fn) {
44+
return Promise.resolve(callMiddleware(fn, context, function next () {
45+
return dispatch(i + 1)
46+
}))
47+
} else {
48+
if (next) {
49+
return Promise.resolve(next())
50+
} else {
51+
return Promise.resolve()
52+
}
53+
}
4654
} catch (err) {
4755
return Promise.reject(err)
4856
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"index.js"
1313
],
1414
"dependencies": {
15-
"any-promise": "^1.1.0"
15+
"any-promise": "^1.1.0",
16+
"koa-call-middleware": "^1.0.0"
1617
},
1718
"devDependencies": {
1819
"co": "^4.6.0",

test/test.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,66 @@ describe('Koa Compose', function () {
4444
})
4545
})
4646

47+
it('should wrap functions with wrappers', function () {
48+
var arr = []
49+
var stack = []
50+
51+
stack.push(function * (context, next) {
52+
arr.push(1)
53+
yield wait(1)
54+
yield next()
55+
yield wait(1)
56+
arr.push(6)
57+
})
58+
59+
stack.push(function * (context, next) {
60+
arr.push(2)
61+
yield wait(1)
62+
yield next()
63+
yield wait(1)
64+
arr.push(5)
65+
})
66+
67+
stack.push(function * (context, next) {
68+
arr.push(3)
69+
yield wait(1)
70+
yield next()
71+
yield wait(1)
72+
arr.push(4)
73+
})
74+
75+
return compose(stack)({
76+
wrappers: [co.wrap]
77+
}).then(function () {
78+
arr.should.eql([1, 2, 3, 4, 5, 6])
79+
})
80+
})
81+
82+
it('should use wrappers in the correct order', function () {
83+
let completed = []
84+
let stack = []
85+
let arr = []
86+
87+
stack.push(function * (context, next) {
88+
arr.push(1)
89+
yield next()
90+
})
91+
92+
stack.push(function * (context, next) {
93+
arr.push(2)
94+
yield next()
95+
})
96+
97+
return compose(stack)({
98+
wrappers: [co.wrap, function (fn) {
99+
return (ctx, next) => fn(ctx, next).then(completed.push(fn))
100+
}]
101+
}).then(function () {
102+
arr.should.eql([1, 2])
103+
completed.length.should.eql(stack.length)
104+
})
105+
})
106+
47107
it('should only accept an array', function () {
48108
var err
49109
try {
@@ -54,6 +114,26 @@ describe('Koa Compose', function () {
54114
return (err).should.be.instanceof(TypeError)
55115
})
56116

117+
it('should throw an error if calling generators or non-functions', function () {
118+
var stack = [
119+
function * () {}
120+
]
121+
return compose(stack)({}).then(function () {
122+
throw new Error('No error thrown')
123+
}).catch(function (err) {
124+
(err).should.be.instanceof(TypeError)
125+
}).then(function () {
126+
stack = [
127+
{ key: 'value' }
128+
]
129+
return compose(stack)({})
130+
}).then(function () {
131+
throw new Error('No error thrown')
132+
}).catch(function (err) {
133+
(err).should.be.instanceof(TypeError)
134+
})
135+
})
136+
57137
it('should work with 0 middleware', function () {
58138
return compose([])({})
59139
})

0 commit comments

Comments
 (0)