Skip to content

Commit

Permalink
added support for valuetasks
Browse files Browse the repository at this point in the history
  • Loading branch information
icanhasjonas committed Nov 6, 2017
1 parent 95f5f5b commit f66d7ba
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Then/AssertTaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ public static async Task<T> Assert<T>( this Task<T> task, Func<T, bool> predicat
return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async ValueTask<T> Assert<T>(this ValueTask<T> task, Func<T, bool> predicate, Action<T> reaction)
{
var result = task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait( false );
if (!predicate(result))
{
reaction(result);
}
return result;
}

[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static async Task<T> Assert<T, TException>( this Task<T> task, Func<T, bool> predicate, Func<T, TException> reaction ) where TException : Exception
{
Expand All @@ -21,5 +32,16 @@ public static async Task<T> Assert<T, TException>( this Task<T> task, Func<T, bo
}
return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async ValueTask<T> Assert<T, TException>(this ValueTask<T> task, Func<T, bool> predicate, Func<T, TException> reaction) where TException : Exception
{
var result = task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait( false );
if (!predicate(result))
{
throw reaction(result);
}
return result;
}
}
}
12 changes: 12 additions & 0 deletions Then/CatchTaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ 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, T> exceptionHandler) where TException : Exception
{
try {
return task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait( false );
}
catch (TException e)
{
return exceptionHandler(e);
}
}

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

Expand Down
32 changes: 32 additions & 0 deletions Then/TapTaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ public static async Task<T> Tap<T>( this Task<T> task, Func<Task> then )
return result;
}

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

[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static async Task<T> Tap<T>( this Task<T> task, Action<T> then )
{
Expand All @@ -18,12 +26,28 @@ public static async Task<T> Tap<T>( this Task<T> task, Action<T> then )
return result;
}

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

[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static async Task<T> Tap<T>( this Task<T> task, Action then )
{
var result = await task.ConfigureAwait( false );
then();
return result;
}

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

[MethodImpl( MethodImplOptions.AggressiveInlining )]
Expand All @@ -40,5 +64,13 @@ public static async Task<T> Tap<T>( this Task<T> task, Func<T, Task> then )
await then( result ).ConfigureAwait( false );
return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async ValueTask<T> Tap<T>(this ValueTask<T> task, Func<T, Task> then)
{
var result = task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait(false);
await then(result).ConfigureAwait(false);
return result;
}
}
}
4 changes: 4 additions & 0 deletions Then/Then.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@
<Version>1.0.3</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.4.0" />
</ItemGroup>

</Project>
31 changes: 31 additions & 0 deletions Then/ThenTaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,58 @@ 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)
{
var result = task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait( false );
return selector(result).Invoke();
}

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

var selectorTask = selector( result ).Invoke();
return selectorTask.IsCompletedSuccessfully ? selectorTask.Result : await selectorTask.ConfigureAwait( false );
}

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

[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);
}

[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static async Task<TResult> Then<T, TResult>( this Task<T> task, Func<T, Task<TResult>> then )
{
var result = await task.ConfigureAwait( false );
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)
{
var result = task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait(false);
var thenTask = then( result );
return thenTask.IsCompletedSuccessfully ? thenTask.Result : await thenTask.ConfigureAwait( false );
}

[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static async Task Then<T>( this Task<T> task, Func<T, Task> then )
{
Expand Down

0 comments on commit f66d7ba

Please sign in to comment.