platform/lib/src/routing_result.dart

92 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.
2017-11-28 21:07:14 +00:00
final ParseResult<Map<String, dynamic>> parseResult;
2016-12-19 04:43:55 +00:00
/// A nested instance, if a sub-path was matched.
2018-08-20 18:59:51 +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.
2016-11-25 23:22:33 +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.
2018-08-20 18:59:51 +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.
2018-08-20 18:59:51 +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 {
2016-11-25 23:22:33 +00:00
var search = this;
2017-11-25 00:26:06 +00:00
while (search?.nested?.isNotEmpty == true) search = search.nested.first;
2016-11-25 23:22:33 +00:00
return search;
}
2016-12-19 04:43:55 +00:00
/// The most specific route.
2018-08-20 18:59:51 +00:00
Route<T> get route => deepest.shallowRoute;
2016-12-19 04:43:55 +00:00
/// The most specific router.
2018-08-20 18:59:51 +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 {
return <T>[]..addAll(shallowRouter.middleware)..addAll(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) {
for (var r in result.nested)
crawl(r);
}
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.
2016-11-27 23:39:03 +00:00
Map<String, dynamic> get allParams {
2016-12-21 23:28:00 +00:00
final Map<String, dynamic> params = {};
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) {
for (var r in result.nested)
crawl(r);
}
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,
2016-11-25 23:22:33 +00:00
Map<String, dynamic> params: const {},
this.nested,
2016-11-27 22:24:30 +00:00
this.shallowRoute,
this.shallowRouter,
2016-11-25 23:22:33 +00:00
this.tail}) {
this.params.addAll(params ?? {});
}
}