TakeUntilCanceled.cs 6.0 KB

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