#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using System;
using System.Collections;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using UniRx.Async.Internal;
namespace UniRx.Async
{
public static partial class UniTaskExtensions
{
///
/// Convert UniTask -> UniTask[AsyncUnit].
///
public static UniTask AsAsyncUnitUniTask(this UniTask task)
{
// use implicit conversion
return task;
}
///
/// Convert UniTask[T] -> UniTask.
///
public static UniTask AsUniTask(this UniTask task)
{
// use implicit conversion
return task;
}
///
/// Convert Task[T] -> UniTask[T].
///
public static UniTask AsUniTask(this Task task, bool useCurrentSynchronizationContext = true)
{
var promise = new UniTaskCompletionSource();
task.ContinueWith((x, state) =>
{
var p = (UniTaskCompletionSource)state;
switch (x.Status)
{
case TaskStatus.Canceled:
p.SetCanceled();
break;
case TaskStatus.Faulted:
p.SetException(x.Exception);
break;
case TaskStatus.RanToCompletion:
p.SetResult(x.Result);
break;
default:
throw new NotSupportedException();
}
}, promise, useCurrentSynchronizationContext ? TaskScheduler.FromCurrentSynchronizationContext() : TaskScheduler.Current);
return promise.Task;
}
///
/// Convert Task -> UniTask.
///
public static UniTask AsUniTask(this Task task, bool useCurrentSynchronizationContext = true)
{
var promise = new UniTaskCompletionSource();
task.ContinueWith((x, state) =>
{
var p = (UniTaskCompletionSource)state;
switch (x.Status)
{
case TaskStatus.Canceled:
p.SetCanceled();
break;
case TaskStatus.Faulted:
p.SetException(x.Exception);
break;
case TaskStatus.RanToCompletion:
p.SetResult();
break;
default:
throw new NotSupportedException();
}
}, promise, useCurrentSynchronizationContext ? TaskScheduler.FromCurrentSynchronizationContext() : TaskScheduler.Current);
return promise.Task;
}
public static Task AsTask(this UniTask task)
{
try
{
var awaiter = task.GetAwaiter();
if (awaiter.IsCompleted)
{
try
{
var result = awaiter.GetResult();
return Task.FromResult(result);
}
catch (Exception ex)
{
return Task.FromException(ex);
}
}
var tcs = new TaskCompletionSource();
awaiter.SourceOnCompleted(state =>
{
var (inTcs, inAwaiter) = ((TaskCompletionSource, UniTask.Awaiter))state;
try
{
var result = inAwaiter.GetResult();
inTcs.SetResult(result);
}
catch (Exception ex)
{
inTcs.SetException(ex);
}
}, (tcs, awaiter));
return tcs.Task;
}
catch (Exception ex)
{
return Task.FromException(ex);
}
}
public static Task AsTask(this UniTask task)
{
try
{
var awaiter = task.GetAwaiter();
if (awaiter.IsCompleted)
{
try
{
awaiter.GetResult(); // check token valid on Succeeded
return Task.CompletedTask;
}
catch (Exception ex)
{
return Task.FromException(ex);
}
}
var tcs = new TaskCompletionSource