SkipUntil.cs 6.2 KB

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