Do.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. throw new ArgumentNullException(nameof(source));
  16. if (onNext == null)
  17. throw new ArgumentNullException(nameof(onNext));
  18. return DoHelper(source, onNext, null, null);
  19. }
  20. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action onCompleted)
  21. {
  22. if (source == null)
  23. throw new ArgumentNullException(nameof(source));
  24. if (onNext == null)
  25. throw new ArgumentNullException(nameof(onNext));
  26. if (onCompleted == null)
  27. throw new ArgumentNullException(nameof(onCompleted));
  28. return DoHelper(source, onNext, null, onCompleted);
  29. }
  30. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError)
  31. {
  32. if (source == null)
  33. throw new ArgumentNullException(nameof(source));
  34. if (onNext == null)
  35. throw new ArgumentNullException(nameof(onNext));
  36. if (onError == null)
  37. throw new ArgumentNullException(nameof(onError));
  38. return DoHelper(source, onNext, onError, null);
  39. }
  40. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
  41. {
  42. if (source == null)
  43. throw new ArgumentNullException(nameof(source));
  44. if (onNext == null)
  45. throw new ArgumentNullException(nameof(onNext));
  46. if (onError == null)
  47. throw new ArgumentNullException(nameof(onError));
  48. if (onCompleted == null)
  49. throw new ArgumentNullException(nameof(onCompleted));
  50. return DoHelper(source, onNext, onError, onCompleted);
  51. }
  52. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, IObserver<TSource> observer)
  53. {
  54. if (source == null)
  55. throw new ArgumentNullException(nameof(source));
  56. if (observer == null)
  57. throw new ArgumentNullException(nameof(observer));
  58. return DoHelper(source, observer.OnNext, observer.OnError, observer.OnCompleted);
  59. }
  60. private static IAsyncEnumerable<TSource> DoHelper<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
  61. {
  62. return new DoAsyncIterator<TSource>(source, onNext, onError, onCompleted);
  63. }
  64. private sealed class DoAsyncIterator<TSource> : AsyncIterator<TSource>
  65. {
  66. private readonly Action onCompleted;
  67. private readonly Action<Exception> onError;
  68. private readonly Action<TSource> onNext;
  69. private readonly IAsyncEnumerable<TSource> source;
  70. private IAsyncEnumerator<TSource> enumerator;
  71. public DoAsyncIterator(IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
  72. {
  73. Debug.Assert(source != null);
  74. Debug.Assert(onNext != null);
  75. this.source = source;
  76. this.onNext = onNext;
  77. this.onError = onError;
  78. this.onCompleted = onCompleted;
  79. }
  80. public override AsyncIterator<TSource> Clone()
  81. {
  82. return new DoAsyncIterator<TSource>(source, onNext, onError, onCompleted);
  83. }
  84. public override void Dispose()
  85. {
  86. if (enumerator != null)
  87. {
  88. enumerator.Dispose();
  89. enumerator = null;
  90. }
  91. base.Dispose();
  92. }
  93. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  94. {
  95. switch (state)
  96. {
  97. case AsyncIteratorState.Allocated:
  98. enumerator = source.GetEnumerator();
  99. state = AsyncIteratorState.Iterating;
  100. goto case AsyncIteratorState.Iterating;
  101. case AsyncIteratorState.Iterating:
  102. try
  103. {
  104. if (await enumerator.MoveNext(cancellationToken)
  105. .ConfigureAwait(false))
  106. {
  107. current = enumerator.Current;
  108. onNext(current);
  109. return true;
  110. }
  111. }
  112. catch (OperationCanceledException)
  113. {
  114. throw;
  115. }
  116. catch (Exception ex)
  117. {
  118. onError?.Invoke(ex);
  119. throw;
  120. }
  121. onCompleted?.Invoke();
  122. Dispose();
  123. break;
  124. }
  125. return false;
  126. }
  127. }
  128. }
  129. }