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

96 lines
2.3 KiB
Dart
Raw Normal View History

2016-11-25 23:22:33 +00:00
part of angel_route.src.router;
2016-12-19 04:43:55 +00:00
/// Represents a complex result of navigating to a path.
2018-08-20 18:59:51 +00:00
class RoutingResult<T> {
2017-11-27 02:21:19 +00:00
/// The parse result that matched the given sub-path.
2021-03-18 00:11:45 +00:00
final ParseResult<RouteResult?>? parseResult;
2016-12-19 04:43:55 +00:00
/// A nested instance, if a sub-path was matched.
2021-03-18 00:11:45 +00:00
final Iterable<RoutingResult<T>>? nested;
2016-12-19 04:43:55 +00:00
/// All route params matching this route on the current sub-path.
2021-03-18 00:11:45 +00:00
final Map<String?, dynamic> params = {};
2016-12-19 04:43:55 +00:00
/// The [Route] that answered this sub-path.
///
/// This is mostly for internal use, and useless in production.
2021-03-18 00:11:45 +00:00
final Route<T>? shallowRoute;
2016-12-19 04:43:55 +00:00
/// The [Router] that answered this sub-path.
///
/// Only really for internal use.
2021-03-18 00:11:45 +00:00
final Router<T>? shallowRouter;
2016-12-19 04:43:55 +00:00
/// The remainder of the full path that was not matched, and was passed to [nested] routes.
2016-11-25 23:22:33 +00:00
final String tail;
2016-12-19 04:43:55 +00:00
/// The [RoutingResult] that matched the most specific sub-path.
2018-08-20 18:59:51 +00:00
RoutingResult<T> get deepest {
2021-03-21 00:02:50 +00:00
var search = this;
2016-11-25 23:22:33 +00:00
2021-03-21 00:02:50 +00:00
while (search.nested?.isNotEmpty == true) {
2021-03-18 00:11:45 +00:00
search = search.nested!.first;
2019-11-28 17:40:32 +00:00
}
2016-11-25 23:22:33 +00:00
return search;
}
2016-12-19 04:43:55 +00:00
/// The most specific route.
2021-03-18 00:11:45 +00:00
Route<T>? get route => deepest.shallowRoute;
2016-12-19 04:43:55 +00:00
/// The most specific router.
2021-03-18 00:11:45 +00:00
Router<T>? get router => deepest.shallowRouter;
2016-11-25 23:22:33 +00:00
2016-12-19 04:43:55 +00:00
/// The handlers at this sub-path.
2018-08-20 18:59:51 +00:00
List<T> get handlers {
2021-03-21 00:02:50 +00:00
return <T>[...shallowRouter!.middleware, ...shallowRoute!.handlers!];
2016-11-25 23:22:33 +00:00
}
2016-12-19 04:43:55 +00:00
/// All handlers on this sub-path and its children.
2018-08-20 18:59:51 +00:00
List<T> get allHandlers {
final handlers = <T>[];
2016-11-25 23:22:33 +00:00
2018-08-20 18:59:51 +00:00
void crawl(RoutingResult<T> result) {
2017-11-25 00:26:06 +00:00
handlers.addAll(result.handlers);
if (result.nested?.isNotEmpty == true) {
2021-03-18 00:11:45 +00:00
for (var r in result.nested!) {
2019-11-28 17:40:32 +00:00
crawl(r);
}
2017-11-25 00:26:06 +00:00
}
2016-11-25 23:22:33 +00:00
}
2017-11-25 00:26:06 +00:00
crawl(this);
2016-11-25 23:22:33 +00:00
return handlers;
}
2016-12-19 04:43:55 +00:00
/// All parameters on this sub-path and its children.
2021-03-18 00:11:45 +00:00
Map<String?, dynamic> get allParams {
2021-03-21 00:02:50 +00:00
final params = <String?, dynamic>{};
2016-11-27 23:39:03 +00:00
2017-11-25 00:26:06 +00:00
void crawl(RoutingResult result) {
params.addAll(result.params);
if (result.nested?.isNotEmpty == true) {
2021-03-18 00:11:45 +00:00
for (var r in result.nested!) {
2019-11-28 17:40:32 +00:00
crawl(r);
}
2017-11-25 00:26:06 +00:00
}
2016-11-27 23:39:03 +00:00
}
2017-11-25 00:26:06 +00:00
crawl(this);
2016-11-27 23:39:03 +00:00
return params;
}
2016-11-25 23:22:33 +00:00
RoutingResult(
2017-11-27 02:21:19 +00:00
{this.parseResult,
2021-03-18 00:11:45 +00:00
Map<String?, dynamic> params = const {},
2016-11-25 23:22:33 +00:00
this.nested,
2016-11-27 22:24:30 +00:00
this.shallowRoute,
this.shallowRouter,
2021-03-18 00:11:45 +00:00
required this.tail}) {
2021-03-21 00:02:50 +00:00
this.params.addAll(params);
2016-11-25 23:22:33 +00:00
}
}