mirror of
https://github.com/Jetsparrow/karmabot.git
synced 2026-01-21 00:56:09 +03:00
36 lines
1.0 KiB
C#
36 lines
1.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace JetBotLib
|
|
{
|
|
public interface IRequestHandler
|
|
{
|
|
Task Handle(RequestContext ctx, Func<RequestContext, Task> next);
|
|
}
|
|
public class RequestChain : IRequestHandler
|
|
{
|
|
List<IRequestHandler> handlerStack = new List<IRequestHandler>();
|
|
public async Task Handle(RequestContext ctx, Func<RequestContext, Task> next = null)
|
|
{
|
|
int i = 0;
|
|
Func<RequestContext, Task> 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);
|
|
}
|
|
}
|
|
} |