Observable.Conversions.cs 7.7 KB

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