123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582 |
- #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using Cysharp.Threading.Tasks.Internal;
- namespace Cysharp.Threading.Tasks
- {
- public partial struct UniTask
- {
- public static UniTask WaitUntil(Func<bool> predicate, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
- {
- return new UniTask(WaitUntilPromise.Create(predicate, timing, cancellationToken, out var token), token);
- }
- public static UniTask WaitWhile(Func<bool> predicate, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
- {
- return new UniTask(WaitWhilePromise.Create(predicate, timing, cancellationToken, out var token), token);
- }
- public static UniTask WaitUntilCanceled(CancellationToken cancellationToken, PlayerLoopTiming timing = PlayerLoopTiming.Update)
- {
- return new UniTask(WaitUntilCanceledPromise.Create(cancellationToken, timing, out var token), token);
- }
- 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 = target is UnityEngine.Object; // don't use (unityObject == null)
- 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);
- }
- sealed class WaitUntilPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<WaitUntilPromise>
- {
- static TaskPool<WaitUntilPromise> pool;
- WaitUntilPromise nextNode;
- public ref WaitUntilPromise NextNode => ref nextNode;
- static WaitUntilPromise()
- {
- TaskPool.RegisterSizeGetter(typeof(WaitUntilPromise), () => pool.Size);
- }
- Func<bool> predicate;
- CancellationToken cancellationToken;
- UniTaskCompletionSourceCore<object> core;
- WaitUntilPromise()
- {
- }
- public static IUniTaskSource Create(Func<bool> predicate, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token)
- {
- if (cancellationToken.IsCancellationRequested)
- {
- return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
- }
- if (!pool.TryPop(out var result))
- {
- result = new WaitUntilPromise();
- }
- result.predicate = predicate;
- result.cancellationToken = cancellationToken;
- TaskTracker.TrackActiveTask(result, 3);
- PlayerLoopHelper.AddAction(timing, result);
- token = result.core.Version;
- return result;
- }
- public void GetResult(short token)
- {
- try
- {
- core.GetResult(token);
- }
- finally
- {
- TryReturn();
- }
- }
- 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()
- {
- if (cancellationToken.IsCancellationRequested)
- {
- core.TrySetCanceled(cancellationToken);
- return false;
- }
- try
- {
- if (!predicate())
- {
- return true;
- }
- }
- catch (Exception ex)
- {
- core.TrySetException(ex);
- return false;
- }
- core.TrySetResult(null);
- return false;
- }
- bool TryReturn()
- {
- TaskTracker.RemoveTracking(this);
- core.Reset();
- predicate = default;
- cancellationToken = default;
- return pool.TryPush(this);
- }
- }
- sealed class WaitWhilePromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<WaitWhilePromise>
- {
- static TaskPool<WaitWhilePromise> pool;
- WaitWhilePromise nextNode;
- public ref WaitWhilePromise NextNode => ref nextNode;
- static WaitWhilePromise()
- {
- TaskPool.RegisterSizeGetter(typeof(WaitWhilePromise), () => pool.Size);
- }
- Func<bool> predicate;
- CancellationToken cancellationToken;
- UniTaskCompletionSourceCore<object> core;
- WaitWhilePromise()
- {
- }
- public static IUniTaskSource Create(Func<bool> predicate, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token)
- {
- if (cancellationToken.IsCancellationRequested)
- {
- return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
- }
- if (!pool.TryPop(out var result))
- {
- result = new WaitWhilePromise();
- }
- result.predicate = predicate;
- result.cancellationToken = cancellationToken;
- TaskTracker.TrackActiveTask(result, 3);
- PlayerLoopHelper.AddAction(timing, result);
- token = result.core.Version;
- return result;
- }
- public void GetResult(short token)
- {
- try
- {
- core.GetResult(token);
- }
- finally
- {
- TryReturn();
- }
- }
- 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()
- {
- if (cancellationToken.IsCancellationRequested)
- {
- core.TrySetCanceled(cancellationToken);
- return false;
- }
- try
- {
- if (predicate())
- {
- return true;
- }
- }
- catch (Exception ex)
- {
- core.TrySetException(ex);
- return false;
- }
- core.TrySetResult(null);
- return false;
- }
- bool TryReturn()
- {
- TaskTracker.RemoveTracking(this);
- core.Reset();
- predicate = default;
- cancellationToken = default;
- return pool.TryPush(this);
- }
- }
- sealed class WaitUntilCanceledPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<WaitUntilCanceledPromise>
- {
- static TaskPool<WaitUntilCanceledPromise> pool;
- WaitUntilCanceledPromise nextNode;
- public ref WaitUntilCanceledPromise NextNode => ref nextNode;
- static WaitUntilCanceledPromise()
- {
- TaskPool.RegisterSizeGetter(typeof(WaitUntilCanceledPromise), () => pool.Size);
- }
- CancellationToken cancellationToken;
- UniTaskCompletionSourceCore<object> core;
- WaitUntilCanceledPromise()
- {
- }
- public static IUniTaskSource Create(CancellationToken cancellationToken, PlayerLoopTiming timing, out short token)
- {
- if (cancellationToken.IsCancellationRequested)
- {
- return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
- }
- if (!pool.TryPop(out var result))
- {
- result = new WaitUntilCanceledPromise();
- }
- result.cancellationToken = cancellationToken;
- TaskTracker.TrackActiveTask(result, 3);
- PlayerLoopHelper.AddAction(timing, result);
- token = result.core.Version;
- return result;
- }
- public void GetResult(short token)
- {
- try
- {
- core.GetResult(token);
- }
- finally
- {
- TryReturn();
- }
- }
- 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()
- {
- if (cancellationToken.IsCancellationRequested)
- {
- core.TrySetResult(null);
- return false;
- }
- return true;
- }
- bool TryReturn()
- {
- TaskTracker.RemoveTracking(this);
- core.Reset();
- cancellationToken = default;
- return pool.TryPush(this);
- }
- }
- // where T : UnityEngine.Object, can not add constraint
- sealed class WaitUntilValueChangedUnityObjectPromise<T, U> : IUniTaskSource<U>, IPlayerLoopItem, ITaskPoolNode<WaitUntilValueChangedUnityObjectPromise<T, U>>
- {
- static TaskPool<WaitUntilValueChangedUnityObjectPromise<T, U>> pool;
- WaitUntilValueChangedUnityObjectPromise<T, U> nextNode;
- public ref WaitUntilValueChangedUnityObjectPromise<T, U> NextNode => ref nextNode;
- static WaitUntilValueChangedUnityObjectPromise()
- {
- TaskPool.RegisterSizeGetter(typeof(WaitUntilValueChangedUnityObjectPromise<T, U>), () => pool.Size);
- }
- T target;
- UnityEngine.Object targetAsUnityObject;
- U currentValue;
- Func<T, U> monitorFunction;
- IEqualityComparer<U> equalityComparer;
- CancellationToken cancellationToken;
- UniTaskCompletionSourceCore<U> core;
- WaitUntilValueChangedUnityObjectPromise()
- {
- }
- 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);
- }
- if (!pool.TryPop(out var result))
- {
- result = new WaitUntilValueChangedUnityObjectPromise<T, U>();
- }
- result.target = target;
- result.targetAsUnityObject = target as UnityEngine.Object;
- result.monitorFunction = monitorFunction;
- result.currentValue = monitorFunction(target);
- result.equalityComparer = equalityComparer ?? UnityEqualityComparer.GetDefault<U>();
- result.cancellationToken = cancellationToken;
- TaskTracker.TrackActiveTask(result, 3);
- PlayerLoopHelper.AddAction(timing, result);
- token = result.core.Version;
- return result;
- }
- public U GetResult(short token)
- {
- try
- {
- return core.GetResult(token);
- }
- finally
- {
- TryReturn();
- }
- }
- 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()
- {
- if (cancellationToken.IsCancellationRequested || targetAsUnityObject == null) // destroyed = cancel.
- {
- core.TrySetCanceled(cancellationToken);
- return false;
- }
- U nextValue = default(U);
- try
- {
- nextValue = monitorFunction(target);
- if (equalityComparer.Equals(currentValue, nextValue))
- {
- return true;
- }
- }
- catch (Exception ex)
- {
- core.TrySetException(ex);
- return false;
- }
- core.TrySetResult(nextValue);
- return false;
- }
- bool TryReturn()
- {
- TaskTracker.RemoveTracking(this);
- core.Reset();
- target = default;
- currentValue = default;
- monitorFunction = default;
- equalityComparer = default;
- cancellationToken = default;
- return pool.TryPush(this);
- }
- }
- sealed class WaitUntilValueChangedStandardObjectPromise<T, U> : IUniTaskSource<U>, IPlayerLoopItem, ITaskPoolNode<WaitUntilValueChangedStandardObjectPromise<T, U>>
- where T : class
- {
- static TaskPool<WaitUntilValueChangedStandardObjectPromise<T, U>> pool;
- WaitUntilValueChangedStandardObjectPromise<T, U> nextNode;
- public ref WaitUntilValueChangedStandardObjectPromise<T, U> NextNode => ref nextNode;
- static WaitUntilValueChangedStandardObjectPromise()
- {
- TaskPool.RegisterSizeGetter(typeof(WaitUntilValueChangedStandardObjectPromise<T, U>), () => pool.Size);
- }
- WeakReference<T> target;
- U currentValue;
- 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);
- }
- if (!pool.TryPop(out var result))
- {
- result = 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);
- PlayerLoopHelper.AddAction(timing, result);
- token = result.core.Version;
- return result;
- }
- public U GetResult(short token)
- {
- try
- {
- return core.GetResult(token);
- }
- finally
- {
- TryReturn();
- }
- }
- 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()
- {
- if (cancellationToken.IsCancellationRequested || !target.TryGetTarget(out var t)) // doesn't find = cancel.
- {
- core.TrySetCanceled(cancellationToken);
- return false;
- }
- U nextValue = default(U);
- try
- {
- nextValue = monitorFunction(t);
- if (equalityComparer.Equals(currentValue, nextValue))
- {
- return true;
- }
- }
- catch (Exception ex)
- {
- core.TrySetException(ex);
- return false;
- }
- core.TrySetResult(nextValue);
- return false;
- }
- bool TryReturn()
- {
- TaskTracker.RemoveTracking(this);
- core.Reset();
- target = default;
- currentValue = default;
- monitorFunction = default;
- equalityComparer = default;
- cancellationToken = default;
- return pool.TryPush(this);
- }
- }
- }
- }
|