Files
Toolkit2/Toolkit.Foundation/NotificationHandlerWrapper.cs
T
TheXamlGuy bc55c4649b tidy
2024-04-26 23:05:36 +01:00

24 lines
892 B
C#

namespace Toolkit.Foundation;
public class NotificationHandlerWrapper<TMessage>(INotificationHandler<TMessage> handler,
IEnumerable<IPipelineBehaviour<TMessage>> pipelineBehaviours)
{
private readonly IEnumerable<IPipelineBehaviour<TMessage>> pipelineBehaviours =
pipelineBehaviours.Reverse();
public async Task Handle(TMessage message,
CancellationToken cancellationToken)
{
NotificationHandlerDelegate<TMessage> currentHandler = handler.Handle;
foreach (IPipelineBehaviour<TMessage> behaviour in pipelineBehaviours)
{
NotificationHandlerDelegate<TMessage> previousHandler = currentHandler;
currentHandler = async (args, token) =>
{
await behaviour.Handle(args, previousHandler, token);
};
}
await currentHandler(message, cancellationToken);
}
}