Last active
January 22, 2018 12:48
-
-
Save oguzhane/911ab85a86d27bf1043d84f06f0e3756 to your computer and use it in GitHub Desktop.
deadly simple, robust pipeline in c#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Pipeline<T> | |
{ | |
private IList<Func<Func<T, Task>, Func<T, Task>>> _middleware; | |
public Pipeline() | |
{ | |
_middleware = new List<Func<Func<T, Task>, Func<T, Task>>>(); | |
} | |
public Pipeline<T> Use(Func<Func<T, Task>, Func<T, Task>> middleware) | |
{ | |
_middleware.Add(middleware); | |
return this; | |
} | |
public async Task SinkAsync(Func<T, Task> finalize, T initial) | |
{ | |
Func<T, Task> next = finalize; | |
foreach (var middleware in _middleware.Reverse()) | |
{ | |
next = middleware(next); | |
} | |
await next(initial); | |
} | |
public void Sink(Func<T, Task> finalize, T initial) | |
{ | |
Func<T, Task> next = finalize; | |
foreach (var middleware in _middleware.Reverse()) | |
{ | |
next = middleware(next); | |
} | |
next(initial); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment