UniTask/Assets/UniRx.Async/UniTask.WaitUntil.cs

478 lines
16 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.Generic;
using System.Threading;
using UniRx.Async.Internal;
namespace UniRx.Async
{
public partial struct UniTask
{
public static UniTask WaitUntil(Func<bool> predicate, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
{
2020-04-21 12:36:23 +08:00
return new UniTask(WaitUntilPromise.Create(predicate, timing, cancellationToken, out var token), token);
2019-05-19 23:14:47 +08:00
}
public static UniTask WaitWhile(Func<bool> predicate, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
{
2020-04-21 12:36:23 +08:00
return new UniTask(WaitWhilePromise.Create(predicate, timing, cancellationToken, out var token), token);
2019-05-19 23:14:47 +08:00
}
public static UniTask<U> WaitUntilValueChanged<T, U>(T target, Func<T, U> monitorFunction, PlayerLoopTiming monitorTiming = PlayerLoopTiming.Update, IEqualityComparer<U> equalityComparer = null, CancellationToken cancellationToken = default(CancellationToken))
where T : class
{
var unityObject = target as UnityEngine.Object;
var isUnityObject = !object.ReferenceEquals(target, null); // don't use (unityObject == null)
2020-04-21 12:36:23 +08:00
return new UniTask<U>(isUnityObject
? WaitUntilValueChangedUnityObjectPromise<T, U>.Create(target, monitorFunction, equalityComparer, monitorTiming, cancellationToken, out var token)
: WaitUntilValueChangedStandardObjectPromise<T, U>.Create(target, monitorFunction, equalityComparer, monitorTiming, cancellationToken, out token), token);
2019-05-19 23:14:47 +08:00
}
2020-04-21 12:36:23 +08:00
sealed class WaitUntilPromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem
2019-05-19 23:14:47 +08:00
{
2020-04-21 12:36:23 +08:00
static readonly PromisePool<WaitUntilPromise> pool = new PromisePool<WaitUntilPromise>();
2019-05-19 23:14:47 +08:00
2020-04-21 12:36:23 +08:00
Func<bool> predicate;
CancellationToken cancellationToken;
UniTaskCompletionSourceCore<object> core;
WaitUntilPromise()
{
}
public static IUniTaskSource Create(Func<bool> predicate, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token)
2019-05-19 23:14:47 +08:00
{
2020-04-21 12:36:23 +08:00
if (cancellationToken.IsCancellationRequested)
{
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
}
var result = pool.TryRent() ?? new WaitUntilPromise();
result.predicate = predicate;
result.cancellationToken = cancellationToken;
TaskTracker.TrackActiveTask(result, 3);
2020-04-21 12:36:23 +08:00
PlayerLoopHelper.AddAction(timing, result);
token = result.core.Version;
return result;
2019-05-19 23:14:47 +08:00
}
2020-04-21 12:36:23 +08:00
public void GetResult(short token)
2019-05-19 23:14:47 +08:00
{
2020-04-21 12:36:23 +08:00
try
{
TaskTracker.RemoveTracking(this);
2020-04-21 12:36:23 +08:00
core.GetResult(token);
}
finally
{
pool.TryReturn(this);
}
2019-05-19 23:14:47 +08:00
}
2020-04-21 12:36:23 +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);
}
public bool MoveNext()
2019-05-19 23:14:47 +08:00
{
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
{
2020-04-21 12:36:23 +08:00
if (!predicate())
{
return true;
}
2019-05-19 23:14:47 +08:00
}
catch (Exception ex)
{
2020-04-21 12:36:23 +08:00
core.TrySetException(ex);
2019-05-19 23:14:47 +08:00
return false;
}
2020-04-21 12:36:23 +08:00
core.TrySetResult(null);
return false;
}
public void Reset()
{
core.Reset();
predicate = default;
cancellationToken = default;
}
~WaitUntilPromise()
{
if (pool.TryReturn(this))
2019-05-19 23:14:47 +08:00
{
2020-04-21 12:36:23 +08:00
GC.ReRegisterForFinalize(this);
2019-05-19 23:14:47 +08:00
}
}
}
2020-04-21 12:36:23 +08:00
sealed class WaitWhilePromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem
2019-05-19 23:14:47 +08:00
{
2020-04-21 12:36:23 +08:00
static readonly PromisePool<WaitWhilePromise> pool = new PromisePool<WaitWhilePromise>();
2019-05-19 23:14:47 +08:00
2020-04-21 12:36:23 +08:00
Func<bool> predicate;
CancellationToken cancellationToken;
UniTaskCompletionSourceCore<object> core;
WaitWhilePromise()
{
}
public static IUniTaskSource Create(Func<bool> predicate, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token)
2019-05-19 23:14:47 +08:00
{
2020-04-21 12:36:23 +08:00
if (cancellationToken.IsCancellationRequested)
{
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
}
var result = pool.TryRent() ?? new WaitWhilePromise();
result.predicate = predicate;
result.cancellationToken = cancellationToken;
TaskTracker.TrackActiveTask(result, 3);
2020-04-21 12:36:23 +08:00
PlayerLoopHelper.AddAction(timing, result);
token = result.core.Version;
return result;
2019-05-19 23:14:47 +08:00
}
2020-04-21 12:36:23 +08:00
public void GetResult(short token)
2019-05-19 23:14:47 +08:00
{
2020-04-21 12:36:23 +08:00
try
{
TaskTracker.RemoveTracking(this);
2020-04-21 12:36:23 +08:00
core.GetResult(token);
}
finally
{
pool.TryReturn(this);
}
2019-05-19 23:14:47 +08:00
}
2020-04-21 12:36:23 +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);
}
public bool MoveNext()
2019-05-19 23:14:47 +08:00
{
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
{
2020-04-21 12:36:23 +08:00
if (predicate())
{
return true;
}
2019-05-19 23:14:47 +08:00
}
catch (Exception ex)
{
2020-04-21 12:36:23 +08:00
core.TrySetException(ex);
2019-05-19 23:14:47 +08:00
return false;
}
2020-04-21 12:36:23 +08:00
core.TrySetResult(null);
return false;
}
public void Reset()
{
core.Reset();
predicate = default;
cancellationToken = default;
}
~WaitWhilePromise()
{
if (pool.TryReturn(this))
2019-05-19 23:14:47 +08:00
{
2020-04-21 12:36:23 +08:00
GC.ReRegisterForFinalize(this);
2019-05-19 23:14:47 +08:00
}
}
}
// where T : UnityEngine.Object, can not add constraint
2020-04-21 12:36:23 +08:00
sealed class WaitUntilValueChangedUnityObjectPromise<T, U> : IUniTaskSource<U>, IPlayerLoopItem, IPromisePoolItem
2019-05-19 23:14:47 +08:00
{
2020-04-21 12:36:23 +08:00
static readonly PromisePool<WaitUntilValueChangedUnityObjectPromise<T, U>> pool = new PromisePool<WaitUntilValueChangedUnityObjectPromise<T, U>>();
T target;
2019-05-19 23:14:47 +08:00
U currentValue;
2020-04-21 12:36:23 +08:00
Func<T, U> monitorFunction;
IEqualityComparer<U> equalityComparer;
CancellationToken cancellationToken;
UniTaskCompletionSourceCore<U> core;
WaitUntilValueChangedUnityObjectPromise()
{
}
2019-05-19 23:14:47 +08:00
2020-04-21 12:36:23 +08:00
public static IUniTaskSource<U> Create(T target, Func<T, U> monitorFunction, IEqualityComparer<U> equalityComparer, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token)
2019-05-19 23:14:47 +08:00
{
2020-04-21 12:36:23 +08:00
if (cancellationToken.IsCancellationRequested)
{
return AutoResetUniTaskCompletionSource<U>.CreateFromCanceled(cancellationToken, out token);
}
var result = pool.TryRent() ?? new WaitUntilValueChangedUnityObjectPromise<T, U>();
result.target = target;
result.monitorFunction = monitorFunction;
result.currentValue = monitorFunction(target);
result.equalityComparer = equalityComparer ?? UnityEqualityComparer.GetDefault<U>();
result.cancellationToken = cancellationToken;
TaskTracker.TrackActiveTask(result, 3);
2020-04-21 12:36:23 +08:00
PlayerLoopHelper.AddAction(timing, result);
token = result.core.Version;
return result;
2019-05-19 23:14:47 +08:00
}
2020-04-21 12:36:23 +08:00
public U GetResult(short token)
2019-05-19 23:14:47 +08:00
{
2020-04-21 12:36:23 +08:00
try
{
TaskTracker.RemoveTracking(this);
2020-04-21 12:36:23 +08:00
return core.GetResult(token);
}
finally
{
pool.TryReturn(this);
}
2019-05-19 23:14:47 +08:00
}
2020-04-21 12:36:23 +08:00
void IUniTaskSource.GetResult(short token)
{
GetResult(token);
}
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);
}
public bool MoveNext()
2019-05-19 23:14:47 +08:00
{
if (cancellationToken.IsCancellationRequested || target == null) // destroyed = cancel.
{
2020-04-21 12:36:23 +08:00
core.TrySetCanceled(cancellationToken);
2019-05-19 23:14:47 +08:00
return false;
}
U nextValue = default(U);
try
{
nextValue = monitorFunction(target);
if (equalityComparer.Equals(currentValue, nextValue))
{
return true;
}
}
catch (Exception ex)
{
2020-04-21 12:36:23 +08:00
core.TrySetException(ex);
2019-05-19 23:14:47 +08:00
return false;
}
2020-04-21 12:36:23 +08:00
core.TrySetResult(nextValue);
2019-05-19 23:14:47 +08:00
return false;
}
2020-04-21 12:36:23 +08:00
public void Reset()
{
core.Reset();
target = default;
currentValue = default;
monitorFunction = default;
equalityComparer = default;
cancellationToken = default;
}
~WaitUntilValueChangedUnityObjectPromise()
{
if (pool.TryReturn(this))
{
GC.ReRegisterForFinalize(this);
}
}
2019-05-19 23:14:47 +08:00
}
2020-04-21 12:36:23 +08:00
sealed class WaitUntilValueChangedStandardObjectPromise<T, U> : IUniTaskSource<U>, IPlayerLoopItem, IPromisePoolItem
2019-05-19 23:14:47 +08:00
where T : class
{
2020-04-21 12:36:23 +08:00
static readonly PromisePool<WaitUntilValueChangedStandardObjectPromise<T, U>> pool = new PromisePool<WaitUntilValueChangedStandardObjectPromise<T, U>>();
WeakReference<T> target;
2019-05-19 23:14:47 +08:00
U currentValue;
2020-04-21 12:36:23 +08:00
Func<T, U> monitorFunction;
IEqualityComparer<U> equalityComparer;
CancellationToken cancellationToken;
UniTaskCompletionSourceCore<U> core;
WaitUntilValueChangedStandardObjectPromise()
{
}
public static IUniTaskSource<U> Create(T target, Func<T, U> monitorFunction, IEqualityComparer<U> equalityComparer, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token)
{
if (cancellationToken.IsCancellationRequested)
{
return AutoResetUniTaskCompletionSource<U>.CreateFromCanceled(cancellationToken, out token);
}
var result = pool.TryRent() ?? new WaitUntilValueChangedStandardObjectPromise<T, U>();
result.target = new WeakReference<T>(target, false); // wrap in WeakReference.
result.monitorFunction = monitorFunction;
result.currentValue = monitorFunction(target);
result.equalityComparer = equalityComparer ?? UnityEqualityComparer.GetDefault<U>();
result.cancellationToken = cancellationToken;
TaskTracker.TrackActiveTask(result, 3);
2020-04-21 12:36:23 +08:00
PlayerLoopHelper.AddAction(timing, result);
token = result.core.Version;
return result;
}
public U GetResult(short token)
{
try
{
TaskTracker.RemoveTracking(this);
2020-04-21 12:36:23 +08:00
return core.GetResult(token);
}
finally
{
pool.TryReturn(this);
}
}
2019-05-19 23:14:47 +08:00
2020-04-21 12:36:23 +08:00
void IUniTaskSource.GetResult(short token)
2019-05-19 23:14:47 +08:00
{
2020-04-21 12:36:23 +08:00
GetResult(token);
2019-05-19 23:14:47 +08:00
}
2020-04-21 12:36:23 +08:00
public UniTaskStatus GetStatus(short token)
2019-05-19 23:14:47 +08:00
{
2020-04-21 12:36:23 +08:00
return core.GetStatus(token);
2019-05-19 23:14:47 +08:00
}
2020-04-21 12:36:23 +08:00
public UniTaskStatus UnsafeGetStatus()
2019-05-19 23:14:47 +08:00
{
2020-04-21 12:36:23 +08:00
return core.UnsafeGetStatus();
}
public void OnCompleted(Action<object> continuation, object state, short token)
{
core.OnCompleted(continuation, state, token);
}
public bool MoveNext()
{
if (cancellationToken.IsCancellationRequested || !target.TryGetTarget(out var t)) // doesn't find = cancel.
2019-05-19 23:14:47 +08:00
{
2020-04-21 12:36:23 +08:00
core.TrySetCanceled(cancellationToken);
2019-05-19 23:14:47 +08:00
return false;
}
U nextValue = default(U);
try
{
nextValue = monitorFunction(t);
if (equalityComparer.Equals(currentValue, nextValue))
{
return true;
}
}
catch (Exception ex)
{
2020-04-21 12:36:23 +08:00
core.TrySetException(ex);
2019-05-19 23:14:47 +08:00
return false;
}
2020-04-21 12:36:23 +08:00
core.TrySetResult(nextValue);
2019-05-19 23:14:47 +08:00
return false;
}
2020-04-21 12:36:23 +08:00
public void Reset()
{
core.Reset();
target = default;
currentValue = default;
monitorFunction = default;
equalityComparer = default;
cancellationToken = default;
}
~WaitUntilValueChangedStandardObjectPromise()
{
if (pool.TryReturn(this))
{
GC.ReRegisterForFinalize(this);
}
}
2019-05-19 23:14:47 +08:00
}
}
}
#endif