< Summary

Class:AvatarSystem.WearableItemResolver
Assembly:AvatarSystem
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/AvatarSystem/WearableItemResolver.cs
Covered lines:42
Uncovered lines:3
Coverable lines:45
Total lines:118
Line coverage:93.3% (42 of 45)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WearableItemResolver()0%110100%
ResolveAndSplit()0%5.015093.33%
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<(List<WearableItem> wearables, List<WearableItem> emotes)> ResolveAndSplit(IEnumerable<stri
 16        {
 17            try
 18            {
 7919                WearableItem[] allItems = await Resolve(wearableIds, ct);
 20
 7621                List<WearableItem> wearables = new List<WearableItem>();
 7622                List<WearableItem> emotes = new List<WearableItem>();
 23
 127624                for (int i = 0; i < allItems.Length; i++)
 25                {
 56226                    if (allItems[i].IsEmote())
 027                        emotes.Add(allItems[i]);
 28                    else
 56229                        wearables.Add(allItems[i]);
 30                }
 31
 7632                return (
 33                    wearables,
 34                    emotes
 35                );
 36            }
 137            catch (OperationCanceledException)
 38            {
 39                //No disposing required
 140                throw;
 41            }
 7642        }
 43
 44        public async UniTask<WearableItem[]> Resolve(IEnumerable<string> wearableId, CancellationToken ct = default)
 45        {
 7846            ct.ThrowIfCancellationRequested();
 47
 48            try
 49            {
 50
 69051                return await UniTask.WhenAll(wearableId.Select(x => Resolve(x, ct)));
 52            }
 153            catch (OperationCanceledException)
 54            {
 55                //No disposing required
 156                throw;
 57            }
 7658        }
 59
 60        public async UniTask<WearableItem> Resolve(string wearableId, CancellationToken ct = default)
 61        {
 60962            if (disposeCts == null)
 52663                disposeCts = new CancellationTokenSource();
 60964            using CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(ct, disposeCts.Tok
 65
 60966            linkedCts.Token.ThrowIfCancellationRequested();
 67
 68            try
 69            {
 60970                if (wearablesRetrieved.ContainsKey(wearableId))
 071                    return wearablesRetrieved[wearableId];
 72
 60973                Promise<WearableItem> promise = CatalogController.RequestWearable(wearableId);
 74                // AttachExternalCancellation is needed because a CustomYieldInstruction requires a frame to operate
 67075                await promise.WithCancellation(linkedCts.Token).AttachExternalCancellation(linkedCts.Token);
 76
 77                // Cancelling is irrelevant at this point,
 78                // either we have the wearable and we have to add it to forget it later
 79                // or it's null and we just return it
 57180                if (promise.value != null)
 57181                    wearablesRetrieved.Add(wearableId, promise.value);
 82
 57183                return promise.value;
 84
 85            }
 2386            catch (OperationCanceledException)
 87            {
 88                //No disposing required
 2389                throw;
 90            }
 91            finally
 92            {
 59493                disposeCts?.Dispose();
 59494                disposeCts = null;
 95            }
 57196        }
 97
 98        public void Forget(List<string> wearableIds)
 99        {
 3952100            foreach (string wearableId in wearableIds)
 101            {
 571102                wearablesRetrieved.Remove(wearableId);
 103            }
 1405104            CatalogController.RemoveWearablesInUse(wearableIds);
 1405105        }
 106
 0107        public void Forget(string wearableId) { CatalogController.RemoveWearablesInUse(new List<string> { wearableId });
 108
 109        public void Dispose()
 110        {
 1405111            disposeCts?.Cancel();
 1405112            disposeCts?.Dispose();
 1405113            disposeCts = null;
 1405114            Forget(wearablesRetrieved.Keys.ToList());
 1405115            wearablesRetrieved.Clear();
 1405116        }
 117    }
 118}