Multicast.cs 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.Reactive.Disposables;
  5. using System.Reactive.Subjects;
  6. using System.Threading.Tasks;
  7. namespace System.Reactive.Linq
  8. {
  9. partial class AsyncObservable
  10. {
  11. public static IConnectableAsyncObservable<TResult> Multicast<TSource, TResult>(this IAsyncObservable<TSource> source, IAsyncSubject<TSource, TResult> subject)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. if (subject == null)
  16. throw new ArgumentNullException(nameof(subject));
  17. return new ConnectableAsyncObservable<TSource, TResult>(source, subject);
  18. }
  19. public static IAsyncObservable<TSource> Multicast<TSource>(this IAsyncObservable<TSource> source, Func<IAsyncSubject<TSource>> subjectFactory)
  20. {
  21. if (source == null)
  22. throw new ArgumentNullException(nameof(source));
  23. if (subjectFactory == null)
  24. throw new ArgumentNullException(nameof(subjectFactory));
  25. return Multicast(source, () => Task.FromResult<IAsyncSubject<TSource, TSource>>(subjectFactory()), x => Task.FromResult(x));
  26. }
  27. public static IAsyncObservable<TSource> Multicast<TSource>(this IAsyncObservable<TSource> source, Func<Task<IAsyncSubject<TSource>>> subjectFactory)
  28. {
  29. if (source == null)
  30. throw new ArgumentNullException(nameof(source));
  31. if (subjectFactory == null)
  32. throw new ArgumentNullException(nameof(subjectFactory));
  33. return Multicast<TSource, TSource, TSource>(source, async () => await subjectFactory().ConfigureAwait(false), x => Task.FromResult(x));
  34. }
  35. public static IAsyncObservable<TResult> Multicast<TSource, TIntermediate, TResult>(this IAsyncObservable<TSource> source, Func<IAsyncSubject<TSource, TIntermediate>> subjectFactory, Func<IAsyncObservable<TIntermediate>, IAsyncObservable<TResult>> selector)
  36. {
  37. if (source == null)
  38. throw new ArgumentNullException(nameof(source));
  39. if (subjectFactory == null)
  40. throw new ArgumentNullException(nameof(subjectFactory));
  41. if (selector == null)
  42. throw new ArgumentNullException(nameof(selector));
  43. return Multicast(source, () => Task.FromResult(subjectFactory()), x => Task.FromResult(selector(x)));
  44. }
  45. public static IAsyncObservable<TResult> Multicast<TSource, TIntermediate, TResult>(this IAsyncObservable<TSource> source, Func<Task<IAsyncSubject<TSource, TIntermediate>>> subjectFactory, Func<IAsyncObservable<TIntermediate>, Task<IAsyncObservable<TResult>>> selector)
  46. {
  47. if (source == null)
  48. throw new ArgumentNullException(nameof(source));
  49. if (subjectFactory == null)
  50. throw new ArgumentNullException(nameof(subjectFactory));
  51. if (selector == null)
  52. throw new ArgumentNullException(nameof(selector));
  53. // REVIEW: Use a lifted observer operator.
  54. return Create<TResult>(async observer =>
  55. {
  56. var observable = default(IAsyncObservable<TResult>);
  57. var connectable = default(IConnectableAsyncObservable<TIntermediate>);
  58. try
  59. {
  60. var subject = await subjectFactory().ConfigureAwait(false);
  61. connectable = new ConnectableAsyncObservable<TSource, TIntermediate>(source, subject);
  62. observable = await selector(connectable).ConfigureAwait(false);
  63. }
  64. catch (Exception ex)
  65. {
  66. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  67. return AsyncDisposable.Nop;
  68. }
  69. var d = new CompositeAsyncDisposable();
  70. var subscription = await observable.SubscribeAsync(observer).ConfigureAwait(false);
  71. await d.AddAsync(subscription).ConfigureAwait(false);
  72. var connection = await connectable.ConnectAsync().ConfigureAwait(false);
  73. await d.AddAsync(connection).ConfigureAwait(false);
  74. return d;
  75. });
  76. }
  77. }
  78. }