using System.Threading.Tasks;
namespace io.github.hatayama.uLoopMCP
{
///
/// Task related extension methods
/// Provides convenient methods for fire-and-forget async operations
///
public static class TaskExtensions
{
///
/// Fire-and-forget extension method for Task
/// Equivalent to "_ = task;" but more explicit and readable
///
/// Task to fire and forget
public static void Forget(this Task task)
{
if (task == null) return;
// Handle exceptions to prevent crashes
_ = HandleTaskExceptions(task);
}
///
/// Handle task exceptions to prevent unhandled exceptions in fire-and-forget scenarios
///
private static async Task HandleTaskExceptions(Task task)
{
try
{
await task;
}
catch (System.Exception ex)
{
// Log the exception to prevent silent failures
UnityEngine.Debug.LogException(ex);
}
}
///
/// Fire-and-forget extension method for DelayFrameAwaitable
/// Allows writing: EditorDelay.DelayFrame().Forget()
///
/// DelayFrameAwaitable to fire and forget
public static void Forget(this DelayFrameAwaitable awaitable)
{
// Convert to Task and discard using consistent pattern
ConvertToTask(awaitable).Forget();
}
///
/// Convert DelayFrameAwaitable to Task for fire-and-forget operations
/// Includes exception handling to prevent unobserved task exceptions in fire-and-forget scenarios
///
private static async Task ConvertToTask(DelayFrameAwaitable awaitable)
{
try
{
await awaitable;
}
catch (System.Exception ex)
{
// Log the exception to prevent silent failures
UnityEngine.Debug.LogException(ex);
// Intentionally swallow exceptions in fire-and-forget scenarios
// to prevent unobserved task exceptions from crashing the application
}
}
}
}