Skip to content

Commit

Permalink
Reworked middleware executor
Browse files Browse the repository at this point in the history
  • Loading branch information
Branko Vukelic committed Sep 26, 2012
1 parent b1733ea commit bcbc6a7
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions frag.js
Expand Up @@ -179,26 +179,28 @@
}

/**
* ## executeMiddlewares(request, final, index)
* ## executeMiddlewares(request, [funcs], final)
*
* Executes middlewares one by one.
*
* @param {Object} request Request object
* @param {Array} funcs Optional array of middlewares to call, defaults to
* global middlewares array
* @param {Function} final Final callback
* @param {Integer} index Index of the current middleware
* @private
*/
function executeMiddlewares(request, final, index) {
index = index || 0;
function executeMiddlewares(request, funcs, final) {
if (typeof funcs === 'function') {
final = funcs;
funcs = middlewares;
}

// No more middlewares?
if (!middlewares[index]) { return final(); }
if (!funcs.length) { return final(); }

// Call next middleware
middlewares[index].call(request, function(err) {
funcs[0].call(request, function(err) {
// TODO: Implement support for error handlers
if (err) { throw err; }
executeMiddlewares(request, final, index + 1);
executeMiddlewares(request, funcs.slice(1), final);
});
}

Expand Down

0 comments on commit bcbc6a7

Please sign in to comment.