using System; using System.Collections.Generic; using System.Threading.Tasks; namespace JetKarmaBot.Services.Handling { public interface IRequestHandler { Task Handle(RequestContext ctx, Func next); } public class RequestChain : IRequestHandler { List handlerStack = new List(); public async Task Handle(RequestContext ctx, Func next = null) { int i = 0; Func chainNext = null; chainNext = (newCtx) => { if (i == handlerStack.Count) return Task.CompletedTask; IRequestHandler handler = handlerStack[i++]; return handler.Handle(newCtx, chainNext); }; await chainNext(ctx); if (next != null) await next(ctx); } public void Add(IRequestHandler handler) { handlerStack.Add(handler); } } }