Catch.cs 8.7 KB

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