OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library shelf.pipeline; |
| 6 |
| 7 import 'handler.dart'; |
| 8 import 'middleware.dart'; |
| 9 |
| 10 /// A helper that makes it easy to compose a set of [Middleware] and a |
| 11 /// [Handler]. |
| 12 /// |
| 13 /// var handler = const Pipeline() |
| 14 /// .addMiddleware(loggingMiddleware) |
| 15 /// .addMiddleware(cachingMiddleware) |
| 16 /// .addHandler(application); |
| 17 class Pipeline { |
| 18 final Pipeline _parent; |
| 19 final Middleware _middleware; |
| 20 |
| 21 const Pipeline() |
| 22 : _middleware = null, |
| 23 _parent = null; |
| 24 |
| 25 Pipeline._(this._middleware, this._parent); |
| 26 |
| 27 /// Returns a new [Pipeline] with [middleware] added to the existing set of |
| 28 /// [Middleware]. |
| 29 /// |
| 30 /// [middleware] will be the last [Middleware] to process a request and |
| 31 /// the first to process a response. |
| 32 Pipeline addMiddleware(Middleware middleware) => |
| 33 new Pipeline._(middleware, this); |
| 34 |
| 35 /// Returns a new [Handler] with [handler] as the final processor of a |
| 36 /// [Request] if all of the middleware in the pipeline have passed the request |
| 37 /// through. |
| 38 Handler addHandler(Handler handler) { |
| 39 if (_middleware == null) return handler; |
| 40 return _parent.addHandler(_middleware(handler)); |
| 41 } |
| 42 |
| 43 /// Exposes this pipeline of [Middleware] as a single middleware instance. |
| 44 Middleware get middleware => addHandler; |
| 45 } |
OLD | NEW |