Skip to content

Commit

Permalink
added valuetask version of catch()
Browse files Browse the repository at this point in the history
  • Loading branch information
icanhasjonas committed Nov 6, 2017
1 parent f66d7ba commit be81481
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
13 changes: 13 additions & 0 deletions Then/CatchTaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ public static async Task<T> Catch<T, TException>( this Task<T> task, Func<TExcep
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async ValueTask<T> Catch<T, TException>(this ValueTask<T> task, Func<TException, ValueTask<T>> exceptionHandler) where TException : Exception
{
try
{
return task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait(false);
}
catch (TException e) {
var exceptionTask = exceptionHandler( e );
return exceptionTask.IsCompletedSuccessfully ? exceptionTask.Result : await exceptionTask.ConfigureAwait( false );
}
}

[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static Task<T> Catch<T>( this Task<T> task, Func<Exception, T> exceptionHandler ) => task.Catch<T, Exception>( exceptionHandler );

Expand Down
2 changes: 1 addition & 1 deletion Then/Then.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<RepositoryType>git</RepositoryType>
<PackageProjectUrl>https://github.com/bmbsqd/then</PackageProjectUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.3</Version>
<Version>1.0.4</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
26 changes: 13 additions & 13 deletions Then/ThenTaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public static async Task<TResult> Then<T, TResult>( this Task<T> task, Func<T, F
return selector( result ).Invoke();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async ValueTask<TResult> Then<T, TResult>(this ValueTask<T> task, Func<T, Func<TResult>> selector)
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static async ValueTask<TResult> Then<T, TResult>( this ValueTask<T> task, Func<T, Func<TResult>> selector )
{
var result = task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait( false );
return selector(result).Invoke();
return selector( result ).Invoke();
}

[MethodImpl( MethodImplOptions.AggressiveInlining )]
Expand All @@ -24,10 +24,10 @@ public static async Task<TResult> Then<T, TResult>( this Task<T> task, Func<T, F
return await selector( result ).Invoke().ConfigureAwait( false );
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async ValueTask<TResult> Then<T, TResult>(this ValueTask<T> task, Func<T, Func<ValueTask<TResult>>> selector)
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static async ValueTask<TResult> Then<T, TResult>( this ValueTask<T> task, Func<T, Func<ValueTask<TResult>>> selector )
{
var result = task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait(false);
var result = task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait( false );

var selectorTask = selector( result ).Invoke();
return selectorTask.IsCompletedSuccessfully ? selectorTask.Result : await selectorTask.ConfigureAwait( false );
Expand All @@ -40,11 +40,11 @@ public static async Task<TResult> Then<T, TResult>( this Task<T> task, Func<T, T
return then( result );
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async ValueTask<TResult> Then<T, TResult>(this ValueTask<T> task, Func<T, TResult> then)
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static async ValueTask<TResult> Then<T, TResult>( this ValueTask<T> task, Func<T, TResult> then )
{
var result = task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait(false);
return then(result);
var result = task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait( false );
return then( result );
}

[MethodImpl( MethodImplOptions.AggressiveInlining )]
Expand All @@ -54,10 +54,10 @@ public static async Task<TResult> Then<T, TResult>( this Task<T> task, Func<T, T
return await then( result ).ConfigureAwait( false );
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async ValueTask<TResult> Then<T, TResult>(this ValueTask<T> task, Func<T, ValueTask<TResult>> then)
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static async ValueTask<TResult> Then<T, TResult>( this ValueTask<T> task, Func<T, ValueTask<TResult>> then )
{
var result = task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait(false);
var result = task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait( false );
var thenTask = then( result );
return thenTask.IsCompletedSuccessfully ? thenTask.Result : await thenTask.ConfigureAwait( false );
}
Expand Down

0 comments on commit be81481

Please sign in to comment.