#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; using UnityEngine.Events; namespace UniRx.Async { public partial struct UniTask { static readonly UniTask CanceledUniTask = new Func(() => { var promise = new UniTaskCompletionSource(); promise.TrySetCanceled(); promise.MarkHandled(); return new UniTask(promise); })(); public static UniTask CompletedTask { get { return new UniTask(); } } public static UniTask FromException(Exception ex) { var promise = new UniTaskCompletionSource(); promise.TrySetException(ex); promise.MarkHandled(); return new UniTask(promise); } public static UniTask FromException(Exception ex) { var promise = new UniTaskCompletionSource(); promise.TrySetException(ex); promise.MarkHandled(); return new UniTask(promise); } public static UniTask FromResult(T value) { return new UniTask(value); } public static UniTask FromCanceled() { return CanceledUniTask; } public static UniTask FromCanceled() { return CanceledUniTaskCache.Task; } public static UniTask FromCanceled(CancellationToken token) { var promise = new UniTaskCompletionSource(); promise.TrySetException(new OperationCanceledException(token)); promise.MarkHandled(); return new UniTask(promise); } public static UniTask FromCanceled(CancellationToken token) { var promise = new UniTaskCompletionSource(); promise.TrySetException(new OperationCanceledException(token)); promise.MarkHandled(); return new UniTask(promise); } /// shorthand of new UniTask[T](Func[UniTask[T]] factory) public static UniTask Lazy(Func> factory) { return new UniTask(factory); } /// /// helper of create add UniTaskVoid to delegate. /// For example: FooEvent += () => UniTask.Void(async () => { /* */ }) /// public static void Void(Func asyncAction) { asyncAction().Forget(); } public static Action VoidAction(Func asyncAction) { return () => Void(asyncAction); } public static UnityAction VoidUnityAction(Func asyncAction) { return () => Void(asyncAction); } /// /// helper of create add UniTaskVoid to delegate. /// For example: FooEvent += (sender, e) => UniTask.Void(async arg => { /* */ }, (sender, e)) /// public static void Void(Func asyncAction, T state) { asyncAction(state).Forget(); } static class CanceledUniTaskCache { public static readonly UniTask Task; static CanceledUniTaskCache() { var promise = new UniTaskCompletionSource(); promise.TrySetCanceled(); promise.MarkHandled(); Task = new UniTask(promise); } } } internal static class CompletedTasks { public static readonly UniTask True = UniTask.FromResult(true); public static readonly UniTask False = UniTask.FromResult(false); public static readonly UniTask Zero = UniTask.FromResult(0); public static readonly UniTask MinusOne = UniTask.FromResult(-1); public static readonly UniTask One = UniTask.FromResult(1); } internal static class CompletedTasks2 { public static readonly UniTask2 Completed = new UniTask2(); public static readonly UniTask2 AsyncUnit = UniTask2.FromResult(UniRx.Async.AsyncUnit.Default); public static readonly UniTask2 True = UniTask2.FromResult(true); public static readonly UniTask2 False = UniTask2.FromResult(false); public static readonly UniTask2 Zero = UniTask2.FromResult(0); public static readonly UniTask2 MinusOne = UniTask2.FromResult(-1); public static readonly UniTask2 One = UniTask2.FromResult(1); } } #endif