OnErrorResumeNext.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 OnErrorResumeNext chains.
  10. public partial class AsyncObservable
  11. {
  12. public static IAsyncObservable<TSource> OnErrorResumeNext<TSource>(this IAsyncObservable<TSource> first, IAsyncObservable<TSource> second)
  13. {
  14. if (first == null)
  15. throw new ArgumentNullException(nameof(first));
  16. if (second == null)
  17. throw new ArgumentNullException(nameof(second));
  18. return Create(
  19. first,
  20. second,
  21. default(TSource),
  22. async (first, second, observer) =>
  23. {
  24. var (sink, inner) = AsyncObserver.OnErrorResumeNext(observer, second);
  25. var subscription = await first.SubscribeSafeAsync(sink).ConfigureAwait(false);
  26. return StableCompositeAsyncDisposable.Create(subscription, inner);
  27. });
  28. }
  29. public static IAsyncObservable<TSource> OnErrorResumeNext<TSource>(params IAsyncObservable<TSource>[] sources) => OnErrorResumeNext((IEnumerable<IAsyncObservable<TSource>>)sources);
  30. public static IAsyncObservable<TSource> OnErrorResumeNext<TSource>(this IEnumerable<IAsyncObservable<TSource>> sources)
  31. {
  32. if (sources == null)
  33. throw new ArgumentNullException(nameof(sources));
  34. return Create<TSource>(async observer =>
  35. {
  36. var enumerator = sources.GetEnumerator();
  37. if (!enumerator.MoveNext())
  38. {
  39. return AsyncDisposable.Nop; // REVIEW: Is Never behavior right here?
  40. }
  41. var source = enumerator.Current;
  42. var (sink, inner) = AsyncObserver.OnErrorResumeNext(observer, enumerator);
  43. var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
  44. return StableCompositeAsyncDisposable.Create(subscription, inner);
  45. });
  46. }
  47. }
  48. public partial class AsyncObserver
  49. {
  50. public static (IAsyncObserver<TSource>, IAsyncDisposable) OnErrorResumeNext<TSource>(IAsyncObserver<TSource> observer, IAsyncObservable<TSource> second)
  51. {
  52. if (observer == null)
  53. throw new ArgumentNullException(nameof(observer));
  54. if (second == null)
  55. throw new ArgumentNullException(nameof(second));
  56. var subscription = new SingleAssignmentAsyncDisposable();
  57. var sink = Create<TSource>(
  58. observer.OnNextAsync,
  59. async ex =>
  60. {
  61. var secondSubscription = await second.SubscribeSafeAsync(observer).ConfigureAwait(false);
  62. await subscription.AssignAsync(secondSubscription).ConfigureAwait(false);
  63. },
  64. async () =>
  65. {
  66. var secondSubscription = await second.SubscribeSafeAsync(observer).ConfigureAwait(false);
  67. await subscription.AssignAsync(secondSubscription).ConfigureAwait(false);
  68. }
  69. );
  70. return (sink, subscription);
  71. }
  72. public static (IAsyncObserver<TSource>, IAsyncDisposable) OnErrorResumeNext<TSource>(IAsyncObserver<TSource> observer, IEnumerator<IAsyncObservable<TSource>> handlers)
  73. {
  74. if (observer == null)
  75. throw new ArgumentNullException(nameof(observer));
  76. if (handlers == null)
  77. throw new ArgumentNullException(nameof(handlers));
  78. var innerSubscription = new SerialAsyncDisposable();
  79. async ValueTask NextAsync()
  80. {
  81. var next = default(IAsyncObservable<TSource>);
  82. try
  83. {
  84. if (handlers.MoveNext())
  85. {
  86. next = handlers.Current;
  87. }
  88. }
  89. catch (Exception err)
  90. {
  91. await observer.OnErrorAsync(err).ConfigureAwait(false);
  92. return;
  93. }
  94. if (next == null)
  95. {
  96. await observer.OnCompletedAsync().ConfigureAwait(false); // REVIEW: Is Empty behavior right here?
  97. return;
  98. }
  99. var nextSubscription = await next.SubscribeSafeAsync(GetSink()).ConfigureAwait(false);
  100. await innerSubscription.AssignAsync(nextSubscription).ConfigureAwait(false);
  101. }
  102. IAsyncObserver<TSource> GetSink() =>
  103. Create<TSource>(
  104. observer.OnNextAsync,
  105. async ex => await NextAsync().ConfigureAwait(false),
  106. NextAsync
  107. );
  108. var disposeEnumerator = AsyncDisposable.Create(() =>
  109. {
  110. handlers.Dispose();
  111. return default;
  112. });
  113. var subscription = StableCompositeAsyncDisposable.Create(innerSubscription, disposeEnumerator);
  114. return (GetSink(), subscription);
  115. }
  116. }
  117. }