| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Threading; |
| | 5 | | using UnityEngine.Pool; |
| | 6 | |
|
| | 7 | | namespace 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 | |
|
| 0 | 22 | | internal bool isDisposed { get; private set; } |
| | 23 | |
|
| 0 | 24 | | 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> |
| 0 | 30 | | public LambdaResponsePagePointer(string constEndpoint, int pageSize, CancellationToken cancellationToken, |
| | 31 | | ILambdaServiceConsumer<T> consumer) |
| | 32 | | { |
| 0 | 33 | | this.pageSize = pageSize; |
| 0 | 34 | | this.cancellationToken = cancellationToken; |
| 0 | 35 | | this.constEndPoint = constEndpoint; |
| | 36 | |
|
| 0 | 37 | | cachedPages = DictionaryPool<int, T>.Get(); |
| 0 | 38 | | serviceConsumer = consumer; |
| | 39 | |
|
| 0 | 40 | | cancellationToken.Register(Dispose, false); |
| 0 | 41 | | } |
| | 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 | | { |
| 0 | 50 | | if (isDisposed) |
| 0 | 51 | | throw new ObjectDisposedException(nameof(LambdaResponsePagePointer<T>)); |
| | 52 | |
|
| 0 | 53 | | if (cachedPages.TryGetValue(pageNum, out var page)) |
| 0 | 54 | | return (page, true); |
| | 55 | |
|
| 0 | 56 | | var ct = this.cancellationToken; |
| | 57 | |
|
| 0 | 58 | | if (localCancellationToken != CancellationToken.None && !localCancellationToken.Equals(ct)) |
| 0 | 59 | | ct = CancellationTokenSource.CreateLinkedTokenSource(this.cancellationToken, localCancellationToken).Tok |
| | 60 | |
|
| 0 | 61 | | var res = await serviceConsumer.CreateRequest(constEndPoint, pageSize, pageNum, ct); |
| | 62 | |
|
| 0 | 63 | | if (res.success) |
| 0 | 64 | | cachedPages[pageNum] = res.response; |
| | 65 | |
|
| 0 | 66 | | return res; |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | public void Dispose() |
| | 70 | | { |
| 0 | 71 | | if (isDisposed) |
| 0 | 72 | | return; |
| | 73 | |
|
| 0 | 74 | | DictionaryPool<int, T>.Release(cachedPages); |
| 0 | 75 | | isDisposed = true; |
| 0 | 76 | | } |
| | 77 | | } |
| | 78 | | } |