2016-12-11 14:08:58 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2017-01-31 11:01:08 +00:00
|
|
|
|
using System.Diagnostics.Eventing.Reader;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-12-11 14:08:58 +00:00
|
|
|
|
|
|
|
|
|
namespace Tapeti.Helpers
|
|
|
|
|
{
|
|
|
|
|
public static class MiddlewareHelper
|
|
|
|
|
{
|
2017-01-31 11:01:08 +00:00
|
|
|
|
public static void Go<T>(IReadOnlyList<T> middleware, Action<T, Action> handle, Action lastHandler)
|
2016-12-11 14:08:58 +00:00
|
|
|
|
{
|
|
|
|
|
var handlerIndex = middleware.Count - 1;
|
|
|
|
|
if (handlerIndex == -1)
|
2017-01-31 11:01:08 +00:00
|
|
|
|
{
|
|
|
|
|
lastHandler();
|
2016-12-11 14:08:58 +00:00
|
|
|
|
return;
|
2017-01-31 11:01:08 +00:00
|
|
|
|
}
|
2016-12-11 14:08:58 +00:00
|
|
|
|
|
|
|
|
|
Action handleNext = null;
|
|
|
|
|
|
|
|
|
|
handleNext = () =>
|
|
|
|
|
{
|
|
|
|
|
handlerIndex--;
|
|
|
|
|
if (handlerIndex >= 0)
|
|
|
|
|
handle(middleware[handlerIndex], handleNext);
|
2017-01-31 11:01:08 +00:00
|
|
|
|
else
|
|
|
|
|
lastHandler();
|
2016-12-11 14:08:58 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handle(middleware[handlerIndex], handleNext);
|
|
|
|
|
}
|
2017-01-31 11:01:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2016-12-11 14:08:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|