UniTask/Assets/UniRx.Async/IUniTaskSource.cs

68 lines
2.1 KiB
C#
Raw Normal View History

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
2020-04-18 04:07:59 +08:00
using System;
2019-05-19 23:14:47 +08:00
using System.Runtime.CompilerServices;
namespace UniRx.Async
{
2020-04-20 07:35:06 +08:00
public enum UniTaskStatus
2019-05-19 23:14:47 +08:00
{
/// <summary>The operation has not yet completed.</summary>
Pending = 0,
/// <summary>The operation completed successfully.</summary>
Succeeded = 1,
/// <summary>The operation completed with an error.</summary>
Faulted = 2,
/// <summary>The operation completed due to cancellation.</summary>
Canceled = 3
}
2020-04-18 04:07:59 +08:00
// similar as IValueTaskSource
public interface IUniTaskSource
{
2020-04-20 07:35:06 +08:00
UniTaskStatus GetStatus(short token);
2020-04-18 04:07:59 +08:00
void OnCompleted(Action<object> continuation, object state, short token);
void GetResult(short token);
2020-04-20 07:35:06 +08:00
UniTaskStatus UnsafeGetStatus(); // only for debug use.
2020-04-18 04:07:59 +08:00
}
public interface IUniTaskSource<out T> : IUniTaskSource
{
new T GetResult(short token);
}
2020-04-20 07:35:06 +08:00
public static class UniTaskStatusExtensions
2019-05-19 23:14:47 +08:00
{
/// <summary>status != Pending.</summary>
2019-05-19 23:14:47 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2020-04-20 07:35:06 +08:00
public static bool IsCompleted(this UniTaskStatus status)
2019-05-19 23:14:47 +08:00
{
2020-04-20 07:35:06 +08:00
return status != UniTaskStatus.Pending;
2019-05-19 23:14:47 +08:00
}
/// <summary>status == Succeeded.</summary>
2019-05-19 23:14:47 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2020-04-20 07:35:06 +08:00
public static bool IsCompletedSuccessfully(this UniTaskStatus status)
2019-05-19 23:14:47 +08:00
{
2020-04-20 07:35:06 +08:00
return status == UniTaskStatus.Succeeded;
2019-05-19 23:14:47 +08:00
}
/// <summary>status == Canceled.</summary>
2019-05-19 23:14:47 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2020-04-20 07:35:06 +08:00
public static bool IsCanceled(this UniTaskStatus status)
2019-05-19 23:14:47 +08:00
{
2020-04-20 07:35:06 +08:00
return status == UniTaskStatus.Canceled;
2019-05-19 23:14:47 +08:00
}
/// <summary>status == Faulted.</summary>
2019-05-19 23:14:47 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2020-04-20 07:35:06 +08:00
public static bool IsFaulted(this UniTaskStatus status)
2019-05-19 23:14:47 +08:00
{
2020-04-20 07:35:06 +08:00
return status == UniTaskStatus.Faulted;
2019-05-19 23:14:47 +08:00
}
}
}
#endif