TakeLast.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using Cysharp.Threading.Tasks.Internal;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. namespace Cysharp.Threading.Tasks.Linq
  6. {
  7. public static partial class UniTaskAsyncEnumerable
  8. {
  9. public static IUniTaskAsyncEnumerable<TSource> TakeLast<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Int32 count)
  10. {
  11. Error.ThrowArgumentNullException(source, nameof(source));
  12. // non take.
  13. if (count <= 0)
  14. {
  15. return Empty<TSource>();
  16. }
  17. return new TakeLast<TSource>(source, count);
  18. }
  19. }
  20. internal sealed class TakeLast<TSource> : IUniTaskAsyncEnumerable<TSource>
  21. {
  22. readonly IUniTaskAsyncEnumerable<TSource> source;
  23. readonly int count;
  24. public TakeLast(IUniTaskAsyncEnumerable<TSource> source, int count)
  25. {
  26. this.source = source;
  27. this.count = count;
  28. }
  29. public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  30. {
  31. return new _TakeLast(source, count, cancellationToken);
  32. }
  33. sealed class _TakeLast : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
  34. {
  35. static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
  36. readonly IUniTaskAsyncEnumerable<TSource> source;
  37. readonly int count;
  38. CancellationToken cancellationToken;
  39. IUniTaskAsyncEnumerator<TSource> enumerator;
  40. UniTask<bool>.Awaiter awaiter;
  41. Queue<TSource> queue;
  42. bool iterateCompleted;
  43. bool continueNext;
  44. public _TakeLast(IUniTaskAsyncEnumerable<TSource> source, int count, CancellationToken cancellationToken)
  45. {
  46. this.source = source;
  47. this.count = count;
  48. this.cancellationToken = cancellationToken;
  49. TaskTracker.TrackActiveTask(this, 3);
  50. }
  51. public TSource Current { get; private set; }
  52. public UniTask<bool> MoveNextAsync()
  53. {
  54. cancellationToken.ThrowIfCancellationRequested();
  55. if (enumerator == null)
  56. {
  57. enumerator = source.GetAsyncEnumerator(cancellationToken);
  58. queue = new Queue<TSource>();
  59. }
  60. completionSource.Reset();
  61. SourceMoveNext();
  62. return new UniTask<bool>(this, completionSource.Version);
  63. }
  64. void SourceMoveNext()
  65. {
  66. if (iterateCompleted)
  67. {
  68. if (queue.Count > 0)
  69. {
  70. Current = queue.Dequeue();
  71. completionSource.TrySetResult(true);
  72. }
  73. else
  74. {
  75. completionSource.TrySetResult(false);
  76. }
  77. return;
  78. }
  79. try
  80. {
  81. LOOP:
  82. awaiter = enumerator.MoveNextAsync().GetAwaiter();
  83. if (awaiter.IsCompleted)
  84. {
  85. continueNext = true;
  86. MoveNextCore(this);
  87. if (continueNext)
  88. {
  89. continueNext = false;
  90. goto LOOP; // avoid recursive
  91. }
  92. }
  93. else
  94. {
  95. awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. completionSource.TrySetException(ex);
  101. }
  102. }
  103. static void MoveNextCore(object state)
  104. {
  105. var self = (_TakeLast)state;
  106. if (self.TryGetResult(self.awaiter, out var result))
  107. {
  108. if (result)
  109. {
  110. if (self.queue.Count < self.count)
  111. {
  112. self.queue.Enqueue(self.enumerator.Current);
  113. if (!self.continueNext)
  114. {
  115. self.SourceMoveNext();
  116. }
  117. }
  118. else
  119. {
  120. self.queue.Dequeue();
  121. self.queue.Enqueue(self.enumerator.Current);
  122. if (!self.continueNext)
  123. {
  124. self.SourceMoveNext();
  125. }
  126. }
  127. }
  128. else
  129. {
  130. self.continueNext = false;
  131. self.iterateCompleted = true;
  132. self.SourceMoveNext();
  133. }
  134. }
  135. else
  136. {
  137. self.continueNext = false;
  138. }
  139. }
  140. public UniTask DisposeAsync()
  141. {
  142. TaskTracker.RemoveTracking(this);
  143. if (enumerator != null)
  144. {
  145. return enumerator.DisposeAsync();
  146. }
  147. return default;
  148. }
  149. }
  150. }
  151. }