forked from CommunityToolkit/Maui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Touch effect maui (CommunityToolkit#1326)
* Fixed some issues in Android implementation * Added iOS implementation for TouchBehavior * Reformatted code * Removed duplicated extension methods --------- Co-authored-by: Christian Rendl <cr@mutor.at>
- Loading branch information
Showing
4 changed files
with
536 additions
and
100 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/Touch/SafeFireAndForgotExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
namespace CommunityToolkit.Maui.Behaviors; | ||
|
||
internal static class SafeFireAndForgetExtensions | ||
{ | ||
#region Private Methods | ||
|
||
private static async void HandleSafeFireAndForget<TException>(ValueTask valueTask, bool continueOnCapturedContext, Action<TException>? onException) where TException : Exception | ||
{ | ||
try | ||
{ | ||
await valueTask.ConfigureAwait(continueOnCapturedContext); | ||
} | ||
catch (TException ex) when (onException != null) | ||
{ | ||
onException(ex); | ||
} | ||
} | ||
|
||
private static async void HandleSafeFireAndForget<TException>(Task task, bool continueOnCapturedContext, Action<TException>? onException) where TException : Exception | ||
{ | ||
try | ||
{ | ||
await task.ConfigureAwait(continueOnCapturedContext); | ||
} | ||
catch (TException ex) when (onException != null) | ||
{ | ||
onException(ex); | ||
} | ||
} | ||
|
||
#endregion | ||
|
||
/// <summary> | ||
/// Safely execute the ValueTask without waiting for it to complete before moving to the next line of code; commonly | ||
/// known as "Fire And Forget". Inspired by John Thiriet's blog post, "Removing Async Void": | ||
/// https://johnthiriet.com/removing-async-void/. | ||
/// </summary> | ||
/// <param name="task">ValueTask.</param> | ||
/// <param name="onException"> | ||
/// If an exception is thrown in the ValueTask, <c>onException</c> will execute. If onException | ||
/// is null, the exception will be re-thrown | ||
/// </param> | ||
/// <param name="continueOnCapturedContext"> | ||
/// If set to <c>true</c>, continue on captured context; this will ensure that the | ||
/// Synchronization Context returns to the calling thread. If set to <c>false</c>, continue on a different context; | ||
/// this will allow the Synchronization Context to continue on a different thread | ||
/// </param> | ||
internal static void SafeFireAndForget(this ValueTask task, in Action<Exception>? onException = null, in bool continueOnCapturedContext = false) | ||
{ | ||
HandleSafeFireAndForget(task, continueOnCapturedContext, onException); | ||
} | ||
|
||
/// <summary> | ||
/// Safely execute the ValueTask without waiting for it to complete before moving to the next line of code; commonly | ||
/// known as "Fire And Forget". Inspired by John Thiriet's blog post, "Removing Async Void": | ||
/// https://johnthiriet.com/removing-async-void/. | ||
/// </summary> | ||
/// <param name="task">ValueTask.</param> | ||
/// <param name="onException"> | ||
/// If an exception is thrown in the Task, <c>onException</c> will execute. If onException is | ||
/// null, the exception will be re-thrown | ||
/// </param> | ||
/// <param name="continueOnCapturedContext"> | ||
/// If set to <c>true</c>, continue on captured context; this will ensure that the | ||
/// Synchronization Context returns to the calling thread. If set to <c>false</c>, continue on a different context; | ||
/// this will allow the Synchronization Context to continue on a different thread | ||
/// </param> | ||
/// <typeparam name="TException">Exception type. If an exception is thrown of a different type, it will not be handled</typeparam> | ||
internal static void SafeFireAndForget<TException>(this ValueTask task, in Action<TException>? onException = null, in bool continueOnCapturedContext = false) where TException : Exception | ||
{ | ||
HandleSafeFireAndForget(task, continueOnCapturedContext, onException); | ||
} | ||
|
||
/// <summary> | ||
/// Safely execute the Task without waiting for it to complete before moving to the next line of code; commonly known | ||
/// as "Fire And Forget". Inspired by John Thiriet's blog post, "Removing Async Void": | ||
/// https://johnthiriet.com/removing-async-void/. | ||
/// </summary> | ||
/// <param name="task">Task.</param> | ||
/// <param name="onException"> | ||
/// If an exception is thrown in the Task, <c>onException</c> will execute. If onException is | ||
/// null, the exception will be re-thrown | ||
/// </param> | ||
/// <param name="continueOnCapturedContext"> | ||
/// If set to <c>true</c>, continue on captured context; this will ensure that the | ||
/// Synchronization Context returns to the calling thread. If set to <c>false</c>, continue on a different context; | ||
/// this will allow the Synchronization Context to continue on a different thread | ||
/// </param> | ||
internal static void SafeFireAndForget(this Task task, in Action<Exception>? onException = null, in bool continueOnCapturedContext = false) | ||
{ | ||
HandleSafeFireAndForget(task, continueOnCapturedContext, onException); | ||
} | ||
|
||
/// <summary> | ||
/// Safely execute the Task without waiting for it to complete before moving to the next line of code; commonly known | ||
/// as "Fire And Forget". Inspired by John Thiriet's blog post, "Removing Async Void": | ||
/// https://johnthiriet.com/removing-async-void/. | ||
/// </summary> | ||
/// <param name="task">Task.</param> | ||
/// <param name="onException"> | ||
/// If an exception is thrown in the Task, <c>onException</c> will execute. If onException is | ||
/// null, the exception will be re-thrown | ||
/// </param> | ||
/// <param name="continueOnCapturedContext"> | ||
/// If set to <c>true</c>, continue on captured context; this will ensure that the | ||
/// Synchronization Context returns to the calling thread. If set to <c>false</c>, continue on a different context; | ||
/// this will allow the Synchronization Context to continue on a different thread | ||
/// </param> | ||
/// <typeparam name="TException">Exception type. If an exception is thrown of a different type, it will not be handled</typeparam> | ||
internal static void SafeFireAndForget<TException>(this Task task, in Action<TException>? onException = null, in bool continueOnCapturedContext = false) where TException : Exception | ||
{ | ||
HandleSafeFireAndForget(task, continueOnCapturedContext, onException); | ||
} | ||
} |
Oops, something went wrong.