complete except trigger
parent
f28743f7f6
commit
7bc9ef90f1
|
@ -22,17 +22,6 @@ public class SandboxMain : MonoBehaviour
|
|||
|
||||
void Start()
|
||||
{
|
||||
// Setup unobserverd tskexception handling
|
||||
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
|
||||
|
||||
// Optional: disable ExecutionContext if you don't use AsyncLocal.
|
||||
//if (!ExecutionContext.IsFlowSuppressed())
|
||||
//{
|
||||
// ExecutionContext.SuppressFlow();
|
||||
//}
|
||||
|
||||
//// Optional: disable SynchronizationContext(to boostup performance) if you completely use UniTask only
|
||||
//SynchronizationContext.SetSynchronizationContext(null);
|
||||
|
||||
// -----
|
||||
|
||||
|
@ -50,7 +39,7 @@ public class SandboxMain : MonoBehaviour
|
|||
{
|
||||
text.text = "";
|
||||
|
||||
ucs.TrySetResult();
|
||||
// ucs.TrySetResult();
|
||||
|
||||
await ucs.Task;
|
||||
});
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
fileFormatVersion: 2
|
||||
guid: fe7a4187b7f89f84582fd1e466a7f27e
|
||||
guid: 01d1404ca421466419a7db7340ff5e77
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
|
@ -35,9 +35,6 @@ namespace UniRx.Async
|
|||
IEnumerator innerEnumerator;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
Action continuation;
|
||||
ExceptionDispatchInfo exception;
|
||||
|
||||
UniTaskCompletionSourceCore<object> core;
|
||||
|
||||
EnumeratorPromise()
|
||||
|
@ -122,8 +119,6 @@ namespace UniRx.Async
|
|||
core.Reset();
|
||||
innerEnumerator = default;
|
||||
cancellationToken = default;
|
||||
continuation = default;
|
||||
exception = default;
|
||||
}
|
||||
|
||||
~EnumeratorPromise()
|
||||
|
|
|
@ -1,130 +0,0 @@
|
|||
#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;
|
||||
|
||||
namespace UniRx.Async.Internal
|
||||
{
|
||||
internal sealed class LazyPromise : IAwaiter
|
||||
{
|
||||
Func<UniTask> factory;
|
||||
UniTask value;
|
||||
|
||||
public LazyPromise(Func<UniTask> factory)
|
||||
{
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
void Create()
|
||||
{
|
||||
var f = Interlocked.Exchange(ref factory, null);
|
||||
if (f != null)
|
||||
{
|
||||
value = f();
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsCompleted
|
||||
{
|
||||
get
|
||||
{
|
||||
Create();
|
||||
return value.IsCompleted;
|
||||
}
|
||||
}
|
||||
|
||||
public UniTaskStatus Status
|
||||
{
|
||||
get
|
||||
{
|
||||
Create();
|
||||
return value.Status;
|
||||
}
|
||||
}
|
||||
|
||||
public void GetResult()
|
||||
{
|
||||
Create();
|
||||
value.GetResult();
|
||||
}
|
||||
|
||||
void IAwaiter.GetResult()
|
||||
{
|
||||
GetResult();
|
||||
}
|
||||
|
||||
public void UnsafeOnCompleted(Action continuation)
|
||||
{
|
||||
Create();
|
||||
value.GetAwaiter().UnsafeOnCompleted(continuation);
|
||||
}
|
||||
|
||||
public void OnCompleted(Action continuation)
|
||||
{
|
||||
UnsafeOnCompleted(continuation);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class LazyPromise<T> : IAwaiter<T>
|
||||
{
|
||||
Func<UniTask<T>> factory;
|
||||
UniTask<T> value;
|
||||
|
||||
public LazyPromise(Func<UniTask<T>> factory)
|
||||
{
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
void Create()
|
||||
{
|
||||
var f = Interlocked.Exchange(ref factory, null);
|
||||
if (f != null)
|
||||
{
|
||||
value = f();
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsCompleted
|
||||
{
|
||||
get
|
||||
{
|
||||
Create();
|
||||
return value.IsCompleted;
|
||||
}
|
||||
}
|
||||
|
||||
public UniTaskStatus Status
|
||||
{
|
||||
get
|
||||
{
|
||||
Create();
|
||||
return value.Status;
|
||||
}
|
||||
}
|
||||
|
||||
public T GetResult()
|
||||
{
|
||||
Create();
|
||||
return value.Result;
|
||||
}
|
||||
|
||||
void IAwaiter.GetResult()
|
||||
{
|
||||
GetResult();
|
||||
}
|
||||
|
||||
public void UnsafeOnCompleted(Action continuation)
|
||||
{
|
||||
Create();
|
||||
value.GetAwaiter().UnsafeOnCompleted(continuation);
|
||||
}
|
||||
|
||||
public void OnCompleted(Action continuation)
|
||||
{
|
||||
UnsafeOnCompleted(continuation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
|
@ -8,7 +8,8 @@ using System.Runtime.CompilerServices;
|
|||
namespace UniRx.Async.Internal
|
||||
{
|
||||
// optimized version of Standard Queue<T>.
|
||||
internal class MinimumQueue<T>
|
||||
// TODO: to internal.
|
||||
public class MinimumQueue<T>
|
||||
{
|
||||
const int MinimumGrow = 4;
|
||||
const int GrowFactor = 200;
|
||||
|
@ -28,7 +29,7 @@ namespace UniRx.Async.Internal
|
|||
public int Count
|
||||
{
|
||||
#if NET_4_6 || NET_STANDARD_2_0
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
#endif
|
||||
get { return size; }
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 8cc7fd65dd1433e419be4764aeb51391
|
||||
guid: 60cdf0bcaea36b444a7ae7263ae7598f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
|
@ -15,46 +15,6 @@ namespace UniRx.Async.Triggers
|
|||
bool TrySetCanceled();
|
||||
}
|
||||
|
||||
public class AsyncTriggerPromise<T> : ReusablePromise<T>, IPromise<T>, ICancelablePromise
|
||||
{
|
||||
public CancellationToken RegisteredCancellationToken { get; private set; }
|
||||
|
||||
public AsyncTriggerPromise()
|
||||
: this(CancellationToken.None)
|
||||
{
|
||||
}
|
||||
|
||||
public AsyncTriggerPromise(CancellationToken cancellationToken)
|
||||
{
|
||||
this.RegisteredCancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this);
|
||||
}
|
||||
|
||||
public override T GetResult()
|
||||
{
|
||||
if (Status == UniTaskStatus.Pending) return RawResult;
|
||||
return base.GetResult();
|
||||
}
|
||||
|
||||
public override bool TrySetResult(T result)
|
||||
{
|
||||
if (Status == UniTaskStatus.Pending)
|
||||
{
|
||||
// keep status as Pending.
|
||||
this.ForceSetResult(result);
|
||||
TryInvokeContinuation();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override bool TrySetCanceled()
|
||||
{
|
||||
if (Status == UniTaskStatus.Canceled) return false;
|
||||
TaskTracker.RemoveTracking(this);
|
||||
return base.TrySetCanceled();
|
||||
}
|
||||
}
|
||||
|
||||
public interface ICancellationTokenKeyDictionary
|
||||
{
|
||||
|
@ -62,7 +22,7 @@ namespace UniRx.Async.Triggers
|
|||
}
|
||||
|
||||
public class AsyncTriggerPromiseDictionary<TPromiseType> :
|
||||
Dictionary<CancellationToken, AsyncTriggerPromise<TPromiseType>>,
|
||||
Dictionary<CancellationToken, AutoResetUniTaskCompletionSource<TPromiseType>>,
|
||||
ICancellationTokenKeyDictionary,
|
||||
IEnumerable<ICancelablePromise>
|
||||
{
|
||||
|
@ -73,7 +33,9 @@ namespace UniRx.Async.Triggers
|
|||
|
||||
IEnumerator<ICancelablePromise> IEnumerable<ICancelablePromise>.GetEnumerator()
|
||||
{
|
||||
return Values.GetEnumerator();
|
||||
// TODO:
|
||||
throw new NotImplementedException();
|
||||
//return Values.GetEnumerator();
|
||||
}
|
||||
|
||||
void ICancellationTokenKeyDictionary.Remove(CancellationToken token)
|
||||
|
|
|
@ -2,8 +2,11 @@
|
|||
#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.Linq;
|
||||
using System.Threading;
|
||||
using UniRx.Async.Internal;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
|
@ -35,6 +38,162 @@ namespace UniRx.Async.Triggers
|
|||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// TODO:remove 2.
|
||||
public abstract class AsyncTriggerBase2 : MonoBehaviour
|
||||
{
|
||||
static readonly Action<object> Callback = CancelCallback;
|
||||
|
||||
bool calledAwake = false;
|
||||
bool destroyCalled = false;
|
||||
CancellationTokenRegistration[] registeredCancellations;
|
||||
int registeredCancellationsCount;
|
||||
|
||||
protected abstract IEnumerable<ICancelablePromise> GetPromises();
|
||||
|
||||
void Awake()
|
||||
{
|
||||
calledAwake = true;
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
if (destroyCalled) return;
|
||||
destroyCalled = true;
|
||||
foreach (var item in GetPromises())
|
||||
{
|
||||
item.TrySetCanceled();
|
||||
}
|
||||
if (registeredCancellations != null)
|
||||
{
|
||||
for (int i = 0; i < registeredCancellationsCount; i++)
|
||||
{
|
||||
registeredCancellations[i].Dispose();
|
||||
registeredCancellations[i] = default(CancellationTokenRegistration);
|
||||
}
|
||||
ArrayPool<CancellationTokenRegistration>.Shared.Return(registeredCancellations);
|
||||
}
|
||||
}
|
||||
|
||||
protected void TrySetResult<T>(MinimumQueue<UniTaskCompletionSource<AsyncUnit>> promise, AsyncTriggerPromiseDictionary<T> promises, T result)
|
||||
{
|
||||
if (promise != null)
|
||||
{
|
||||
// TODO:
|
||||
}
|
||||
|
||||
if (promises != null)
|
||||
{
|
||||
PromiseHelper.TrySetResultAll(promises.Values, result);
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask<T> CreatePromise<T>(ref MinimumQueue<AutoResetUniTaskCompletionSource<T>> promise, ref AsyncTriggerPromiseDictionary<T> promises, CancellationToken cancellationToken)
|
||||
{
|
||||
if (destroyCalled) return UniTask.FromCanceled<T>();
|
||||
|
||||
if (!calledAwake)
|
||||
{
|
||||
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this));
|
||||
}
|
||||
|
||||
if (!cancellationToken.CanBeCanceled)
|
||||
{
|
||||
if (promise == null)
|
||||
{
|
||||
promise = new MinimumQueue<AutoResetUniTaskCompletionSource<T>>(4); // kakko kari.(ArrayPool?)
|
||||
}
|
||||
|
||||
var tcs = AutoResetUniTaskCompletionSource<T>.Create();
|
||||
promise.Enqueue(tcs);
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
|
||||
CancellationTokenRegistration registrationToken = default;
|
||||
// TODO:atode.
|
||||
// var registrationToken = cancellationToken.RegisterWithoutCaptureExecutionContext(Callback, Tuple.Create((ICancellationTokenKeyDictionary)promises, (ICancelablePromise)cancellablePromise));
|
||||
if (registeredCancellations == null)
|
||||
{
|
||||
registeredCancellations = ArrayPool<CancellationTokenRegistration>.Shared.Rent(4);
|
||||
}
|
||||
|
||||
ArrayPoolUtil.EnsureCapacity(ref registeredCancellations, registeredCancellationsCount + 1, ArrayPool<CancellationTokenRegistration>.Shared);
|
||||
registeredCancellations[registeredCancellationsCount++] = registrationToken;
|
||||
|
||||
// TODO:<3A>ªuse at registration
|
||||
{
|
||||
if (promises == null)
|
||||
{
|
||||
promises = new AsyncTriggerPromiseDictionary<T>();
|
||||
}
|
||||
|
||||
var tcs = AutoResetUniTaskCompletionSource<T>.Create();
|
||||
promises.Add(cancellationToken, tcs);
|
||||
return tcs.Task;
|
||||
}
|
||||
}
|
||||
|
||||
static void CancelCallback(object state)
|
||||
{
|
||||
// TODO:nantokasuru.
|
||||
|
||||
//var tuple = (Tuple<ICancellationTokenKeyDictionary, ICancelablePromise>)state;
|
||||
//var dict = tuple.Item1;
|
||||
//var promise = tuple.Item2;
|
||||
|
||||
//promise.TrySetCanceled();
|
||||
//dict.Remove(promise.RegisteredCancellationToken);
|
||||
}
|
||||
|
||||
class AwakeMonitor : IPlayerLoopItem
|
||||
{
|
||||
readonly AsyncTriggerBase2 trigger;
|
||||
|
||||
public AwakeMonitor(AsyncTriggerBase2 trigger)
|
||||
{
|
||||
this.trigger = trigger;
|
||||
}
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (trigger.calledAwake) return false;
|
||||
if (trigger == null)
|
||||
{
|
||||
trigger.OnDestroy();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// TODO:remove 2.
|
||||
[DisallowMultipleComponent]
|
||||
public class AsyncUpdateTrigger2 : AsyncTriggerBase2
|
||||
{
|
||||
MinimumQueue<UniTaskCompletionSource<AsyncUnit>> promise;
|
||||
AsyncTriggerPromiseDictionary<AsyncUnit> promises;
|
||||
|
||||
protected override IEnumerable<ICancelablePromise> GetPromises()
|
||||
{
|
||||
// TODO:
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// TrySetResult
|
||||
}
|
||||
|
||||
public UniTask UpdateAsync(CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
return CreatePromise<AsyncUnit>(ref promise, ref promises, cancellationToken).AsUniTask();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 5110117231c8a6d4095fd0cbd3f4c142
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2c2ecfd98ee1b9a4c9fae1b398c32d75
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 13d604ac281570c4eac9962429f19ca9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 71c2c0bce7543454c8ef545083e18170
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4b4ff020f73dc6d4b8ebd4760d61fb43
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9a75b5d7e55a5a34fb6476586df37c72
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -86,12 +86,12 @@ namespace UniRx.Async
|
|||
}
|
||||
}
|
||||
|
||||
static async UniTaskVoid Fire(AsyncSubject<object> subject, UniTask task)
|
||||
static async UniTaskVoid Fire(AsyncSubject<AsyncUnit> subject, UniTask task)
|
||||
{
|
||||
try
|
||||
{
|
||||
await task;
|
||||
subject.OnNext(null);
|
||||
subject.OnNext(AsyncUnit.Default);
|
||||
subject.OnCompleted();
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2edd588bb09eb0a4695d039d6a1f02b2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,234 @@
|
|||
<#@ template debug="false" hostspecific="false" language="C#" #>
|
||||
<#@ assembly name="System.Core" #>
|
||||
<#@ import namespace="System.Linq" #>
|
||||
<#@ import namespace="System.Text" #>
|
||||
<#@ import namespace="System.Collections.Generic" #>
|
||||
<#@ output extension=".cs" #>
|
||||
<#
|
||||
var types = new (string typeName, string returnType, string returnField)[]
|
||||
{
|
||||
("AsyncOperation", "void", null),
|
||||
("ResourceRequest", "UnityEngine.Object", "asset"),
|
||||
("AssetBundleRequest", "UnityEngine.Object", "asset"), // allAssets?
|
||||
("AssetBundleCreateRequest", "AssetBundle", "assetBundle"),
|
||||
("UnityWebRequestAsyncOperation", "UnityWebRequest", "webRequest") // -> #if ENABLE_UNITYWEBREQUEST
|
||||
};
|
||||
|
||||
Func<string, string> ToUniTaskReturnType = x => (x == "void") ? "UniTask" : $"UniTask<{x}>";
|
||||
Func<string, string> ToIUniTaskSourceReturnType = x => (x == "void") ? "IUniTaskSource" : $"IUniTaskSource<{x}>";
|
||||
Func<(string typeName, string returnType, string returnField), bool> IsVoid = x => x.returnType == "void";
|
||||
#>
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
using UnityEngine;
|
||||
using UniRx.Async.Internal;
|
||||
#if ENABLE_UNITYWEBREQUEST
|
||||
using UnityEngine.Networking;
|
||||
#endif
|
||||
|
||||
namespace UniRx.Async
|
||||
{
|
||||
public static partial class UnityAsyncExtensions
|
||||
{
|
||||
<# foreach(var t in types) { #>
|
||||
<# if(t.returnType == "UnityWebRequest") { #>
|
||||
#if ENABLE_UNITYWEBREQUEST
|
||||
<# } #>
|
||||
#region <#= t.typeName #>
|
||||
|
||||
public static <#= t.typeName #>Awaiter GetAwaiter(this <#= t.typeName #> asyncOperation)
|
||||
{
|
||||
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
||||
return new <#= t.typeName #>Awaiter(asyncOperation);
|
||||
}
|
||||
|
||||
public static <#= ToUniTaskReturnType(t.returnType) #> ToUniTask(this <#= t.typeName #> asyncOperation)
|
||||
{
|
||||
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
||||
|
||||
return new <#= ToUniTaskReturnType(t.returnType) #>(<#= t.typeName #>ConfiguredSource.Create(asyncOperation, PlayerLoopTiming.Update, null, CancellationToken.None, out var token), token);
|
||||
}
|
||||
|
||||
public static <#= ToUniTaskReturnType(t.returnType) #> ConfigureAwait(this <#= t.typeName #> asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellation = default(CancellationToken))
|
||||
{
|
||||
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
||||
|
||||
return new <#= ToUniTaskReturnType(t.returnType) #>(<#= t.typeName #>ConfiguredSource.Create(asyncOperation, timing, progress, cancellation, out var token), token);
|
||||
}
|
||||
|
||||
public struct <#= t.typeName #>Awaiter : ICriticalNotifyCompletion
|
||||
{
|
||||
<#= t.typeName #> asyncOperation;
|
||||
Action<AsyncOperation> continuationAction;
|
||||
<# if (!IsVoid(t)) { #>
|
||||
<#= t.returnType #> result;
|
||||
<# } #>
|
||||
|
||||
public <#= t.typeName #>Awaiter(<#= t.typeName #> asyncOperation)
|
||||
{
|
||||
this.asyncOperation = asyncOperation;
|
||||
this.continuationAction = null;
|
||||
<# if (!IsVoid(t)) { #>
|
||||
this.result = default;
|
||||
<# } #>
|
||||
}
|
||||
|
||||
public bool IsCompleted => asyncOperation.isDone;
|
||||
|
||||
public <#= t.returnType #> GetResult()
|
||||
{
|
||||
if (continuationAction != null)
|
||||
{
|
||||
asyncOperation.completed -= continuationAction;
|
||||
asyncOperation = null; // remove reference.
|
||||
continuationAction = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
asyncOperation = null; // remove reference.
|
||||
}
|
||||
|
||||
<# if (!IsVoid(t)) { #>
|
||||
return this.result;
|
||||
<# } #>
|
||||
}
|
||||
|
||||
public void OnCompleted(Action continuation)
|
||||
{
|
||||
UnsafeOnCompleted(continuation);
|
||||
}
|
||||
|
||||
public void UnsafeOnCompleted(Action continuation)
|
||||
{
|
||||
Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction);
|
||||
continuationAction = continuation.AsFuncOfT<AsyncOperation>(); // allocate delegate.
|
||||
asyncOperation.completed += continuationAction;
|
||||
}
|
||||
}
|
||||
|
||||
class <#= t.typeName #>ConfiguredSource : <#= ToIUniTaskSourceReturnType(t.returnType) #>, IPlayerLoopItem, IPromisePoolItem
|
||||
{
|
||||
static readonly PromisePool<<#= t.typeName #>ConfiguredSource> pool = new PromisePool<<#= t.typeName #>ConfiguredSource>();
|
||||
|
||||
<#= t.typeName #> asyncOperation;
|
||||
IProgress<float> progress;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
UniTaskCompletionSourceCore<<#= IsVoid(t) ? "AsyncUnit" : t.returnType #>> core;
|
||||
|
||||
<#= t.typeName #>ConfiguredSource()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public static <#= ToIUniTaskSourceReturnType(t.returnType) #> Create(<#= t.typeName #> asyncOperation, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, out short token)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return AutoResetUniTaskCompletionSource<#= IsVoid(t) ? "" : $"<{t.returnType}>" #>.CreateFromCanceled(cancellationToken, out token);
|
||||
}
|
||||
|
||||
var result = pool.TryRent() ?? new <#= t.typeName #>ConfiguredSource();
|
||||
|
||||
result.asyncOperation = asyncOperation;
|
||||
result.progress = progress;
|
||||
result.cancellationToken = cancellationToken;
|
||||
|
||||
TaskTracker2.TrackActiveTask(result, 3);
|
||||
|
||||
PlayerLoopHelper.AddAction(timing, result);
|
||||
|
||||
token = result.core.Version;
|
||||
return result;
|
||||
}
|
||||
|
||||
public <#= t.returnType #> GetResult(short token)
|
||||
{
|
||||
try
|
||||
{
|
||||
TaskTracker2.RemoveTracking(this);
|
||||
|
||||
<# if (!IsVoid(t)) { #>
|
||||
return core.GetResult(token);
|
||||
<# } else { #>
|
||||
core.GetResult(token);
|
||||
<# } #>
|
||||
}
|
||||
finally
|
||||
{
|
||||
pool.TryReturn(this);
|
||||
}
|
||||
}
|
||||
|
||||
<# if (!IsVoid(t)) { #>
|
||||
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)
|
||||
{
|
||||
core.TrySetCanceled(cancellationToken);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (progress != null)
|
||||
{
|
||||
progress.Report(asyncOperation.progress);
|
||||
}
|
||||
|
||||
if (asyncOperation.isDone)
|
||||
{
|
||||
core.TrySetResult(<#= IsVoid(t) ? "AsyncUnit.Default" : $"asyncOperation.{t.returnField}" #>);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
core.Reset();
|
||||
asyncOperation = default;
|
||||
progress = default;
|
||||
cancellationToken = default;
|
||||
}
|
||||
|
||||
~<#= t.typeName #>ConfiguredSource()
|
||||
{
|
||||
if (pool.TryReturn(this))
|
||||
{
|
||||
GC.ReRegisterForFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# endregion
|
||||
<# if(t.returnType == "UnityWebRequest") { #>
|
||||
#endif
|
||||
<# } #>
|
||||
|
||||
<# } #>
|
||||
}
|
||||
}
|
|
@ -1,14 +1,11 @@
|
|||
#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
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
using UniRx.Async.Internal;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.UI;
|
||||
using UniRx.Async.Triggers;
|
||||
|
||||
namespace UniRx.Async
|
||||
{
|
||||
|
@ -168,86 +165,72 @@ namespace UniRx.Async
|
|||
public interface IAsyncClickEventHandler : IDisposable
|
||||
{
|
||||
UniTask OnClickAsync();
|
||||
UniTask<bool> OnClickAsyncSuppressCancellationThrow();
|
||||
}
|
||||
|
||||
public interface IAsyncValueChangedEventHandler<T> : IDisposable
|
||||
{
|
||||
UniTask<T> OnValueChangedAsync();
|
||||
UniTask<(bool IsCanceled, T Result)> OnValueChangedAsyncSuppressCancellationThrow();
|
||||
}
|
||||
|
||||
public interface IAsyncEndEditEventHandler<T> : IDisposable
|
||||
{
|
||||
UniTask<T> OnEndEditAsync();
|
||||
UniTask<(bool IsCanceled, T Result)> OnEndEditAsyncSuppressCancellationThrow();
|
||||
}
|
||||
|
||||
// event handler is reusable when callOnce = false.
|
||||
public class AsyncUnityEventHandler : IAwaiter, IDisposable, IAsyncClickEventHandler
|
||||
public class AsyncUnityEventHandler : IUniTaskSource, IDisposable, IAsyncClickEventHandler
|
||||
{
|
||||
static Action<object> cancellationCallback = CancellationCallback;
|
||||
|
||||
readonly UnityAction action;
|
||||
readonly UnityEvent unityEvent;
|
||||
Action continuation;
|
||||
|
||||
CancellationToken cancellationToken;
|
||||
CancellationTokenRegistration registration;
|
||||
bool isDisposed;
|
||||
bool callOnce;
|
||||
UniTask<bool>? suppressCancellationThrowTask;
|
||||
|
||||
UniTaskCompletionSourceCore<AsyncUnit> core;
|
||||
|
||||
public AsyncUnityEventHandler(UnityEvent unityEvent, CancellationToken cancellationToken, bool callOnce)
|
||||
{
|
||||
this.callOnce = callOnce;
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
isDisposed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
action = Invoke;
|
||||
unityEvent.AddListener(action);
|
||||
this.action = Invoke;
|
||||
this.unityEvent = unityEvent;
|
||||
this.cancellationToken = cancellationToken;
|
||||
this.callOnce = callOnce;
|
||||
|
||||
unityEvent.AddListener(action);
|
||||
|
||||
if (cancellationToken.CanBeCanceled)
|
||||
{
|
||||
registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this);
|
||||
}
|
||||
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
TaskTracker2.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public UniTask OnInvokeAsync()
|
||||
{
|
||||
// zero allocation wait handler.
|
||||
return new UniTask(this);
|
||||
}
|
||||
|
||||
public UniTask<bool> OnInvokeAsyncSuppressCancellationThrow()
|
||||
{
|
||||
if (suppressCancellationThrowTask == null)
|
||||
{
|
||||
suppressCancellationThrowTask = OnInvokeAsync().SuppressCancellationThrow();
|
||||
}
|
||||
return suppressCancellationThrowTask.Value;
|
||||
core.Reset();
|
||||
return new UniTask(this, core.Version);
|
||||
}
|
||||
|
||||
void Invoke()
|
||||
{
|
||||
var c = continuation;
|
||||
continuation = null;
|
||||
if (c != null)
|
||||
{
|
||||
c.Invoke();
|
||||
}
|
||||
core.TrySetResult(AsyncUnit.Default);
|
||||
}
|
||||
|
||||
static void CancellationCallback(object state)
|
||||
{
|
||||
var self = (AsyncUnityEventHandler)state;
|
||||
self.Dispose();
|
||||
self.Invoke(); // call continuation if exists yet(GetResult -> throw OperationCanceledException).
|
||||
|
||||
self.core.TrySetCanceled(self.cancellationToken);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
@ -255,7 +238,7 @@ namespace UniRx.Async
|
|||
if (!isDisposed)
|
||||
{
|
||||
isDisposed = true;
|
||||
TaskTracker.RemoveTracking(this);
|
||||
TaskTracker2.RemoveTracking(this);
|
||||
registration.Dispose();
|
||||
if (unityEvent != null)
|
||||
{
|
||||
|
@ -264,106 +247,96 @@ namespace UniRx.Async
|
|||
}
|
||||
}
|
||||
|
||||
bool IAwaiter.IsCompleted => isDisposed ? true : false;
|
||||
UniTaskStatus IAwaiter.Status => isDisposed ? UniTaskStatus.Canceled : UniTaskStatus.Pending;
|
||||
void IAwaiter.GetResult()
|
||||
{
|
||||
if (isDisposed) throw new OperationCanceledException();
|
||||
if (callOnce) Dispose();
|
||||
}
|
||||
|
||||
void INotifyCompletion.OnCompleted(Action action)
|
||||
{
|
||||
((ICriticalNotifyCompletion)this).UnsafeOnCompleted(action);
|
||||
}
|
||||
|
||||
void ICriticalNotifyCompletion.UnsafeOnCompleted(Action action)
|
||||
{
|
||||
Error.ThrowWhenContinuationIsAlreadyRegistered(this.continuation);
|
||||
this.continuation = action;
|
||||
}
|
||||
|
||||
// Interface events.
|
||||
|
||||
UniTask IAsyncClickEventHandler.OnClickAsync()
|
||||
{
|
||||
return OnInvokeAsync();
|
||||
}
|
||||
|
||||
UniTask<bool> IAsyncClickEventHandler.OnClickAsyncSuppressCancellationThrow()
|
||||
void IUniTaskSource.GetResult(short token)
|
||||
{
|
||||
return OnInvokeAsyncSuppressCancellationThrow();
|
||||
try
|
||||
{
|
||||
core.GetResult(token);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (callOnce)
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UniTaskStatus IUniTaskSource.GetStatus(short token)
|
||||
{
|
||||
return core.GetStatus(token);
|
||||
}
|
||||
|
||||
UniTaskStatus IUniTaskSource.UnsafeGetStatus()
|
||||
{
|
||||
return core.UnsafeGetStatus();
|
||||
}
|
||||
|
||||
void IUniTaskSource.OnCompleted(Action<object> continuation, object state, short token)
|
||||
{
|
||||
core.OnCompleted(continuation, state, token);
|
||||
}
|
||||
}
|
||||
|
||||
// event handler is reusable when callOnce = false.
|
||||
public class AsyncUnityEventHandler<T> : IAwaiter<T>, IDisposable, IAsyncValueChangedEventHandler<T>, IAsyncEndEditEventHandler<T>
|
||||
public class AsyncUnityEventHandler<T> : IUniTaskSource<T>, IDisposable, IAsyncValueChangedEventHandler<T>, IAsyncEndEditEventHandler<T>
|
||||
{
|
||||
static Action<object> cancellationCallback = CancellationCallback;
|
||||
|
||||
readonly UnityAction<T> action;
|
||||
readonly UnityEvent<T> unityEvent;
|
||||
Action continuation;
|
||||
|
||||
CancellationToken cancellationToken;
|
||||
CancellationTokenRegistration registration;
|
||||
bool isDisposed;
|
||||
T eventValue;
|
||||
bool callOnce;
|
||||
UniTask<(bool, T)>? suppressCancellationThrowTask;
|
||||
|
||||
UniTaskCompletionSourceCore<T> core;
|
||||
|
||||
public AsyncUnityEventHandler(UnityEvent<T> unityEvent, CancellationToken cancellationToken, bool callOnce)
|
||||
{
|
||||
this.callOnce = callOnce;
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
isDisposed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
action = Invoke;
|
||||
unityEvent.AddListener(action);
|
||||
this.action = Invoke;
|
||||
this.unityEvent = unityEvent;
|
||||
this.cancellationToken = cancellationToken;
|
||||
this.callOnce = callOnce;
|
||||
|
||||
unityEvent.AddListener(action);
|
||||
|
||||
if (cancellationToken.CanBeCanceled)
|
||||
{
|
||||
registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this);
|
||||
}
|
||||
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
TaskTracker2.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public UniTask<T> OnInvokeAsync()
|
||||
{
|
||||
// zero allocation wait handler.
|
||||
return new UniTask<T>(this);
|
||||
core.Reset();
|
||||
return new UniTask<T>(this, core.Version);
|
||||
}
|
||||
|
||||
public UniTask<(bool IsCanceled, T Result)> OnInvokeAsyncSuppressCancellationThrow()
|
||||
void Invoke(T result)
|
||||
{
|
||||
if (suppressCancellationThrowTask == null)
|
||||
{
|
||||
suppressCancellationThrowTask = OnInvokeAsync().SuppressCancellationThrow();
|
||||
}
|
||||
return suppressCancellationThrowTask.Value;
|
||||
}
|
||||
|
||||
void Invoke(T value)
|
||||
{
|
||||
this.eventValue = value;
|
||||
|
||||
var c = continuation;
|
||||
continuation = null;
|
||||
if (c != null)
|
||||
{
|
||||
c.Invoke();
|
||||
}
|
||||
core.TrySetResult(result);
|
||||
}
|
||||
|
||||
static void CancellationCallback(object state)
|
||||
{
|
||||
var self = (AsyncUnityEventHandler<T>)state;
|
||||
self.Dispose();
|
||||
self.Invoke(default(T)); // call continuation if exists yet(GetResult -> throw OperationCanceledException).
|
||||
|
||||
self.core.TrySetCanceled(self.cancellationToken);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
@ -371,7 +344,7 @@ namespace UniRx.Async
|
|||
if (!isDisposed)
|
||||
{
|
||||
isDisposed = true;
|
||||
TaskTracker.RemoveTracking(this);
|
||||
TaskTracker2.RemoveTracking(this);
|
||||
registration.Dispose();
|
||||
if (unityEvent != null)
|
||||
{
|
||||
|
@ -380,55 +353,49 @@ namespace UniRx.Async
|
|||
}
|
||||
}
|
||||
|
||||
bool IAwaiter.IsCompleted => isDisposed ? true : false;
|
||||
UniTaskStatus IAwaiter.Status => isDisposed ? UniTaskStatus.Canceled : UniTaskStatus.Pending;
|
||||
|
||||
T IAwaiter<T>.GetResult()
|
||||
{
|
||||
if (isDisposed) throw new OperationCanceledException();
|
||||
if (callOnce) Dispose();
|
||||
return eventValue;
|
||||
}
|
||||
|
||||
void IAwaiter.GetResult()
|
||||
{
|
||||
if (isDisposed) throw new OperationCanceledException();
|
||||
if (callOnce) Dispose();
|
||||
}
|
||||
|
||||
void INotifyCompletion.OnCompleted(Action action)
|
||||
{
|
||||
((ICriticalNotifyCompletion)this).UnsafeOnCompleted(action);
|
||||
}
|
||||
|
||||
void ICriticalNotifyCompletion.UnsafeOnCompleted(Action action)
|
||||
{
|
||||
Error.ThrowWhenContinuationIsAlreadyRegistered(this.continuation);
|
||||
this.continuation = action;
|
||||
}
|
||||
|
||||
// Interface events.
|
||||
|
||||
UniTask<T> IAsyncValueChangedEventHandler<T>.OnValueChangedAsync()
|
||||
{
|
||||
return OnInvokeAsync();
|
||||
}
|
||||
|
||||
UniTask<(bool IsCanceled, T Result)> IAsyncValueChangedEventHandler<T>.OnValueChangedAsyncSuppressCancellationThrow()
|
||||
{
|
||||
return OnInvokeAsyncSuppressCancellationThrow();
|
||||
}
|
||||
|
||||
UniTask<T> IAsyncEndEditEventHandler<T>.OnEndEditAsync()
|
||||
{
|
||||
return OnInvokeAsync();
|
||||
}
|
||||
|
||||
UniTask<(bool IsCanceled, T Result)> IAsyncEndEditEventHandler<T>.OnEndEditAsyncSuppressCancellationThrow()
|
||||
T IUniTaskSource<T>.GetResult(short token)
|
||||
{
|
||||
return OnInvokeAsyncSuppressCancellationThrow();
|
||||
try
|
||||
{
|
||||
return core.GetResult(token);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (callOnce)
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void IUniTaskSource.GetResult(short token)
|
||||
{
|
||||
((IUniTaskSource<T>)this).GetResult(token);
|
||||
}
|
||||
|
||||
UniTaskStatus IUniTaskSource.GetStatus(short token)
|
||||
{
|
||||
return core.GetStatus(token);
|
||||
}
|
||||
|
||||
UniTaskStatus IUniTaskSource.UnsafeGetStatus()
|
||||
{
|
||||
return core.UnsafeGetStatus();
|
||||
}
|
||||
|
||||
void IUniTaskSource.OnCompleted(Action<object> continuation, object state, short token)
|
||||
{
|
||||
core.OnCompleted(continuation, state, token);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
Loading…
Reference in New Issue