< Summary

Class:AvatarSystem.WearableItemResolver
Assembly:AvatarSystem
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/AvatarSystem/WearableItemResolver.cs
Covered lines:32
Uncovered lines:2
Coverable lines:34
Total lines:89
Line coverage:94.1% (32 of 34)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WearableItemResolver()0%110100%
Resolve()0%440100%
Resolve()0%12.0212094.44%
Forget(...)0%220100%
Forget(...)0%2100%
Dispose()0%330100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/AvatarSystem/WearableItemResolver.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Threading;
 5using Cysharp.Threading.Tasks;
 6using DCL.Helpers;
 7
 8namespace AvatarSystem
 9{
 10    public class WearableItemResolver : IWearableItemResolver
 11    {
 130512        private CancellationTokenSource disposeCts = new CancellationTokenSource();
 130513        private readonly Dictionary<string, WearableItem> wearablesRetrieved = new Dictionary<string, WearableItem>();
 14
 15        public async UniTask<WearableItem[]> Resolve(IEnumerable<string> wearableId, CancellationToken ct = default)
 16        {
 7817            ct.ThrowIfCancellationRequested();
 18
 19            try
 20            {
 21
 66822                return await UniTask.WhenAll(wearableId.Select(x => Resolve(x, ct)));
 23            }
 124            catch (OperationCanceledException)
 25            {
 26                //No disposing required
 127                throw;
 28            }
 7629        }
 30
 31        public async UniTask<WearableItem> Resolve(string wearableId, CancellationToken ct = default)
 32        {
 58733            if (disposeCts == null)
 52534                disposeCts = new CancellationTokenSource();
 58735            using CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(ct, disposeCts.Tok
 36
 58737            linkedCts.Token.ThrowIfCancellationRequested();
 38
 39            try
 40            {
 58741                if (wearablesRetrieved.ContainsKey(wearableId))
 042                    return wearablesRetrieved[wearableId];
 43
 58744                Promise<WearableItem> promise = CatalogController.RequestWearable(wearableId);
 45                // AttachExternalCancellation is needed because a CustomYieldInstruction requires a frame to operate
 60446                await promise.WithCancellation(linkedCts.Token).AttachExternalCancellation(linkedCts.Token);
 47
 48                // Cancelling is irrelevant at this point,
 49                // either we have the wearable and we have to add it to forget it later
 50                // or it's null and we just return it
 57151                if (promise.value != null)
 57152                    wearablesRetrieved.Add(wearableId, promise.value);
 53
 57154                return promise.value;
 55
 56            }
 157            catch (OperationCanceledException)
 58            {
 59                //No disposing required
 160                throw;
 61            }
 62            finally
 63            {
 57264                disposeCts?.Dispose();
 57265                disposeCts = null;
 66            }
 57167        }
 68
 69        public void Forget(List<string> wearableIds)
 70        {
 395271            foreach (string wearableId in wearableIds)
 72            {
 57173                wearablesRetrieved.Remove(wearableId);
 74            }
 140575            CatalogController.RemoveWearablesInUse(wearableIds);
 140576        }
 77
 078        public void Forget(string wearableId) { CatalogController.RemoveWearablesInUse(new List<string> { wearableId });
 79
 80        public void Dispose()
 81        {
 140582            disposeCts?.Cancel();
 140583            disposeCts?.Dispose();
 140584            disposeCts = null;
 140585            Forget(wearablesRetrieved.Keys.ToList());
 140586            wearablesRetrieved.Clear();
 140587        }
 88    }
 89}