Do.cs 5.8 KB

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