Catch.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Reactive.Disposables;
  6. using System.Threading.Tasks;
  7. namespace System.Reactive.Linq
  8. {
  9. // TODO: Implement tail call behavior to flatten Catch chains.
  10. public partial class AsyncObservable
  11. {
  12. public static IAsyncObservable<TSource> Catch<TSource, TException>(this IAsyncObservable<TSource> source, Func<TException, IAsyncObservable<TSource>> handler)
  13. where TException : Exception
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  17. if (handler == null)
  18. throw new ArgumentNullException(nameof(handler));
  19. return Create(
  20. source,
  21. handler,
  22. static async (source, handler, observer) =>
  23. {
  24. var (sink, inner) = AsyncObserver.Catch(observer, handler);
  25. var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
  26. return StableCompositeAsyncDisposable.Create(subscription, inner);
  27. });
  28. }
  29. public static IAsyncObservable<TSource> Catch<TSource, TException>(this IAsyncObservable<TSource> source, Func<TException, ValueTask<IAsyncObservable<TSource>>> handler)
  30. where TException : Exception
  31. {
  32. if (source == null)
  33. throw new ArgumentNullException(nameof(source));
  34. if (handler == null)
  35. throw new ArgumentNullException(nameof(handler));
  36. return Create(
  37. source,
  38. handler,
  39. static async (source, handler, observer) =>
  40. {
  41. var (sink, inner) = AsyncObserver.Catch(observer, handler);
  42. var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
  43. return StableCompositeAsyncDisposable.Create(subscription, inner);
  44. });
  45. }
  46. public static IAsyncObservable<TSource> Catch<TSource>(this IAsyncObservable<TSource> first, IAsyncObservable<TSource> second)
  47. {
  48. if (first == null)
  49. throw new ArgumentNullException(nameof(first));
  50. if (second == null)
  51. throw new ArgumentNullException(nameof(second));
  52. return Create(
  53. first,
  54. second,
  55. static async (first, second, observer) =>
  56. {
  57. var (sink, inner) = AsyncObserver.Catch(observer, second);
  58. var subscription = await first.SubscribeSafeAsync(sink).ConfigureAwait(false);
  59. return StableCompositeAsyncDisposable.Create(subscription, inner);
  60. });
  61. }
  62. public static IAsyncObservable<TSource> Catch<TSource>(params IAsyncObservable<TSource>[] sources) => Catch((IEnumerable<IAsyncObservable<TSource>>)sources);
  63. public static IAsyncObservable<TSource> Catch<TSource>(this IEnumerable<IAsyncObservable<TSource>> sources)
  64. {
  65. if (sources == null)
  66. throw new ArgumentNullException(nameof(sources));
  67. return Create<TSource>(async observer =>
  68. {
  69. var enumerator = sources.GetEnumerator();
  70. if (!enumerator.MoveNext())
  71. {
  72. return AsyncDisposable.Nop; // REVIEW: Is Never behavior right here?
  73. }
  74. var source = enumerator.Current;
  75. var (sink, inner) = AsyncObserver.Catch(observer, enumerator);
  76. var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
  77. return StableCompositeAsyncDisposable.Create(subscription, inner);
  78. });
  79. }
  80. }
  81. public partial class AsyncObserver
  82. {
  83. public static (IAsyncObserver<TSource>, IAsyncDisposable) Catch<TSource, TException>(IAsyncObserver<TSource> observer, Func<TException, IAsyncObservable<TSource>> handler)
  84. where TException : Exception
  85. {
  86. if (observer == null)
  87. throw new ArgumentNullException(nameof(observer));
  88. if (handler == null)
  89. throw new ArgumentNullException(nameof(handler));
  90. return Catch<TSource, TException>(observer, ex => new ValueTask<IAsyncObservable<TSource>>(handler(ex)));
  91. }
  92. public static (IAsyncObserver<TSource>, IAsyncDisposable) Catch<TSource, TException>(IAsyncObserver<TSource> observer, Func<TException, ValueTask<IAsyncObservable<TSource>>> handler)
  93. where TException : Exception
  94. {
  95. if (observer == null)
  96. throw new ArgumentNullException(nameof(observer));
  97. if (handler == null)
  98. throw new ArgumentNullException(nameof(handler));
  99. var subscription = new SingleAssignmentAsyncDisposable();
  100. var sink = Create<TSource>(
  101. observer.OnNextAsync,
  102. async ex =>
  103. {
  104. if (ex is TException error)
  105. {
  106. IAsyncObservable<TSource> handlerObservable;
  107. try
  108. {
  109. handlerObservable = await handler(error).ConfigureAwait(false);
  110. }
  111. catch (Exception err)
  112. {
  113. await observer.OnErrorAsync(err).ConfigureAwait(false);
  114. return;
  115. }
  116. var handlerSubscription = await handlerObservable.SubscribeSafeAsync(observer).ConfigureAwait(false);
  117. await subscription.AssignAsync(handlerSubscription).ConfigureAwait(false);
  118. }
  119. else
  120. {
  121. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  122. }
  123. },
  124. observer.OnCompletedAsync
  125. );
  126. return (sink, subscription);
  127. }
  128. public static (IAsyncObserver<TSource>, IAsyncDisposable) Catch<TSource>(IAsyncObserver<TSource> observer, IAsyncObservable<TSource> second)
  129. {
  130. if (observer == null)
  131. throw new ArgumentNullException(nameof(observer));
  132. if (second == null)
  133. throw new ArgumentNullException(nameof(second));
  134. var subscription = new SingleAssignmentAsyncDisposable();
  135. var sink = Create<TSource>(
  136. observer.OnNextAsync,
  137. async ex =>
  138. {
  139. var secondSubscription = await second.SubscribeSafeAsync(observer).ConfigureAwait(false);
  140. await subscription.AssignAsync(secondSubscription).ConfigureAwait(false);
  141. },
  142. observer.OnCompletedAsync
  143. );
  144. return (sink, subscription);
  145. }
  146. public static (IAsyncObserver<TSource>, IAsyncDisposable) Catch<TSource>(IAsyncObserver<TSource> observer, IEnumerator<IAsyncObservable<TSource>> handlers)
  147. {
  148. if (observer == null)
  149. throw new ArgumentNullException(nameof(observer));
  150. if (handlers == null)
  151. throw new ArgumentNullException(nameof(handlers));
  152. var innerSubscription = new SerialAsyncDisposable();
  153. IAsyncObserver<TSource> GetSink() =>
  154. Create<TSource>(
  155. observer.OnNextAsync,
  156. async ex =>
  157. {
  158. var handler = default(IAsyncObservable<TSource>);
  159. try
  160. {
  161. if (handlers.MoveNext())
  162. {
  163. handler = handlers.Current;
  164. }
  165. }
  166. catch (Exception err)
  167. {
  168. await observer.OnErrorAsync(err).ConfigureAwait(false);
  169. return;
  170. }
  171. if (handler == null)
  172. {
  173. await observer.OnErrorAsync(ex).ConfigureAwait(false); // REVIEW: Is Throw behavior right here?
  174. return;
  175. }
  176. var handlerSubscription = await handler.SubscribeSafeAsync(GetSink()).ConfigureAwait(false);
  177. await innerSubscription.AssignAsync(handlerSubscription).ConfigureAwait(false);
  178. },
  179. observer.OnCompletedAsync
  180. );
  181. var disposeEnumerator = AsyncDisposable.Create(() =>
  182. {
  183. handlers.Dispose();
  184. return default;
  185. });
  186. var subscription = StableCompositeAsyncDisposable.Create(innerSubscription, disposeEnumerator);
  187. return (GetSink(), subscription);
  188. }
  189. }
  190. }