-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResultChain.cs
44 lines (39 loc) · 1.66 KB
/
ResultChain.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
namespace FlowControl.NET;
public class ResultChain<T, E>(Task<ResultOrError<T, E>> initial) where E : Error?
{
private Task<ResultOrError<List<object>, E>> results = initial.ContinueWith(t =>
t.Result.MatchFuncAsync(
success => ResultOrError<List<object>, E>.Success([success]),
error => ResultOrError<List<object>, E>.Error(error)
)
).Unwrap();
public ResultChain<T, E> Then<U>(Func<Task<ResultOrError<U, E>>> next)
{
results = results.ContinueWith(t =>
t.Result.MatchFuncAsync(async list =>
{
var nextResult = await next();
return await nextResult.MatchFuncAsync(
success =>
{
list.Add(success!);
return ResultOrError<List<object>, E>.Success(list);
},
error => ResultOrError<List<object>, E>.Error(error)
);
},
error => Task.FromResult(ResultOrError<List<object>, E>.Error(error))
)
).Unwrap();
return this;
}
public Task<U> FinallyAsync<U>(Func<List<object>, Task<U>> success, Func<E, Task<U>> failure) =>
results.ContinueWith(t => t.Result.MatchFuncAsync(success, failure)).Unwrap();
public U Finally<U>(Func<List<object>, U> success, Func<E, U> failure) =>
results.ContinueWith(t => t.Result.MatchFuncAsync(success, failure)).Unwrap().Result;
}
public static class ResultOrErrorExtensions
{
public static ResultChain<T, E> AsChain<T, E>(this Task<ResultOrError<T, E>> result)
where E : Error? => new(result);
}