From 547b700ba7c2c5e70b392ec1cae79f93a294540a Mon Sep 17 00:00:00 2001 From: neuecc Date: Thu, 16 Jul 2020 06:12:39 +0900 Subject: [PATCH] AsyncOperation/Addressables returns Cacnceled UniTask when already canceled. --- .../AddressablesAsyncExtensions.cs | 4 ++++ .../UniTask/Runtime/UnityAsyncExtensions.cs | 20 ++++++++++++++----- .../UniTask/Runtime/UnityAsyncExtensions.tt | 2 ++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/External/Addressables/AddressablesAsyncExtensions.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/External/Addressables/AddressablesAsyncExtensions.cs index 510f2dd..1edc5b5 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/External/Addressables/AddressablesAsyncExtensions.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/External/Addressables/AddressablesAsyncExtensions.cs @@ -22,6 +22,7 @@ namespace Cysharp.Threading.Tasks public static UniTask WithCancellation(this AsyncOperationHandle handle, CancellationToken cancellationToken) { + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (handle.IsDone) { if (handle.Status == AsyncOperationStatus.Failed) @@ -36,6 +37,7 @@ namespace Cysharp.Threading.Tasks public static UniTask ToUniTask(this AsyncOperationHandle handle, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) { + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (handle.IsDone) { if (handle.Status == AsyncOperationStatus.Failed) @@ -339,6 +341,7 @@ namespace Cysharp.Threading.Tasks public static UniTask WithCancellation(this AsyncOperationHandle handle, CancellationToken cancellationToken) { + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (handle.IsDone) { if (handle.Status == AsyncOperationStatus.Failed) @@ -352,6 +355,7 @@ namespace Cysharp.Threading.Tasks public static UniTask ToUniTask(this AsyncOperationHandle handle, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) { + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (handle.IsDone) { if (handle.Status == AsyncOperationStatus.Failed) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs index 015f0e5..b045d96 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs @@ -24,6 +24,7 @@ namespace Cysharp.Threading.Tasks public static UniTask WithCancellation(this AsyncOperation asyncOperation, CancellationToken cancellationToken) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.CompletedTask; return new UniTask(AsyncOperationWithCancellationSource.Create(asyncOperation, cancellationToken, out var token), token); } @@ -31,6 +32,7 @@ namespace Cysharp.Threading.Tasks public static UniTask ToUniTask(this AsyncOperation asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.CompletedTask; return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); } @@ -166,7 +168,7 @@ namespace Cysharp.Threading.Tasks } public bool MoveNext() - { + { if (completed) { TryReturn(); @@ -312,6 +314,7 @@ namespace Cysharp.Threading.Tasks public static UniTask WithCancellation(this ResourceRequest asyncOperation, CancellationToken cancellationToken) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset); return new UniTask(ResourceRequestWithCancellationSource.Create(asyncOperation, cancellationToken, out var token), token); } @@ -319,6 +322,7 @@ namespace Cysharp.Threading.Tasks public static UniTask ToUniTask(this ResourceRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset); return new UniTask(ResourceRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); } @@ -462,7 +466,7 @@ namespace Cysharp.Threading.Tasks } public bool MoveNext() - { + { if (completed) { TryReturn(); @@ -612,6 +616,7 @@ namespace Cysharp.Threading.Tasks public static UniTask WithCancellation(this AssetBundleRequest asyncOperation, CancellationToken cancellationToken) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset); return new UniTask(AssetBundleRequestWithCancellationSource.Create(asyncOperation, cancellationToken, out var token), token); } @@ -619,6 +624,7 @@ namespace Cysharp.Threading.Tasks public static UniTask ToUniTask(this AssetBundleRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset); return new UniTask(AssetBundleRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); } @@ -762,7 +768,7 @@ namespace Cysharp.Threading.Tasks } public bool MoveNext() - { + { if (completed) { TryReturn(); @@ -912,6 +918,7 @@ namespace Cysharp.Threading.Tasks public static UniTask WithCancellation(this AssetBundleCreateRequest asyncOperation, CancellationToken cancellationToken) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.assetBundle); return new UniTask(AssetBundleCreateRequestWithCancellationSource.Create(asyncOperation, cancellationToken, out var token), token); } @@ -919,6 +926,7 @@ namespace Cysharp.Threading.Tasks public static UniTask ToUniTask(this AssetBundleCreateRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.assetBundle); return new UniTask(AssetBundleCreateRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); } @@ -1062,7 +1070,7 @@ namespace Cysharp.Threading.Tasks } public bool MoveNext() - { + { if (completed) { TryReturn(); @@ -1213,6 +1221,7 @@ namespace Cysharp.Threading.Tasks public static UniTask WithCancellation(this UnityWebRequestAsyncOperation asyncOperation, CancellationToken cancellationToken) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) { if (asyncOperation.webRequest.IsError()) @@ -1227,6 +1236,7 @@ namespace Cysharp.Threading.Tasks public static UniTask ToUniTask(this UnityWebRequestAsyncOperation asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) { if (asyncOperation.webRequest.IsError()) @@ -1393,7 +1403,7 @@ namespace Cysharp.Threading.Tasks } public bool MoveNext() - { + { if (completed) { TryReturn(); diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt index a32a1f6..ee4c457 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt @@ -49,6 +49,7 @@ namespace Cysharp.Threading.Tasks public static <#= ToUniTaskReturnType(t.returnType) #> WithCancellation(this <#= t.typeName #> asyncOperation, CancellationToken cancellationToken) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<#= IsVoid(t) ? "" : "<" + t.returnType + ">" #>(cancellationToken); <# if(IsUnityWebRequest(t)) { #> if (asyncOperation.isDone) { @@ -67,6 +68,7 @@ namespace Cysharp.Threading.Tasks public static <#= ToUniTaskReturnType(t.returnType) #> ToUniTask(this <#= t.typeName #> asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<#= IsVoid(t) ? "" : "<" + t.returnType + ">" #>(cancellationToken); <# if(IsUnityWebRequest(t)) { #> if (asyncOperation.isDone) {