Observable.Conversions.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.Concurrency;
  6. namespace System.Reactive.Linq
  7. {
  8. public static partial class Observable
  9. {
  10. #region + Subscribe +
  11. /// <summary>
  12. /// Subscribes an observer to an enumerable sequence.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  15. /// <param name="source">Enumerable sequence to subscribe to.</param>
  16. /// <param name="observer">Observer that will receive notifications from the enumerable sequence.</param>
  17. /// <returns>Disposable object that can be used to unsubscribe the observer from the enumerable</returns>
  18. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="observer"/> is null.</exception>
  19. public static IDisposable Subscribe<TSource>(this IEnumerable<TSource> source, IObserver<TSource> observer)
  20. {
  21. if (source == null)
  22. throw new ArgumentNullException(nameof(source));
  23. if (observer == null)
  24. throw new ArgumentNullException(nameof(observer));
  25. return s_impl.Subscribe<TSource>(source, observer);
  26. }
  27. /// <summary>
  28. /// Subscribes an observer to an enumerable sequence, using the specified scheduler to run the enumeration loop.
  29. /// </summary>
  30. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  31. /// <param name="source">Enumerable sequence to subscribe to.</param>
  32. /// <param name="observer">Observer that will receive notifications from the enumerable sequence.</param>
  33. /// <param name="scheduler">Scheduler to perform the enumeration on.</param>
  34. /// <returns>Disposable object that can be used to unsubscribe the observer from the enumerable</returns>
  35. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="observer"/> or <paramref name="scheduler"/> is null.</exception>
  36. public static IDisposable Subscribe<TSource>(this IEnumerable<TSource> source, IObserver<TSource> observer, IScheduler scheduler)
  37. {
  38. if (source == null)
  39. throw new ArgumentNullException(nameof(source));
  40. if (observer == null)
  41. throw new ArgumentNullException(nameof(observer));
  42. if (scheduler == null)
  43. throw new ArgumentNullException(nameof(scheduler));
  44. return s_impl.Subscribe<TSource>(source, observer, scheduler);
  45. }
  46. #endregion
  47. #region + ToEnumerable +
  48. /// <summary>
  49. /// Converts an observable sequence to an enumerable sequence.
  50. /// </summary>
  51. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  52. /// <param name="source">An observable sequence to convert to an enumerable sequence.</param>
  53. /// <returns>The enumerable sequence containing the elements in the observable sequence.</returns>
  54. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  55. public static IEnumerable<TSource> ToEnumerable<TSource>(this IObservable<TSource> source)
  56. {
  57. if (source == null)
  58. throw new ArgumentNullException(nameof(source));
  59. return s_impl.ToEnumerable<TSource>(source);
  60. }
  61. #endregion
  62. #region ToEvent
  63. /// <summary>
  64. /// Exposes an observable sequence as an object with an Action-based .NET event.
  65. /// </summary>
  66. /// <param name="source">Observable source sequence.</param>
  67. /// <returns>The event source object.</returns>
  68. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  69. public static IEventSource<Unit> ToEvent(this IObservable<Unit> source)
  70. {
  71. if (source == null)
  72. throw new ArgumentNullException(nameof(source));
  73. return s_impl.ToEvent(source);
  74. }
  75. /// <summary>
  76. /// Exposes an observable sequence as an object with an Action&lt;TSource&gt;-based .NET event.
  77. /// </summary>
  78. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  79. /// <param name="source">Observable source sequence.</param>
  80. /// <returns>The event source object.</returns>
  81. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  82. public static IEventSource<TSource> ToEvent<TSource>(this IObservable<TSource> source)
  83. {
  84. if (source == null)
  85. throw new ArgumentNullException(nameof(source));
  86. return s_impl.ToEvent<TSource>(source);
  87. }
  88. #endregion
  89. #region ToEventPattern
  90. /// <summary>
  91. /// Exposes an observable sequence as an object with a .NET event, conforming to the standard .NET event pattern.
  92. /// </summary>
  93. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  94. /// <param name="source">Observable source sequence.</param>
  95. /// <returns>The event source object.</returns>
  96. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  97. public static IEventPatternSource<TEventArgs> ToEventPattern<TEventArgs>(this IObservable<EventPattern<TEventArgs>> source)
  98. #if !NO_EVENTARGS_CONSTRAINT
  99. where TEventArgs : EventArgs
  100. #endif
  101. {
  102. if (source == null)
  103. throw new ArgumentNullException(nameof(source));
  104. return s_impl.ToEventPattern<TEventArgs>(source);
  105. }
  106. #endregion
  107. #region + ToObservable +
  108. /// <summary>
  109. /// Converts an enumerable sequence to an observable sequence.
  110. /// </summary>
  111. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  112. /// <param name="source">Enumerable sequence to convert to an observable sequence.</param>
  113. /// <returns>The observable sequence whose elements are pulled from the given enumerable sequence.</returns>
  114. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  115. public static IObservable<TSource> ToObservable<TSource>(this IEnumerable<TSource> source)
  116. {
  117. if (source == null)
  118. throw new ArgumentNullException(nameof(source));
  119. return s_impl.ToObservable<TSource>(source);
  120. }
  121. /// <summary>
  122. /// Converts an enumerable sequence to an observable sequence, using the specified scheduler to run the enumeration loop.
  123. /// </summary>
  124. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  125. /// <param name="source">Enumerable sequence to convert to an observable sequence.</param>
  126. /// <param name="scheduler">Scheduler to run the enumeration of the input sequence on.</param>
  127. /// <returns>The observable sequence whose elements are pulled from the given enumerable sequence.</returns>
  128. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="scheduler"/> is null.</exception>
  129. public static IObservable<TSource> ToObservable<TSource>(this IEnumerable<TSource> source, IScheduler scheduler)
  130. {
  131. if (source == null)
  132. throw new ArgumentNullException(nameof(source));
  133. if (scheduler == null)
  134. throw new ArgumentNullException(nameof(scheduler));
  135. return s_impl.ToObservable<TSource>(source, scheduler);
  136. }
  137. #endregion
  138. }
  139. }