< Summary

Class:DCLServices.Lambdas.LambdaResponsePagePointer[T]
Assembly:LambdasService
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/Lambdas/LambdaResponsePagePointer.cs
Covered lines:0
Uncovered lines:27
Coverable lines:27
Total lines:78
Line coverage:0% (0 of 27)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
LambdaResponsePagePointer(...)0%2100%
GetPageAsync()0%72800%
Dispose()0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/Lambdas/LambdaResponsePagePointer.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using System;
 3using System.Collections.Generic;
 4using System.Threading;
 5using UnityEngine.Pool;
 6
 7namespace DCLServices.Lambdas
 8{
 9    /// <summary>
 10    /// Caches paginated results until `Dispose` is called
 11    /// assuming the results are immutable while iterating
 12    /// </summary>
 13    /// <typeparam name="T"></typeparam>
 14    public class LambdaResponsePagePointer<T> : IDisposable where T : PaginatedResponse
 15    {
 16        private readonly int pageSize;
 17        private readonly Dictionary<int, T> cachedPages;
 18        private readonly ILambdaServiceConsumer<T> serviceConsumer;
 19        private readonly CancellationToken cancellationToken;
 20        private readonly string constEndPoint;
 21
 022        internal bool isDisposed { get; private set; }
 23
 024        internal IReadOnlyDictionary<int, T> CachedPages => cachedPages;
 25
 26        /// <param name="constEndpoint"></param>
 27        /// <param name="pageSize"></param>
 28        /// <param name="cancellationToken">Pass Cancellation Token so the pointer will be automatically disposed on can
 29        /// <param name="consumer"></param>
 030        public LambdaResponsePagePointer(string constEndpoint, int pageSize, CancellationToken cancellationToken,
 31            ILambdaServiceConsumer<T> consumer)
 32        {
 033            this.pageSize = pageSize;
 034            this.cancellationToken = cancellationToken;
 035            this.constEndPoint = constEndpoint;
 36
 037            cachedPages = DictionaryPool<int, T>.Get();
 038            serviceConsumer = consumer;
 39
 040            cancellationToken.Register(Dispose, false);
 041        }
 42
 43        /// <summary>
 44        /// Retrieves a page from the endpoint or cache
 45        /// </summary>
 46        /// <exception cref="ObjectDisposedException"></exception>
 47        /// <exception cref="OperationCanceledException"></exception>
 48        public async UniTask<(T response, bool success)> GetPageAsync(int pageNum, CancellationToken localCancellationTo
 49        {
 050            if (isDisposed)
 051                throw new ObjectDisposedException(nameof(LambdaResponsePagePointer<T>));
 52
 053            if (cachedPages.TryGetValue(pageNum, out var page))
 054                return (page, true);
 55
 056            var ct = this.cancellationToken;
 57
 058            if (localCancellationToken != CancellationToken.None && !localCancellationToken.Equals(ct))
 059                ct = CancellationTokenSource.CreateLinkedTokenSource(this.cancellationToken, localCancellationToken).Tok
 60
 061            var res = await serviceConsumer.CreateRequest(constEndPoint, pageSize, pageNum, ct);
 62
 063            if (res.success)
 064                cachedPages[pageNum] = res.response;
 65
 066            return res;
 067        }
 68
 69        public void Dispose()
 70        {
 071            if (isDisposed)
 072                return;
 73
 074            DictionaryPool<int, T>.Release(cachedPages);
 075            isDisposed = true;
 076        }
 77    }
 78}