Concat.cs 5.1 KB

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