executeHandlers function

Future<ReqRes> executeHandlers(
  1. Iterable handlers,
  2. ReqRes reqRes
)

Implementation

Future<ReqRes> executeHandlers(
  Iterable<dynamic> handlers,
  ReqRes reqRes,
) async {
  var result = reqRes;
  final iterator = handlers.iterator;

  Future<void> handleChain([dynamic nr_, Middleware? mdw]) async {
    result = result.merge(nr_);
    if (mdw == null || result.res.ended) return;

    return await mdw.call(
      result.req,
      result.res,
      ([nr_, chain]) => handleChain(nr_, chain),
    );
  }

  while (iterator.moveNext()) {
    await handleChain(null, iterator.current);
    if (result.res.ended) break;
  }

  return result;
}