2019-05-19 23:14:47 +08:00
|
|
|
|
#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.Threading;
|
2020-04-18 04:07:59 +08:00
|
|
|
|
using UnityEngine.Events;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
|
|
|
|
namespace UniRx.Async
|
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public partial struct UniTask
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
static readonly UniTask CanceledUniTask = new Func<UniTask>(() =>
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
var promise = new UniTaskCompletionSource();
|
2020-04-26 01:38:16 +08:00
|
|
|
|
promise.TrySetCanceled(CancellationToken.None);
|
2020-04-19 01:55:44 +08:00
|
|
|
|
promise.MarkHandled();
|
|
|
|
|
return promise.Task;
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
static class CanceledUniTaskCache<T>
|
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public static readonly UniTask<T> Task;
|
2020-04-19 01:55:44 +08:00
|
|
|
|
|
|
|
|
|
static CanceledUniTaskCache()
|
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
var promise = new UniTaskCompletionSource<T>();
|
2020-04-26 01:38:16 +08:00
|
|
|
|
promise.TrySetCanceled(CancellationToken.None);
|
2020-04-19 01:55:44 +08:00
|
|
|
|
promise.MarkHandled();
|
|
|
|
|
Task = promise.Task;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public static readonly UniTask CompletedTask = new UniTask();
|
2020-04-19 01:55:44 +08:00
|
|
|
|
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public static UniTask FromException(Exception ex)
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
var promise = new UniTaskCompletionSource();
|
2020-04-26 01:38:16 +08:00
|
|
|
|
promise.TrySetException(ex);
|
2020-04-19 01:55:44 +08:00
|
|
|
|
promise.MarkHandled();
|
|
|
|
|
return promise.Task;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public static UniTask<T> FromException<T>(Exception ex)
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
var promise = new UniTaskCompletionSource<T>();
|
2020-04-26 01:38:16 +08:00
|
|
|
|
promise.TrySetException(ex);
|
2020-04-19 01:55:44 +08:00
|
|
|
|
promise.MarkHandled();
|
|
|
|
|
return promise.Task;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public static UniTask<T> FromResult<T>(T value)
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
return new UniTask<T>(value);
|
2020-04-19 01:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public static UniTask FromCanceled(CancellationToken cancellationToken = default)
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
|
|
|
|
if (cancellationToken == CancellationToken.None)
|
|
|
|
|
{
|
|
|
|
|
return CanceledUniTask;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
var promise = new UniTaskCompletionSource();
|
2020-04-26 01:38:16 +08:00
|
|
|
|
promise.TrySetCanceled(cancellationToken);
|
2020-04-19 01:55:44 +08:00
|
|
|
|
promise.MarkHandled();
|
|
|
|
|
return promise.Task;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public static UniTask<T> FromCanceled<T>(CancellationToken cancellationToken = default)
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
|
|
|
|
if (cancellationToken == CancellationToken.None)
|
|
|
|
|
{
|
|
|
|
|
return CanceledUniTaskCache<T>.Task;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
var promise = new UniTaskCompletionSource<T>();
|
2020-04-26 01:38:16 +08:00
|
|
|
|
promise.TrySetCanceled(cancellationToken);
|
2020-04-19 01:55:44 +08:00
|
|
|
|
promise.MarkHandled();
|
|
|
|
|
return promise.Task;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
public static UniTask Create(Func<UniTask> factory)
|
|
|
|
|
{
|
|
|
|
|
return factory();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static UniTask<T> Create<T>(Func<UniTask<T>> factory)
|
|
|
|
|
{
|
|
|
|
|
return factory();
|
|
|
|
|
}
|
2020-04-19 01:55:44 +08:00
|
|
|
|
|
2020-05-05 03:17:08 +08:00
|
|
|
|
public static AsyncLazy Lazy(Func<UniTask> factory)
|
|
|
|
|
{
|
|
|
|
|
return new AsyncLazy(factory);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
public static AsyncLazy<T> Lazy<T>(Func<UniTask<T>> factory)
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
2020-04-20 07:35:06 +08:00
|
|
|
|
return new AsyncLazy<T>(factory);
|
2020-04-19 01:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// helper of create add UniTaskVoid to delegate.
|
|
|
|
|
/// For example: FooEvent += () => UniTask.Void(async () => { /* */ })
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void Void(Func<UniTask> asyncAction)
|
|
|
|
|
{
|
|
|
|
|
asyncAction().Forget();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Action VoidAction(Func<UniTask> asyncAction)
|
|
|
|
|
{
|
|
|
|
|
return () => Void(asyncAction);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static UnityAction VoidUnityAction(Func<UniTask> asyncAction)
|
|
|
|
|
{
|
|
|
|
|
return () => Void(asyncAction);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// helper of create add UniTaskVoid to delegate.
|
|
|
|
|
/// For example: FooEvent += (sender, e) => UniTask.Void(async arg => { /* */ }, (sender, e))
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void Void<T>(Func<T, UniTask> asyncAction, T state)
|
|
|
|
|
{
|
|
|
|
|
asyncAction(state).Forget();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-19 23:14:47 +08:00
|
|
|
|
internal static class CompletedTasks
|
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public static readonly UniTask<AsyncUnit> AsyncUnit = UniTask.FromResult(UniRx.Async.AsyncUnit.Default);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
public static readonly UniTask<bool> True = UniTask.FromResult(true);
|
|
|
|
|
public static readonly UniTask<bool> False = UniTask.FromResult(false);
|
|
|
|
|
public static readonly UniTask<int> Zero = UniTask.FromResult(0);
|
|
|
|
|
public static readonly UniTask<int> MinusOne = UniTask.FromResult(-1);
|
|
|
|
|
public static readonly UniTask<int> One = UniTask.FromResult(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|