//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#nullable enable
using System;
using System.Threading;
using System.Threading.Tasks;

#if !WASM
namespace CommonHelpers {
#else
namespace CommonHelpers.WASM {
#endif

//**************************************************************************************************
/// <summary>
/// </summary>
public static partial class HCriticalOperation {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static HFakeException RaiseError(Exception e) =>
   HFatalError.Raise(
      "Exception has occured during performing of critical operation.",
      e );

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

public static void Do(Action operation)
{
   HArgChecking.VerifyNotNull( operation );
   try
      { operation(); }
   catch (Exception e)
      { throw RaiseError( e ); }
}
public static R Do<R>(Func<R> function)
{
   HArgChecking.VerifyNotNull( function );
   try
      { return function(); }
   catch (Exception e)
      { throw RaiseError( e ); }
}

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

public static async Task Do_A(Func<Task> operationA)
{
   HArgChecking.VerifyNotNull( operationA );
   try {
      await operationA().ConfigureAwait(false); }
   catch (Exception e) {
      throw RaiseError( e ); }
}
public static async Task<R> Do_A<R>(Func<Task<R>> functionA)
{
   HArgChecking.VerifyNotNull( functionA );
   try {
      return await functionA().ConfigureAwait(false); }
   catch (Exception e) {
      throw RaiseError( e ); }
}
public static async ValueTask<R> Do_A<R>(Func<ValueTask<R>> functionA)
{
   HArgChecking.VerifyNotNull( functionA );
   try { return await functionA(); }
   catch (Exception e) {
      throw RaiseError(e); }
}

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

static HCriticalOperation()
{
   CommonHelpers_Core.Access();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HCriticalOperation
//**************************************************************************************************

} // CommonHelpers (.WASM)
