Do.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.Threading.Tasks;
  5. namespace System.Reactive.Linq
  6. {
  7. partial class AsyncObservable
  8. {
  9. public static IAsyncObservable<TSource> Do<TSource>(this IAsyncObservable<TSource> source, IAsyncObserver<TSource> observer)
  10. {
  11. if (source == null)
  12. throw new ArgumentNullException(nameof(source));
  13. if (observer == null)
  14. throw new ArgumentNullException(nameof(observer));
  15. return Create<TSource>(target => source.SubscribeSafeAsync(AsyncObserver.Do(target, observer)));
  16. }
  17. public static IAsyncObservable<TSource> Do<TSource>(this IAsyncObservable<TSource> source, Func<TSource, Task> onNext)
  18. {
  19. if (source == null)
  20. throw new ArgumentNullException(nameof(source));
  21. if (onNext == null)
  22. throw new ArgumentNullException(nameof(onNext));
  23. return Create<TSource>(target => source.SubscribeSafeAsync(AsyncObserver.Do(target, onNext)));
  24. }
  25. public static IAsyncObservable<TSource> Do<TSource>(this IAsyncObservable<TSource> source, Func<TSource, Task> onNext, Func<Exception, Task> onError, Func<Task> onCompleted)
  26. {
  27. if (source == null)
  28. throw new ArgumentNullException(nameof(source));
  29. if (onNext == null)
  30. throw new ArgumentNullException(nameof(onNext));
  31. if (onError == null)
  32. throw new ArgumentNullException(nameof(onError));
  33. if (onCompleted == null)
  34. throw new ArgumentNullException(nameof(onCompleted));
  35. return Create<TSource>(target => source.SubscribeSafeAsync(AsyncObserver.Do(target, onNext, onError, onCompleted)));
  36. }
  37. }
  38. partial class AsyncObserver
  39. {
  40. public static IAsyncObserver<TSource> Do<TSource>(IAsyncObserver<TSource> observer, IAsyncObserver<TSource> witness)
  41. {
  42. if (observer == null)
  43. throw new ArgumentNullException(nameof(observer));
  44. if (witness == null)
  45. throw new ArgumentNullException(nameof(witness));
  46. return Create<TSource>(
  47. async x =>
  48. {
  49. try
  50. {
  51. await witness.OnNextAsync(x).ConfigureAwait(false);
  52. }
  53. catch (Exception ex)
  54. {
  55. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  56. return;
  57. }
  58. await observer.OnNextAsync(x).ConfigureAwait(false);
  59. },
  60. async error =>
  61. {
  62. try
  63. {
  64. await witness.OnErrorAsync(error).ConfigureAwait(false);
  65. }
  66. catch (Exception ex)
  67. {
  68. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  69. return;
  70. }
  71. await observer.OnErrorAsync(error).ConfigureAwait(false);
  72. },
  73. async () =>
  74. {
  75. try
  76. {
  77. await witness.OnCompletedAsync().ConfigureAwait(false);
  78. }
  79. catch (Exception ex)
  80. {
  81. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  82. return;
  83. }
  84. await observer.OnCompletedAsync().ConfigureAwait(false);
  85. }
  86. );
  87. }
  88. public static IAsyncObserver<TSource> Do<TSource>(IAsyncObserver<TSource> observer, Func<TSource, Task> onNext)
  89. {
  90. if (observer == null)
  91. throw new ArgumentNullException(nameof(observer));
  92. if (onNext == null)
  93. throw new ArgumentNullException(nameof(onNext));
  94. return Do(observer, Create(onNext));
  95. }
  96. public static IAsyncObserver<TSource> Do<TSource>(IAsyncObserver<TSource> observer, Func<TSource, Task> onNext, Func<Exception, Task> onError, Func<Task> onCompleted)
  97. {
  98. if (observer == null)
  99. throw new ArgumentNullException(nameof(observer));
  100. if (onNext == null)
  101. throw new ArgumentNullException(nameof(onNext));
  102. if (onError == null)
  103. throw new ArgumentNullException(nameof(onError));
  104. if (onCompleted == null)
  105. throw new ArgumentNullException(nameof(onCompleted));
  106. return Do(observer, Create(onNext, onError, onCompleted));
  107. }
  108. }
  109. }