//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// UserRTX.NET satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Threading.Tasks;

#if !WASM
namespace user_rtx { using u = user_rtx;
#else
namespace user_rtx.wasm { using u = user_rtx.wasm;
#endif

/// <summary>
/// Task helpers
/// </summary>
public static partial class task {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// <exception cref="AggregateException" />
/// <exception cref="OperationCanceledException" />
public static void Execute( Action action ) =>
   Task.Run( action ).Wait();

/// <exception cref="AggregateException" />
/// <exception cref="OperationCanceledException" />
public static void Execute( Func<Task> actionA ) =>
   Task.Run( actionA ).Wait();

//--------------------------------------------------------------------------------------------------

/// <exception cref="AggregateException" />
public static R Execute<R>( Func<R> function ) =>
   Task.Run( function ).Result;

/// <exception cref="AggregateException" />
public static R Execute<R>( Func<Task<R>> functionA ) =>
   Task.Run( functionA ).Result;

//==================================================================================================

public static bool TryExec( out Exception? error, Action action ) =>
   u.ops.Try( out error, () => Execute( action ) );

public static bool TryExec( out Exception? error, Func<Task> actionA ) =>
   u.ops.Try( out error, () => Execute( actionA ) );

//--------------------------------------------------------------------------------------------------

public static R? TryExec<R>( out Exception? error, Func<R> function ) =>
   u.ops.Try<R>( out error, () => Execute<R>( function ) );

public static R? TryExec<R>( out Exception? error, Func<Task<R>> functionA ) =>
   u.ops.Try<R>( out error, () => Execute<R>( functionA ) );

//==================================================================================================

public static Exception? CatchExec( Action action ) =>
   u.ops.Catch(() => Execute( action ));

public static Exception? CatchExec( Func<Task> actionA ) =>
   u.ops.Catch(() => Execute( actionA ));

//--------------------------------------------------------------------------------------------------

public static Exception? CatchExec<R>( out R? result, Func<R> function ) =>
   u.ops.Catch<R>( out result, () => Execute<R>( function ) );

public static Exception? CatchExec<R>( out R? result, Func<Task<R>> functionA ) =>
   u.ops.Catch<R>( out result, () => Execute<R>( functionA ) );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // task

} // user_rtx (.wasm)
