SkipUntilCanceled.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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> SkipUntilCanceled<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  9. {
  10. Error.ThrowArgumentNullException(source, nameof(source));
  11. return new SkipUntilCanceled<TSource>(source, cancellationToken);
  12. }
  13. }
  14. internal sealed class SkipUntilCanceled<TSource> : IUniTaskAsyncEnumerable<TSource>
  15. {
  16. readonly IUniTaskAsyncEnumerable<TSource> source;
  17. readonly CancellationToken cancellationToken;
  18. public SkipUntilCanceled(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  19. {
  20. this.source = source;
  21. this.cancellationToken = cancellationToken;
  22. }
  23. public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  24. {
  25. return new _SkipUntilCanceled(source, this.cancellationToken, cancellationToken);
  26. }
  27. sealed class _SkipUntilCanceled : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
  28. {
  29. static readonly Action<object> CancelDelegate1 = OnCanceled1;
  30. static readonly Action<object> CancelDelegate2 = OnCanceled2;
  31. static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
  32. readonly IUniTaskAsyncEnumerable<TSource> source;
  33. CancellationToken cancellationToken1;
  34. CancellationToken cancellationToken2;
  35. CancellationTokenRegistration cancellationTokenRegistration1;
  36. CancellationTokenRegistration cancellationTokenRegistration2;
  37. int isCanceled;
  38. IUniTaskAsyncEnumerator<TSource> enumerator;
  39. UniTask<bool>.Awaiter awaiter;
  40. bool continueNext;
  41. public _SkipUntilCanceled(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken1, CancellationToken cancellationToken2)
  42. {
  43. this.source = source;
  44. this.cancellationToken1 = cancellationToken1;
  45. this.cancellationToken2 = cancellationToken2;
  46. if (cancellationToken1.CanBeCanceled)
  47. {
  48. this.cancellationTokenRegistration1 = cancellationToken1.RegisterWithoutCaptureExecutionContext(CancelDelegate1, this);
  49. }
  50. if (cancellationToken1 != cancellationToken2 && cancellationToken2.CanBeCanceled)
  51. {
  52. this.cancellationTokenRegistration2 = cancellationToken2.RegisterWithoutCaptureExecutionContext(CancelDelegate2, this);
  53. }
  54. TaskTracker.TrackActiveTask(this, 3);
  55. }
  56. public TSource Current { get; private set; }
  57. public UniTask<bool> MoveNextAsync()
  58. {
  59. if (enumerator == null)
  60. {
  61. if (cancellationToken1.IsCancellationRequested) isCanceled = 1;
  62. if (cancellationToken2.IsCancellationRequested) isCanceled = 1;
  63. enumerator = source.GetAsyncEnumerator(cancellationToken2); // use only AsyncEnumerator provided token.
  64. }
  65. completionSource.Reset();
  66. if (isCanceled != 0)
  67. {
  68. SourceMoveNext();
  69. }
  70. return new UniTask<bool>(this, completionSource.Version);
  71. }
  72. void SourceMoveNext()
  73. {
  74. try
  75. {
  76. LOOP:
  77. awaiter = enumerator.MoveNextAsync().GetAwaiter();
  78. if (awaiter.IsCompleted)
  79. {
  80. continueNext = true;
  81. MoveNextCore(this);
  82. if (continueNext)
  83. {
  84. continueNext = false;
  85. goto LOOP;
  86. }
  87. }
  88. else
  89. {
  90. awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. completionSource.TrySetException(ex);
  96. }
  97. }
  98. static void MoveNextCore(object state)
  99. {
  100. var self = (_SkipUntilCanceled)state;
  101. if (self.TryGetResult(self.awaiter, out var result))
  102. {
  103. if (result)
  104. {
  105. self.Current = self.enumerator.Current;
  106. self.completionSource.TrySetResult(true);
  107. if (self.continueNext)
  108. {
  109. self.SourceMoveNext();
  110. }
  111. }
  112. else
  113. {
  114. self.completionSource.TrySetResult(false);
  115. }
  116. }
  117. }
  118. static void OnCanceled1(object state)
  119. {
  120. var self = (_SkipUntilCanceled)state;
  121. if (self.isCanceled == 0)
  122. {
  123. if (Interlocked.Increment(ref self.isCanceled) == 1)
  124. {
  125. self.cancellationTokenRegistration2.Dispose();
  126. self.SourceMoveNext();
  127. }
  128. }
  129. }
  130. static void OnCanceled2(object state)
  131. {
  132. var self = (_SkipUntilCanceled)state;
  133. if (self.isCanceled == 0)
  134. {
  135. if (Interlocked.Increment(ref self.isCanceled) == 1)
  136. {
  137. self.cancellationTokenRegistration2.Dispose();
  138. self.SourceMoveNext();
  139. }
  140. }
  141. }
  142. public UniTask DisposeAsync()
  143. {
  144. TaskTracker.RemoveTracking(this);
  145. cancellationTokenRegistration1.Dispose();
  146. cancellationTokenRegistration2.Dispose();
  147. if (enumerator != null)
  148. {
  149. return enumerator.DisposeAsync();
  150. }
  151. return default;
  152. }
  153. }
  154. }
  155. }