1
0
mirror of synced 2024-11-16 14:53:50 +00:00
Tapeti/Helpers/MiddlewareHelper.cs

58 lines
1.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Threading.Tasks;
namespace Tapeti.Helpers
{
public static class MiddlewareHelper
{
public static void Go<T>(IReadOnlyList<T> middleware, Action<T, Action> handle, Action lastHandler)
{
var handlerIndex = middleware.Count - 1;
if (handlerIndex == -1)
{
lastHandler();
return;
}
Action handleNext = null;
handleNext = () =>
{
handlerIndex--;
if (handlerIndex >= 0)
handle(middleware[handlerIndex], handleNext);
else
lastHandler();
};
handle(middleware[handlerIndex], handleNext);
}
public static async Task GoAsync<T>(IReadOnlyList<T> middleware, Func<T, Func<Task>, Task> handle, Func<Task> lastHandler)
{
var handlerIndex = middleware.Count - 1;
if (handlerIndex == -1)
{
await lastHandler();
return;
}
Func<Task> handleNext = null;
handleNext = async () =>
{
handlerIndex--;
if (handlerIndex >= 0)
await handle(middleware[handlerIndex], handleNext);
else
await lastHandler();
};
await handle(middleware[handlerIndex], handleNext);
}
}
}