Publish.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.Reactive.Subjects;
  5. using System.Threading.Tasks;
  6. namespace System.Reactive.Linq
  7. {
  8. // REVIEW: Expose Publish using ConcurrentSimpleAsyncSubject<T> or ConcurrentBehaviorAsyncSubject<T> underneath.
  9. public partial class AsyncObservable
  10. {
  11. public static IConnectableAsyncObservable<TSource> Publish<TSource>(this IAsyncObservable<TSource> source)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. return Multicast(source, new SequentialSimpleAsyncSubject<TSource>());
  16. }
  17. public static IConnectableAsyncObservable<TSource> Publish<TSource>(this IAsyncObservable<TSource> source, TSource value)
  18. {
  19. if (source == null)
  20. throw new ArgumentNullException(nameof(source));
  21. return Multicast(source, new SequentialBehaviorAsyncSubject<TSource>(value));
  22. }
  23. public static IAsyncObservable<TResult> Publish<TSource, TResult>(this IAsyncObservable<TSource> source, Func<IAsyncObservable<TSource>, IAsyncObservable<TResult>> selector)
  24. {
  25. if (source == null)
  26. throw new ArgumentNullException(nameof(source));
  27. if (selector == null)
  28. throw new ArgumentNullException(nameof(selector));
  29. return Multicast(source, () => new SequentialSimpleAsyncSubject<TSource>(), selector);
  30. }
  31. public static IAsyncObservable<TResult> Publish<TSource, TResult>(this IAsyncObservable<TSource> source, Func<IAsyncObservable<TSource>, IAsyncObservable<TResult>> selector, TSource value)
  32. {
  33. if (source == null)
  34. throw new ArgumentNullException(nameof(source));
  35. if (selector == null)
  36. throw new ArgumentNullException(nameof(selector));
  37. return Multicast(source, () => new SequentialBehaviorAsyncSubject<TSource>(value), selector);
  38. }
  39. public static IAsyncObservable<TResult> Publish<TSource, TResult>(this IAsyncObservable<TSource> source, Func<IAsyncObservable<TSource>, ValueTask<IAsyncObservable<TResult>>> selector)
  40. {
  41. if (source == null)
  42. throw new ArgumentNullException(nameof(source));
  43. if (selector == null)
  44. throw new ArgumentNullException(nameof(selector));
  45. return Multicast(source, () => new ValueTask<IAsyncSubject<TSource, TSource>>(new SequentialSimpleAsyncSubject<TSource>()), selector);
  46. }
  47. public static IAsyncObservable<TResult> Publish<TSource, TResult>(this IAsyncObservable<TSource> source, Func<IAsyncObservable<TSource>, ValueTask<IAsyncObservable<TResult>>> selector, TSource value)
  48. {
  49. if (source == null)
  50. throw new ArgumentNullException(nameof(source));
  51. if (selector == null)
  52. throw new ArgumentNullException(nameof(selector));
  53. return Multicast(source, () => new ValueTask<IAsyncSubject<TSource, TSource>>(new SequentialBehaviorAsyncSubject<TSource>(value)), selector);
  54. }
  55. }
  56. }