runMiddleware method

Future<RouteDecoder?> runMiddleware(
  1. RouteDecoder config
)

Implementation

Future<RouteDecoder?> runMiddleware(RouteDecoder config) async {
  final List<GetMiddleware> middlewares =
      config.currentTreeBranch.last.middlewares;
  if (middlewares.isEmpty) {
    return config;
  }
  RouteDecoder iterator = config;
  for (final GetMiddleware item in middlewares) {
    final RouteDecoder? redirectRes = await item.redirectDelegate(iterator);
    if (redirectRes == null) {
      return null;
    }
    iterator = redirectRes;
    // Stop the iteration over the middleware if we changed page
    // and that redirectRes is not the same as the current config.
    if (config != redirectRes) {
      break;
    }
  }
  // If the target is not the same as the source, we need
  // to run the middlewares for the new route.
  if (iterator != config) {
    return runMiddleware(iterator);
  }
  return iterator;
}