waterfall

This commit is contained in:
thosakwe 2017-01-13 19:36:38 -05:00
parent f00705d2d0
commit 466b05c347
3 changed files with 22 additions and 2 deletions

View file

@ -1,6 +1,6 @@
# angel_framework
[![pub 1.0.0-dev.42](https://img.shields.io/badge/pub-1.0.0--dev.42-red.svg)](https://pub.dartlang.org/packages/angel_framework)
[![pub 1.0.0-dev.43](https://img.shields.io/badge/pub-1.0.0--dev.43-red.svg)](https://pub.dartlang.org/packages/angel_framework)
[![build status](https://travis-ci.org/angel-dart/framework.svg)](https://travis-ci.org/angel-dart/framework)
Core libraries for the Angel Framework.

View file

@ -19,6 +19,26 @@ typedef Future<bool> RequestMiddleware(RequestContext req, ResponseContext res);
/// A function that receives an incoming [RequestContext] and responds to it.
typedef Future RequestHandler(RequestContext req, ResponseContext res);
/// Sequentially runs a list of [handlers] of middleware, and breaks if any does not
/// return `true`. Works well with [Router].chain.
RequestMiddleware waterfall(List handlers) {
for (var handler in handlers) {
if (handler is! RequestMiddleware && handler is! RequestHandler)
throw new ArgumentError(
'`waterfall` only accepts middleware and handlers. $handler is not a valid option.');
}
return (req, res) async {
for (var handler in handlers) {
var result = await handler(req, res);
if (result != true) return result;
}
return true;
};
}
/// A routable server that can handle dynamic requests.
class Routable extends Router {
final Map<Pattern, Controller> _controllers = {};

View file

@ -1,5 +1,5 @@
name: angel_framework
version: 1.0.0-dev.42
version: 1.0.0-dev.43
description: Core libraries for the Angel framework.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_framework