Concat.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. 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<TSource>(async observer =>
  19. {
  20. var (sink, inner) = AsyncObserver.Concat(observer, second);
  21. var subscription = await first.SubscribeSafeAsync(sink).ConfigureAwait(false);
  22. return StableCompositeAsyncDisposable.Create(subscription, inner);
  23. });
  24. }
  25. public static IAsyncObservable<TSource> Concat<TSource>(params IAsyncObservable<TSource>[] sources) => Concat((IEnumerable<IAsyncObservable<TSource>>)sources);
  26. public static IAsyncObservable<TSource> Concat<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.Concat(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) Concat<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. observer.OnErrorAsync,
  56. async () =>
  57. {
  58. var secondSubscription = await second.SubscribeSafeAsync(observer).ConfigureAwait(false);
  59. await subscription.AssignAsync(secondSubscription).ConfigureAwait(false);
  60. }
  61. );
  62. return (sink, subscription);
  63. }
  64. public static (IAsyncObserver<TSource>, IAsyncDisposable) Concat<TSource>(IAsyncObserver<TSource> observer, IEnumerator<IAsyncObservable<TSource>> handlers)
  65. {
  66. if (observer == null)
  67. throw new ArgumentNullException(nameof(observer));
  68. if (handlers == null)
  69. throw new ArgumentNullException(nameof(handlers));
  70. var innerSubscription = new SerialAsyncDisposable();
  71. IAsyncObserver<TSource> GetSink() =>
  72. Create<TSource>(
  73. observer.OnNextAsync,
  74. observer.OnErrorAsync,
  75. async () =>
  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. );
  99. var disposeEnumerator = AsyncDisposable.Create(() =>
  100. {
  101. handlers.Dispose();
  102. return Task.CompletedTask;
  103. });
  104. var subscription = StableCompositeAsyncDisposable.Create(innerSubscription, disposeEnumerator);
  105. return (GetSink(), subscription);
  106. }
  107. }
  108. }