UniTask/Assets/UniRx.Async/EnumeratorAsyncExtensions.cs

230 lines
7.7 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 // Missing XML comment for publicly visible type or member
using System;
using System.Collections;
2019-05-30 18:41:23 +08:00
using System.Reflection;
2019-05-19 23:14:47 +08:00
using System.Runtime.ExceptionServices;
using System.Threading;
using UniRx.Async.Internal;
2019-05-30 18:41:23 +08:00
using UnityEngine;
2019-05-19 23:14:47 +08:00
namespace UniRx.Async
{
public static class EnumeratorAsyncExtensions
{
2020-04-20 07:35:06 +08:00
public static UniTask.Awaiter GetAwaiter(this IEnumerator enumerator)
2019-05-19 23:14:47 +08:00
{
2020-04-20 07:35:06 +08:00
return new UniTask(EnumeratorPromise.Create(enumerator, PlayerLoopTiming.Update, CancellationToken.None, out var token), token).GetAwaiter();
2019-05-19 23:14:47 +08:00
}
public static UniTask ToUniTask(this IEnumerator enumerator)
{
2020-04-20 07:35:06 +08:00
return new UniTask(EnumeratorPromise.Create(enumerator, PlayerLoopTiming.Update, CancellationToken.None, out var token), token);
2019-05-19 23:14:47 +08:00
}
public static UniTask ConfigureAwait(this IEnumerator enumerator, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
{
2020-04-20 07:35:06 +08:00
return new UniTask(EnumeratorPromise.Create(enumerator, timing, cancellationToken, out var token), token);
2019-05-19 23:14:47 +08:00
}
2020-04-20 07:35:06 +08:00
class EnumeratorPromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem
2019-05-19 23:14:47 +08:00
{
2020-04-20 07:35:06 +08:00
static readonly PromisePool<EnumeratorPromise> pool = new PromisePool<EnumeratorPromise>();
2019-05-19 23:14:47 +08:00
IEnumerator innerEnumerator;
CancellationToken cancellationToken;
2020-04-20 07:35:06 +08:00
UniTaskCompletionSourceCore<object> core;
EnumeratorPromise()
{
}
public static IUniTaskSource Create(IEnumerator innerEnumerator, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token)
2019-05-19 23:14:47 +08:00
{
if (cancellationToken.IsCancellationRequested)
{
2020-04-20 07:35:06 +08:00
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
2019-05-19 23:14:47 +08:00
}
2020-04-20 07:35:06 +08:00
var result = pool.TryRent() ?? new EnumeratorPromise();
2019-05-19 23:14:47 +08:00
2020-04-20 07:35:06 +08:00
result.innerEnumerator = ConsumeEnumerator(innerEnumerator);
result.cancellationToken = cancellationToken;
2019-05-19 23:14:47 +08:00
TaskTracker.TrackActiveTask(result, 3);
2019-05-19 23:14:47 +08:00
2020-04-20 07:35:06 +08:00
PlayerLoopHelper.AddAction(timing, result);
token = result.core.Version;
return result;
}
2019-05-19 23:14:47 +08:00
2020-04-20 07:35:06 +08:00
public void GetResult(short token)
2019-05-19 23:14:47 +08:00
{
2020-04-20 07:35:06 +08:00
try
{
TaskTracker.RemoveTracking(this);
2020-04-20 07:35:06 +08:00
core.GetResult(token);
}
finally
2019-05-19 23:14:47 +08:00
{
2020-04-20 07:35:06 +08:00
pool.TryReturn(this);
2019-05-19 23:14:47 +08:00
}
}
2020-04-20 07:35:06 +08:00
public UniTaskStatus GetStatus(short token)
{
return core.GetStatus(token);
}
public UniTaskStatus UnsafeGetStatus()
{
return core.UnsafeGetStatus();
}
public void OnCompleted(Action<object> continuation, object state, short token)
{
core.OnCompleted(continuation, state, token);
}
2019-05-19 23:14:47 +08:00
public bool MoveNext()
{
if (cancellationToken.IsCancellationRequested)
{
2020-04-21 12:36:23 +08:00
core.TrySetCanceled(cancellationToken);
2019-05-19 23:14:47 +08:00
return false;
}
try
{
if (innerEnumerator.MoveNext())
{
return true;
}
}
catch (Exception ex)
{
2020-04-21 12:36:23 +08:00
core.TrySetException(ex);
2020-04-20 07:35:06 +08:00
return false;
2019-05-19 23:14:47 +08:00
}
2020-04-21 12:36:23 +08:00
core.TrySetResult(null);
2019-05-19 23:14:47 +08:00
return false;
}
2020-04-20 07:35:06 +08:00
public void Reset()
2019-05-19 23:14:47 +08:00
{
2020-04-20 07:35:06 +08:00
core.Reset();
innerEnumerator = default;
cancellationToken = default;
2019-05-19 23:14:47 +08:00
}
2019-05-30 18:41:23 +08:00
2020-04-21 12:36:23 +08:00
~EnumeratorPromise()
{
if (pool.TryReturn(this))
{
GC.ReRegisterForFinalize(this);
}
}
2019-05-30 18:41:23 +08:00
// Unwrap YieldInstructions
static IEnumerator ConsumeEnumerator(IEnumerator enumerator)
{
while (enumerator.MoveNext())
{
var current = enumerator.Current;
if (current == null)
{
yield return null;
}
else if (current is CustomYieldInstruction)
{
// WWW, WaitForSecondsRealtime
var e2 = UnwrapWaitCustomYieldInstruction((CustomYieldInstruction)current);
while (e2.MoveNext())
{
yield return null;
}
}
else if (current is YieldInstruction)
{
IEnumerator innerCoroutine = null;
switch (current)
{
case AsyncOperation ao:
innerCoroutine = UnwrapWaitAsyncOperation(ao);
break;
case WaitForSeconds wfs:
innerCoroutine = UnwrapWaitForSeconds(wfs);
break;
}
if (innerCoroutine != null)
{
while (innerCoroutine.MoveNext())
{
yield return null;
}
}
else
{
yield return null;
}
}
else if (current is IEnumerator e3)
{
var e4 = ConsumeEnumerator(e3);
while (e4.MoveNext())
2019-05-30 18:41:23 +08:00
{
yield return null;
}
}
else
{
// WaitForEndOfFrame, WaitForFixedUpdate, others.
yield return null;
}
}
}
// WWW and others as CustomYieldInstruction.
static IEnumerator UnwrapWaitCustomYieldInstruction(CustomYieldInstruction yieldInstruction)
{
while (yieldInstruction.keepWaiting)
{
yield return null;
}
}
static readonly FieldInfo waitForSeconds_Seconds = typeof(WaitForSeconds).GetField("m_Seconds", BindingFlags.Instance | BindingFlags.GetField | BindingFlags.NonPublic);
static IEnumerator UnwrapWaitForSeconds(WaitForSeconds waitForSeconds)
{
var second = (float)waitForSeconds_Seconds.GetValue(waitForSeconds);
var startTime = DateTimeOffset.UtcNow;
while (true)
{
yield return null;
var elapsed = (DateTimeOffset.UtcNow - startTime).TotalSeconds;
if (elapsed >= second)
{
break;
}
};
}
static IEnumerator UnwrapWaitAsyncOperation(AsyncOperation asyncOperation)
{
while (!asyncOperation.isDone)
{
yield return null;
}
}
2019-05-19 23:14:47 +08:00
}
}
}
#endif