Do.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext)
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  17. if (onNext == null)
  18. throw new ArgumentNullException(nameof(onNext));
  19. return DoHelper(source, onNext, _ => { }, () => { });
  20. }
  21. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action onCompleted)
  22. {
  23. if (source == null)
  24. throw new ArgumentNullException(nameof(source));
  25. if (onNext == null)
  26. throw new ArgumentNullException(nameof(onNext));
  27. if (onCompleted == null)
  28. throw new ArgumentNullException(nameof(onCompleted));
  29. return DoHelper(source, onNext, _ => { }, onCompleted);
  30. }
  31. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError)
  32. {
  33. if (source == null)
  34. throw new ArgumentNullException(nameof(source));
  35. if (onNext == null)
  36. throw new ArgumentNullException(nameof(onNext));
  37. if (onError == null)
  38. throw new ArgumentNullException(nameof(onError));
  39. return DoHelper(source, onNext, onError, () => { });
  40. }
  41. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
  42. {
  43. if (source == null)
  44. throw new ArgumentNullException(nameof(source));
  45. if (onNext == null)
  46. throw new ArgumentNullException(nameof(onNext));
  47. if (onError == null)
  48. throw new ArgumentNullException(nameof(onError));
  49. if (onCompleted == null)
  50. throw new ArgumentNullException(nameof(onCompleted));
  51. return DoHelper(source, onNext, onError, onCompleted);
  52. }
  53. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, IObserver<TSource> observer)
  54. {
  55. if (source == null)
  56. throw new ArgumentNullException(nameof(source));
  57. if (observer == null)
  58. throw new ArgumentNullException(nameof(observer));
  59. return DoHelper(source, observer.OnNext, observer.OnError, observer.OnCompleted);
  60. }
  61. private static IAsyncEnumerable<TSource> DoHelper<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
  62. {
  63. return CreateEnumerable(
  64. () =>
  65. {
  66. var e = source.GetEnumerator();
  67. var cts = new CancellationTokenDisposable();
  68. var d = Disposable.Create(cts, e);
  69. var current = default(TSource);
  70. var f = default(Func<CancellationToken, Task<bool>>);
  71. f = async ct =>
  72. {
  73. try
  74. {
  75. var result = await e.MoveNext(ct)
  76. .ConfigureAwait(false);
  77. if (!result)
  78. {
  79. onCompleted();
  80. }
  81. else
  82. {
  83. current = e.Current;
  84. onNext(current);
  85. }
  86. return result;
  87. }
  88. catch (OperationCanceledException)
  89. {
  90. throw;
  91. }
  92. catch (Exception ex)
  93. {
  94. onError(ex);
  95. throw;
  96. }
  97. };
  98. return CreateEnumerator(
  99. f,
  100. () => current,
  101. d.Dispose,
  102. e
  103. );
  104. });
  105. }
  106. }
  107. }