< Summary

Class:DCL.Helpers.PromiseAsyncExtensionsPromiseCompletionSource[T]
Assembly:Promise
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/Promise/PromiseAsyncExtensions.cs
Covered lines:39
Uncovered lines:4
Coverable lines:43
Total lines:132
Line coverage:90.6% (39 of 43)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PromiseCompletionSource()0%110100%
PromiseCompletionSource()0%110100%
SetData(...)0%110100%
SetCancelled()0%110100%
SetResult(...)0%110100%
SetException(...)0%2100%
UnsubscribeBeforeReset()0%110100%
Create(...)0%3.023087.5%
GetStatus(...)0%110100%
OnCompleted(...)0%110100%
GetResult(...)0%110100%
GetResult(...)0%110100%
UnsafeGetStatus()0%2100%
TryReturn()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/Promise/PromiseAsyncExtensions.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using System;
 3using System.Threading;
 4
 5namespace DCL.Helpers
 6{
 7    public static class PromiseAsyncExtensions
 8    {
 9        public static UniTask<T>.Awaiter GetAwaiter<T>(this Promise<T> promise) =>
 10            promise.ToUniTask().GetAwaiter();
 11
 12        public static UniTask<T> WithCancellation<T>(this Promise<T> promise, CancellationToken cancellationToken) =>
 13            promise.ToUniTask(cancellationToken);
 14
 15        public static UniTask<T> ToUniTask<T>(this Promise<T> promise, CancellationToken cancellationToken = default)
 16        {
 17            if (promise == null)
 18                throw new ArgumentNullException(nameof(promise));
 19
 20            if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<T>(cancellationToken);
 21            if (promise.failed) return UniTask.FromException<T>(promise.exception);
 22            if (promise.resolved) return UniTask.FromResult(promise.value);
 23
 24            return new UniTask<T>(PromiseCompletionSource<T>.Create(promise, cancellationToken, out short token), token)
 25        }
 26
 27        internal sealed class PromiseCompletionSource<T> : IUniTaskSource<T>, ITaskPoolNode<PromiseCompletionSource<T>>
 28        {
 29            private static TaskPool<PromiseCompletionSource<T>> pool;
 30
 31            private PromiseCompletionSource<T> nextNode;
 32
 2733            public ref PromiseCompletionSource<T> NextNode => ref nextNode;
 34
 35            private Promise<T> innerPromise;
 36            private CancellationToken cancellationToken;
 37            private CancellationTokenRegistration cancellationTokenRegistration;
 38
 39            private UniTaskCompletionSourceCore<T> core;
 40
 41            static PromiseCompletionSource()
 42            {
 243                TaskPool.RegisterSizeGetter(typeof(PromiseCompletionSource<T>), () => pool.Size);
 244            }
 45
 4446            private PromiseCompletionSource() { }
 47
 48            private void SetData(Promise<T> innerPromise, CancellationToken cancellationToken)
 49            {
 3250                this.innerPromise = innerPromise;
 3251                this.cancellationToken = cancellationToken;
 52
 53                // Subscribe directly to `Promise` instead of calling `MoveNext` to prevent skipping an extra frame
 3254                innerPromise.onSuccess += SetResult;
 3255                innerPromise.onError += SetException;
 56
 3257                cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(SetCancelled);
 3258            }
 59
 60            private void SetCancelled()
 61            {
 1662                UnsubscribeBeforeReset();
 1663                core.TrySetCanceled(cancellationToken);
 1664            }
 65
 66            private void SetResult(T result)
 67            {
 168                UnsubscribeBeforeReset();
 169                core.TrySetResult(result);
 170            }
 71
 72            private void SetException(string _)
 73            {
 074                UnsubscribeBeforeReset();
 075                core.TrySetException(innerPromise.exception);
 076            }
 77
 78            private void UnsubscribeBeforeReset()
 79            {
 1780                cancellationTokenRegistration.Dispose();
 1781                innerPromise.onSuccess -= SetResult;
 1782                innerPromise.onError -= SetException;
 1783            }
 84
 85            public static IUniTaskSource<T> Create(Promise<T> promise, CancellationToken cancellationToken, out short to
 86            {
 3287                if (cancellationToken.IsCancellationRequested) { return AutoResetUniTaskCompletionSource<T>.CreateFromCa
 88
 5489                if (!pool.TryPop(out var result)) result = new PromiseCompletionSource<T>();
 90
 3291                result.SetData(promise, cancellationToken);
 92
 3293                TaskTracker.TrackActiveTask(result, 3);
 94
 3295                token = result.core.Version;
 96
 3297                return result;
 98            }
 99
 100            public UniTaskStatus GetStatus(short token) =>
 33101                core.GetStatus(token);
 102
 103            public void OnCompleted(Action<object> continuation, object state, short token)
 104            {
 32105                core.OnCompleted(continuation, state, token);
 32106            }
 107
 108            public T GetResult(short token)
 109            {
 17110                try { return core.GetResult(token); }
 34111                finally { TryReturn(); }
 1112            }
 113
 114            void IUniTaskSource.GetResult(short token)
 115            {
 1116                GetResult(token);
 1117            }
 118
 119            public UniTaskStatus UnsafeGetStatus() =>
 0120                core.UnsafeGetStatus();
 121
 122            private bool TryReturn()
 123            {
 17124                TaskTracker.RemoveTracking(this);
 17125                core.Reset();
 17126                innerPromise = null;
 17127                cancellationToken = default;
 17128                return pool.TryPush(this);
 129            }
 130        }
 131    }
 132}