Exemplo n.º 1
0
 public static Task <ResultGeneric <TResult, TError> > ThenAsync <TValue, TError, TResult>(this ResultGeneric <TValue, TError> result, Func <TValue, Task <TResult> > func)
 {
     return(result.IsError
         ? Task.FromResult(ResultGeneric <TResult, TError> .FromError(result.Error))
         : func(result.Value).ContinueWith(task => ResultGeneric <TResult, TError> .FromValue(task.Result), CancellationToken.None,
                                           TaskContinuationOptions.DenyChildAttach | TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default));
 }
Exemplo n.º 2
0
 public static Task <ResultGeneric <TValue, TError> > Where <TValue, TError>(this Task <ResultGeneric <TValue, TError> > resultTask, Func <TValue, bool> predicate,
                                                                             Func <TValue, TError> errorValueFunc, TaskScheduler scheduler = null)
 {
     return(resultTask.ContinueWith((task, f) =>
     {
         var result = task.Result;
         return result.IsError ? ResultGeneric <TValue, TError> .FromError(result.Error) : result.Where((Func <TValue, bool>)f, errorValueFunc);
     }, predicate, CancellationToken.None, TaskContinuationOptions.DenyChildAttach, scheduler ?? TaskScheduler.Default));
 }
Exemplo n.º 3
0
 public static Task <ResultGeneric <TResult, TError> > ThenAsync <TValue, TError, TResult>(this Task <ResultGeneric <TValue, TError> > resultTask,
                                                                                           Func <TValue, Task <ResultGeneric <TResult, TError> > > func, TaskScheduler scheduler = null)
 {
     return(resultTask.ContinueWith((task, f) =>
     {
         return task.Result.IsError
                 ? Task.FromResult(ResultGeneric <TResult, TError> .FromError(task.Result.Error))
                 : ((Func <TValue, Task <ResultGeneric <TResult, TError> > >)f)(task.Result.Value)
         .ContinueWith(t => t.Result, TaskContinuationOptions.ExecuteSynchronously);
     }, func,
                                    CancellationToken.None, TaskContinuationOptions.DenyChildAttach,
                                    scheduler ?? TaskScheduler.Default).Unwrap());
 }
Exemplo n.º 4
0
        public static Task <ResultGeneric <TResult, TError> > ThenTryAsync <TValue, TError, TResult>(this ResultGeneric <TValue, TError> result,
                                                                                                     Func <TValue, Task <ResultGeneric <TResult, TError> > > func,
                                                                                                     Func <Exception, TError> catchFunc)
        {
            if (result.IsError)
            {
                return(Task.FromResult(ResultGeneric <TResult, TError> .FromError(result.Error)));
            }

            try
            {
                return(func(result.Value));
            }
            catch (Exception e)
            {
                return(Task.FromResult(ResultGeneric <TResult, TError> .FromError(catchFunc(e))));
            }
        }
Exemplo n.º 5
0
        public static Task <ResultGeneric <TResult, TError> > ThenTryAsync <TValue, TError, TResult>(this ResultGeneric <TValue, TError> result, Func <TValue, Task <TResult> > func,
                                                                                                     Func <Exception, TError> catchFunc)
        {
            if (result.IsError)
            {
                return(Task.FromResult(ResultGeneric <TResult, TError> .FromError(result.Error)));
            }

            try
            {
                return(func(result.Value).ContinueWith(task => task.IsFaulted
                        ? ResultGeneric <TResult, TError> .FromError(catchFunc(task.Exception.TryFlattenAggregateException()))
                        : ResultGeneric <TResult, TError> .FromValue(task.Result), CancellationToken.None,
                                                       TaskContinuationOptions.DenyChildAttach | TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default));
            }
            catch (Exception e)
            {
                return(Task.FromResult(ResultGeneric <TResult, TError> .FromError(catchFunc(e))));
            }
        }
Exemplo n.º 6
0
        public static Task <ResultGeneric <TResult, TError> > ThenTryAsync <TValue, TError, TResult>(this Task <ResultGeneric <TValue, TError> > resultTask,
                                                                                                     Func <TValue, Task <TResult> > func, Func <Exception, TError> catchFunc, TaskScheduler scheduler = null)
        {
            return(resultTask.ContinueWith((task, f) =>
            {
                if (task.Result.IsError)
                {
                    return Task.FromResult(ResultGeneric <TResult, TError> .FromError(task.Result.Error));
                }

                try
                {
                    return ((Func <TValue, Task <TResult> >)f)(task.Result.Value)
                    .ContinueWith(t => ResultGeneric <TResult, TError> .FromValue(t.Result), TaskContinuationOptions.ExecuteSynchronously);
                }
                catch (Exception e)
                {
                    return Task.FromResult(ResultGeneric <TResult, TError> .FromError(catchFunc(e)));
                }
            }, func, CancellationToken.None, TaskContinuationOptions.DenyChildAttach,
                                           scheduler ?? TaskScheduler.Default).Unwrap());
        }
Exemplo n.º 7
0
 public static Task <ResultGeneric <TResult, TError> > ThenAsync <TValue, TError, TResult>(this ResultGeneric <TValue, TError> result, Func <TValue, Task <ResultGeneric <TResult, TError> > > func)
 {
     return(result.IsError
         ? Task.FromResult(ResultGeneric <TResult, TError> .FromError(result.Error))
         : func(result.Value));
 }