OnErrorResumeNext.cs 5.3 KB

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