runMiddleware method

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

Implementation

Future<RouteDecoder?> runMiddleware(RouteDecoder config) async {
  final middlewares = config.currentTreeBranch.last.middlewares;
  if (middlewares.isEmpty) {
    return config;
  }
  var iterator = config;
  for (var item in middlewares) {
    var redirectRes = await item.redirectDelegate(iterator);

    if (redirectRes == null) {
      config.route?.completer?.complete();
      return null;
    }
    if (config != redirectRes) {
      config.route?.completer?.complete();
      Get.log('Redirect to ${redirectRes.pageSettings?.name}');
    }

    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 await runMiddleware(iterator);
  }
  return iterator;
}