Do.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerable
  11. {
  12. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext)
  13. {
  14. if (source == null)
  15. {
  16. throw new ArgumentNullException(nameof(source));
  17. }
  18. if (onNext == null)
  19. {
  20. throw new ArgumentNullException(nameof(onNext));
  21. }
  22. return DoHelper(source, onNext, null, null);
  23. }
  24. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action onCompleted)
  25. {
  26. if (source == null)
  27. {
  28. throw new ArgumentNullException(nameof(source));
  29. }
  30. if (onNext == null)
  31. {
  32. throw new ArgumentNullException(nameof(onNext));
  33. }
  34. if (onCompleted == null)
  35. {
  36. throw new ArgumentNullException(nameof(onCompleted));
  37. }
  38. return DoHelper(source, onNext, null, onCompleted);
  39. }
  40. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError)
  41. {
  42. if (source == null)
  43. {
  44. throw new ArgumentNullException(nameof(source));
  45. }
  46. if (onNext == null)
  47. {
  48. throw new ArgumentNullException(nameof(onNext));
  49. }
  50. if (onError == null)
  51. {
  52. throw new ArgumentNullException(nameof(onError));
  53. }
  54. return DoHelper(source, onNext, onError, null);
  55. }
  56. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
  57. {
  58. if (source == null)
  59. {
  60. throw new ArgumentNullException(nameof(source));
  61. }
  62. if (onNext == null)
  63. {
  64. throw new ArgumentNullException(nameof(onNext));
  65. }
  66. if (onError == null)
  67. {
  68. throw new ArgumentNullException(nameof(onError));
  69. }
  70. if (onCompleted == null)
  71. {
  72. throw new ArgumentNullException(nameof(onCompleted));
  73. }
  74. return DoHelper(source, onNext, onError, onCompleted);
  75. }
  76. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, IObserver<TSource> observer)
  77. {
  78. if (source == null)
  79. {
  80. throw new ArgumentNullException(nameof(source));
  81. }
  82. if (observer == null)
  83. {
  84. throw new ArgumentNullException(nameof(observer));
  85. }
  86. return DoHelper(source, observer.OnNext, observer.OnError, observer.OnCompleted);
  87. }
  88. private static IAsyncEnumerable<TSource> DoHelper<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
  89. {
  90. return new DoAsyncIterator<TSource>(source, onNext, onError, onCompleted);
  91. }
  92. private sealed class DoAsyncIterator<TSource> : AsyncIterator<TSource>
  93. {
  94. private readonly Action onCompleted;
  95. private readonly Action<Exception> onError;
  96. private readonly Action<TSource> onNext;
  97. private readonly IAsyncEnumerable<TSource> source;
  98. private IAsyncEnumerator<TSource> enumerator;
  99. public DoAsyncIterator(IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
  100. {
  101. Debug.Assert(source != null);
  102. Debug.Assert(onNext != null);
  103. this.source = source;
  104. this.onNext = onNext;
  105. this.onError = onError;
  106. this.onCompleted = onCompleted;
  107. }
  108. public override AsyncIterator<TSource> Clone()
  109. {
  110. return new DoAsyncIterator<TSource>(source, onNext, onError, onCompleted);
  111. }
  112. public override void Dispose()
  113. {
  114. if (enumerator != null)
  115. {
  116. enumerator.Dispose();
  117. enumerator = null;
  118. }
  119. base.Dispose();
  120. }
  121. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  122. {
  123. switch (state)
  124. {
  125. case AsyncIteratorState.Allocated:
  126. enumerator = source.GetEnumerator();
  127. state = AsyncIteratorState.Iterating;
  128. goto case AsyncIteratorState.Iterating;
  129. case AsyncIteratorState.Iterating:
  130. try
  131. {
  132. if (await enumerator.MoveNext(cancellationToken)
  133. .ConfigureAwait(false))
  134. {
  135. current = enumerator.Current;
  136. onNext(current);
  137. return true;
  138. }
  139. }
  140. catch (OperationCanceledException)
  141. {
  142. throw;
  143. }
  144. catch (Exception ex)
  145. {
  146. onError?.Invoke(ex);
  147. throw;
  148. }
  149. onCompleted?.Invoke();
  150. Dispose();
  151. break;
  152. }
  153. return false;
  154. }
  155. }
  156. }
  157. }