From c1f75d9ebd92de83b05ab8e872eb7176f45d2b0d Mon Sep 17 00:00:00 2001 From: neuecc Date: Tue, 30 Jun 2020 04:01:00 +0900 Subject: [PATCH] Add UniTask.Never --- .../UniTask/Runtime/UniTask.Factory.cs | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Factory.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Factory.cs index 0c4ec5c..01b8116 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Factory.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Factory.cs @@ -175,6 +175,22 @@ namespace Cysharp.Threading.Tasks return new UniTask(new DeferPromise(factory), 0); } + /// + /// Never complete. + /// + public static UniTask Never(CancellationToken cancellationToken) + { + return new UniTask(new NeverPromise(cancellationToken), 0); + } + + /// + /// Never complete. + /// + public static UniTask Never(CancellationToken cancellationToken) + { + return new UniTask(new NeverPromise(cancellationToken), 0); + } + sealed class ExceptionResultSource : IUniTaskSource { readonly ExceptionDispatchInfo exception; @@ -384,6 +400,54 @@ namespace Cysharp.Threading.Tasks return task.Status; } } + + sealed class NeverPromise : IUniTaskSource + { + static readonly Action cancellationCallback = CancellationCallback; + + CancellationToken cancellationToken; + UniTaskCompletionSourceCore core; + + public NeverPromise(CancellationToken cancellationToken) + { + this.cancellationToken = cancellationToken; + if (this.cancellationToken.CanBeCanceled) + { + this.cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this); + } + } + + static void CancellationCallback(object state) + { + var self = (NeverPromise)state; + self.core.TrySetCanceled(self.cancellationToken); + } + + public T GetResult(short token) + { + return core.GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + void IUniTaskSource.GetResult(short token) + { + core.GetResult(token); + } + } } internal static class CompletedTasks