TakeUntil.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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> TakeUntil<TSource>(this IUniTaskAsyncEnumerable<TSource> source, UniTask other)
  9. {
  10. Error.ThrowArgumentNullException(source, nameof(source));
  11. return new TakeUntil<TSource>(source, other, null);
  12. }
  13. public static IUniTaskAsyncEnumerable<TSource> TakeUntil<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 TakeUntil<TSource>(source, default, other);
  18. }
  19. }
  20. internal sealed class TakeUntil<TSource> : IUniTaskAsyncEnumerable<TSource>
  21. {
  22. readonly IUniTaskAsyncEnumerable<TSource> source;
  23. readonly UniTask other;
  24. readonly Func<CancellationToken, UniTask> other2;
  25. public TakeUntil(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 _TakeUntil(source, this.other2(cancellationToken), cancellationToken);
  36. }
  37. else
  38. {
  39. return new _TakeUntil(source, this.other, cancellationToken);
  40. }
  41. }
  42. sealed class _TakeUntil : 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. CancellationTokenRegistration cancellationTokenRegistration1;
  49. bool completed;
  50. Exception exception;
  51. IUniTaskAsyncEnumerator<TSource> enumerator;
  52. UniTask<bool>.Awaiter awaiter;
  53. public _TakeUntil(IUniTaskAsyncEnumerable<TSource> source, UniTask other, CancellationToken cancellationToken1)
  54. {
  55. this.source = source;
  56. this.cancellationToken1 = cancellationToken1;
  57. if (cancellationToken1.CanBeCanceled)
  58. {
  59. this.cancellationTokenRegistration1 = cancellationToken1.RegisterWithoutCaptureExecutionContext(CancelDelegate1, this);
  60. }
  61. TaskTracker.TrackActiveTask(this, 3);
  62. RunOther(other).Forget();
  63. }
  64. public TSource Current { get; private set; }
  65. public UniTask<bool> MoveNextAsync()
  66. {
  67. if (completed)
  68. {
  69. return CompletedTasks.False;
  70. }
  71. if (exception != null)
  72. {
  73. return UniTask.FromException<bool>(exception);
  74. }
  75. if (cancellationToken1.IsCancellationRequested)
  76. {
  77. return UniTask.FromCanceled<bool>(cancellationToken1);
  78. }
  79. if (enumerator == null)
  80. {
  81. enumerator = source.GetAsyncEnumerator(cancellationToken1);
  82. }
  83. completionSource.Reset();
  84. SourceMoveNext();
  85. return new UniTask<bool>(this, completionSource.Version);
  86. }
  87. void SourceMoveNext()
  88. {
  89. try
  90. {
  91. awaiter = enumerator.MoveNextAsync().GetAwaiter();
  92. if (awaiter.IsCompleted)
  93. {
  94. MoveNextCore(this);
  95. }
  96. else
  97. {
  98. awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
  99. }
  100. }
  101. catch (Exception ex)
  102. {
  103. completionSource.TrySetException(ex);
  104. }
  105. }
  106. static void MoveNextCore(object state)
  107. {
  108. var self = (_TakeUntil)state;
  109. if (self.TryGetResult(self.awaiter, out var result))
  110. {
  111. if (result)
  112. {
  113. if (self.exception != null)
  114. {
  115. self.completionSource.TrySetException(self.exception);
  116. }
  117. else if (self.cancellationToken1.IsCancellationRequested)
  118. {
  119. self.completionSource.TrySetCanceled(self.cancellationToken1);
  120. }
  121. else
  122. {
  123. self.Current = self.enumerator.Current;
  124. self.completionSource.TrySetResult(true);
  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. completionSource.TrySetResult(false);
  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 = (_TakeUntil)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. }