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 92d70e3
Show file tree
Hide file tree
Showing 2 changed files with 28 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))
}
}
return ret
}
21 changes: 21 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,25 @@ 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 (toPrepare) {
assert.equal(toPrepare, mw)
}
}
composed.onUsed(useContext)
return composed({}, () => {})
})
})

0 comments on commit 92d70e3

Please sign in to comment.