Skip to content

Commit

Permalink
Added on used support
Browse files Browse the repository at this point in the history
  • Loading branch information
PlasmaPower committed Apr 13, 2016
1 parent 8c7e940 commit a8466f2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function compose (middleware) {
* @api public
*/

return function (context, next) {
let ret = function (context, next) {
// last called middleware #
let index = -1
return dispatch(0)
Expand All @@ -48,4 +48,10 @@ function compose (middleware) {
}
}
}
ret.onUsed = function (useCtx) {
if (useCtx.prepareMiddleware) {
middleware = middleware.map((mw) => useCtx.prepareMiddleware(mw, 'compose'))
}
}
return ret
}
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,28 @@ describe('Koa Compose', function () {
val.should.equal(1)
})
})

it('should accept any onUsed object', function () {
var composed = compose([(ctx, next) => next()])
composed.onUsed({})
return composed({})
})

it('should prepare the middleware', function () {
var mw = (ctx) => { ctx.body = 'body' }
var composed = compose([mw])
var prepared
var useContext = {
app: {},
useChain: [],
prepareMiddleware: function (mw, name) {
prepared = mw
assert.equal(name, 'compose')
}
}
composed.onUsed(useContext)
return composed({}, () => {}).then(function () {
assert.equal(prepared, mw)
})
})
})

0 comments on commit a8466f2

Please sign in to comment.