platform/packages/route/lib/src/middleware_pipeline.dart

39 lines
1.1 KiB
Dart
Raw Normal View History

2016-11-25 23:22:33 +00:00
import 'router.dart';
2016-12-19 04:43:55 +00:00
/// A chain of arbitrary handlers obtained by routing a path.
2018-08-20 18:59:51 +00:00
class MiddlewarePipeline<T> {
2016-12-19 04:43:55 +00:00
/// All the possible routes that matched the given path.
2018-08-20 18:59:51 +00:00
final Iterable<RoutingResult<T>> routingResults;
2021-03-18 00:11:45 +00:00
List<T>? _handlers;
2016-11-25 23:22:33 +00:00
2016-12-19 04:43:55 +00:00
/// An ordered list of every handler delegated to handle this request.
2021-03-18 00:11:45 +00:00
List<T>? get handlers {
2017-11-25 00:26:06 +00:00
if (_handlers != null) return _handlers;
2018-08-20 18:59:51 +00:00
final handlers = <T>[];
2016-11-25 23:22:33 +00:00
2018-08-20 18:59:51 +00:00
for (var result in routingResults) {
2016-11-25 23:22:33 +00:00
handlers.addAll(result.allHandlers);
}
2017-11-25 00:26:06 +00:00
return _handlers = handlers;
2016-11-25 23:22:33 +00:00
}
2018-08-20 19:34:00 +00:00
MiddlewarePipeline(Iterable<RoutingResult<T>> routingResults)
2018-06-23 02:54:20 +00:00
: this.routingResults = routingResults.toList();
2016-11-25 23:22:33 +00:00
}
2019-02-03 18:28:25 +00:00
/// Iterates through a [MiddlewarePipeline].
class MiddlewarePipelineIterator<T> extends Iterator<RoutingResult<T>> {
final MiddlewarePipeline<T> pipeline;
final Iterator<RoutingResult<T>> _inner;
MiddlewarePipelineIterator(this.pipeline)
: _inner = pipeline.routingResults.iterator;
@override
RoutingResult<T> get current => _inner.current;
@override
bool moveNext() => _inner.moveNext();
}