diff --git a/Then/AssertTaskExtensions.cs b/Then/AssertTaskExtensions.cs index 8294b39..b839c0d 100644 --- a/Then/AssertTaskExtensions.cs +++ b/Then/AssertTaskExtensions.cs @@ -12,6 +12,17 @@ public static async Task Assert( this Task task, Func predicat return result; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask Assert(this ValueTask task, Func predicate, Action 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 Assert( this Task task, Func predicate, Func reaction ) where TException : Exception { @@ -21,5 +32,16 @@ public static async Task Assert( this Task task, Func Assert(this ValueTask task, Func predicate, Func reaction) where TException : Exception + { + var result = task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait( false ); + if (!predicate(result)) + { + throw reaction(result); + } + return result; + } } } \ No newline at end of file diff --git a/Then/CatchTaskExtensions.cs b/Then/CatchTaskExtensions.cs index 467f159..a9c6749 100644 --- a/Then/CatchTaskExtensions.cs +++ b/Then/CatchTaskExtensions.cs @@ -38,6 +38,18 @@ public static async Task Catch( this Task task, Func Catch(this ValueTask task, Func 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 exceptionHandler ) => task.Catch( exceptionHandler ); diff --git a/Then/TapTaskExtensions.cs b/Then/TapTaskExtensions.cs index e34126b..9a02dc8 100644 --- a/Then/TapTaskExtensions.cs +++ b/Then/TapTaskExtensions.cs @@ -10,6 +10,14 @@ public static async Task Tap( this Task task, Func then ) return result; } + [MethodImpl( MethodImplOptions.AggressiveInlining )] + public static async ValueTask Tap( this ValueTask task, Func then ) + { + var result = task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait( false ); + await then().ConfigureAwait( false ); + return result; + } + [MethodImpl( MethodImplOptions.AggressiveInlining )] public static async Task Tap( this Task task, Action then ) { @@ -18,12 +26,28 @@ public static async Task Tap( this Task task, Action then ) return result; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask Tap(this ValueTask task, Action then) + { + var result = task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait(false); + then(result); + return result; + } + [MethodImpl( MethodImplOptions.AggressiveInlining )] public static async Task Tap( this Task task, Action then ) { var result = await task.ConfigureAwait( false ); then(); return result; + } + + [MethodImpl( MethodImplOptions.AggressiveInlining )] + public static async ValueTask Tap( this ValueTask task, Action then ) + { + var result = task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait( false ); + then(); + return result; } [MethodImpl( MethodImplOptions.AggressiveInlining )] @@ -40,5 +64,13 @@ public static async Task Tap( this Task task, Func then ) await then( result ).ConfigureAwait( false ); return result; } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async ValueTask Tap(this ValueTask task, Func then) + { + var result = task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait(false); + await then(result).ConfigureAwait(false); + return result; + } } } \ No newline at end of file diff --git a/Then/Then.csproj b/Then/Then.csproj index 1215c9e..67fcefb 100644 --- a/Then/Then.csproj +++ b/Then/Then.csproj @@ -19,4 +19,8 @@ 1.0.3 + + + + diff --git a/Then/ThenTaskExtensions.cs b/Then/ThenTaskExtensions.cs index 2e10f74..0453374 100644 --- a/Then/ThenTaskExtensions.cs +++ b/Then/ThenTaskExtensions.cs @@ -10,6 +10,13 @@ public static async Task Then( this Task task, Func Then(this ValueTask task, Func> selector) + { + var result = task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait( false ); + return selector(result).Invoke(); + } + [MethodImpl( MethodImplOptions.AggressiveInlining )] public static async Task Then( this Task task, Func>> selector ) { @@ -17,6 +24,15 @@ public static async Task Then( this Task task, Func Then(this ValueTask task, Func>> 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 Then( this Task task, Func then ) { @@ -24,6 +40,13 @@ public static async Task Then( this Task task, Func Then(this ValueTask task, Func then) + { + var result = task.IsCompletedSuccessfully ? task.Result : await task.ConfigureAwait(false); + return then(result); + } + [MethodImpl( MethodImplOptions.AggressiveInlining )] public static async Task Then( this Task task, Func> then ) { @@ -31,6 +54,14 @@ public static async Task Then( this Task task, Func Then(this ValueTask task, Func> 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( this Task task, Func then ) {