1
0

ObservableEx.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. /// <summary>
  7. /// Provides a set of static methods for writing in-memory queries over observable sequences.
  8. /// </summary>
  9. public static class ObservableEx
  10. {
  11. private static IQueryLanguageEx s_impl = QueryServices.GetQueryImpl<IQueryLanguageEx>(new QueryLanguageEx());
  12. #region Create
  13. /// <summary>
  14. /// Subscribes to each observable sequence returned by the iteratorMethod in sequence and returns the observable sequence of values sent to the observer given to the iteratorMethod.
  15. /// </summary>
  16. /// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
  17. /// <param name="iteratorMethod">Iterator method that produces elements in the resulting sequence by calling the given observer.</param>
  18. /// <returns>An observable sequence obtained by running the iterator and returning the elements that were sent to the observer.</returns>
  19. /// <exception cref="ArgumentNullException"><paramref name="iteratorMethod"/> is null.</exception>
  20. [Experimental]
  21. public static IObservable<TResult> Create<TResult>(Func<IObserver<TResult>, IEnumerable<IObservable<object>>> iteratorMethod)
  22. {
  23. if (iteratorMethod == null)
  24. throw new ArgumentNullException("iteratorMethod");
  25. return s_impl.Create<TResult>(iteratorMethod);
  26. }
  27. /// <summary>
  28. /// Subscribes to each observable sequence returned by the iteratorMethod in sequence and produces a Unit value on the resulting sequence for each step of the iteration.
  29. /// </summary>
  30. /// <param name="iteratorMethod">Iterator method that drives the resulting observable sequence.</param>
  31. /// <returns>An observable sequence obtained by running the iterator and returning Unit values for each iteration step.</returns>
  32. /// <exception cref="ArgumentNullException"><paramref name="iteratorMethod"/> is null.</exception>
  33. [Experimental]
  34. public static IObservable<Unit> Create(Func<IEnumerable<IObservable<object>>> iteratorMethod)
  35. {
  36. if (iteratorMethod == null)
  37. throw new ArgumentNullException("iteratorMethod");
  38. return s_impl.Create(iteratorMethod);
  39. }
  40. #endregion
  41. #region Expand
  42. /// <summary>
  43. /// Expands an observable sequence by recursively invoking selector, using the specified scheduler to enumerate the queue of obtained sequences.
  44. /// </summary>
  45. /// <typeparam name="TSource">The type of the elements in the source sequence and each of the recursively expanded sources obtained by running the selector function.</typeparam>
  46. /// <param name="source">Source sequence with the initial elements.</param>
  47. /// <param name="selector">Selector function to invoke for each produced element, resulting in another sequence to which the selector will be invoked recursively again.</param>
  48. /// <param name="scheduler">Scheduler on which to perform the expansion by enumerating the internal queue of obtained sequences.</param>
  49. /// <returns>An observable sequence containing all the elements produced by the recursive expansion.</returns>
  50. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> or <paramref name="scheduler"/> is null.</exception>
  51. [Experimental]
  52. public static IObservable<TSource> Expand<TSource>(this IObservable<TSource> source, Func<TSource, IObservable<TSource>> selector, IScheduler scheduler)
  53. {
  54. if (source == null)
  55. throw new ArgumentNullException("source");
  56. if (selector == null)
  57. throw new ArgumentNullException("selector");
  58. if (scheduler == null)
  59. throw new ArgumentNullException("scheduler");
  60. return s_impl.Expand<TSource>(source, selector, scheduler);
  61. }
  62. /// <summary>
  63. /// Expands an observable sequence by recursively invoking selector.
  64. /// </summary>
  65. /// <typeparam name="TSource">The type of the elements in the source sequence and each of the recursively expanded sources obtained by running the selector function.</typeparam>
  66. /// <param name="source">Source sequence with the initial elements.</param>
  67. /// <param name="selector">Selector function to invoke for each produced element, resulting in another sequence to which the selector will be invoked recursively again.</param>
  68. /// <returns>An observable sequence containing all the elements produced by the recursive expansion.</returns>
  69. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  70. [Experimental]
  71. public static IObservable<TSource> Expand<TSource>(this IObservable<TSource> source, Func<TSource, IObservable<TSource>> selector)
  72. {
  73. if (source == null)
  74. throw new ArgumentNullException("source");
  75. if (selector == null)
  76. throw new ArgumentNullException("selector");
  77. return s_impl.Expand<TSource>(source, selector);
  78. }
  79. #endregion
  80. #region ForkJoin
  81. /// <summary>
  82. /// Runs two observable sequences in parallel and combines their last elemenets.
  83. /// </summary>
  84. /// <typeparam name="TSource1">The type of the elements in the first source sequence.</typeparam>
  85. /// <typeparam name="TSource2">The type of the elements in the second source sequence.</typeparam>
  86. /// <typeparam name="TResult">The type of the elements in the result sequence, returned by the selector function.</typeparam>
  87. /// <param name="first">First observable sequence.</param>
  88. /// <param name="second">Second observable sequence.</param>
  89. /// <param name="resultSelector">Result selector function to invoke with the last elements of both sequences.</param>
  90. /// <returns>An observable sequence with the result of calling the selector function with the last elements of both input sequences.</returns>
  91. /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> or <paramref name="resultSelector"/> is null.</exception>
  92. [Experimental]
  93. public static IObservable<TResult> ForkJoin<TSource1, TSource2, TResult>(this IObservable<TSource1> first, IObservable<TSource2> second, Func<TSource1, TSource2, TResult> resultSelector)
  94. {
  95. if (first == null)
  96. throw new ArgumentNullException("first");
  97. if (second == null)
  98. throw new ArgumentNullException("second");
  99. if (resultSelector == null)
  100. throw new ArgumentNullException("resultSelector");
  101. return s_impl.ForkJoin<TSource1, TSource2, TResult>(first, second, resultSelector);
  102. }
  103. /// <summary>
  104. /// Runs all specified observable sequences in parallel and collects their last elements.
  105. /// </summary>
  106. /// <typeparam name="TSource">The type of the elements in the source sequences.</typeparam>
  107. /// <param name="sources">Observable sequence to collect the last elements for.</param>
  108. /// <returns>An observable sequence with an array collecting the last elements of all the input sequences.</returns>
  109. /// <exception cref="ArgumentNullException"><paramref name="sources"/> is null.</exception>
  110. [Experimental]
  111. public static IObservable<TSource[]> ForkJoin<TSource>(params IObservable<TSource>[] sources)
  112. {
  113. if (sources == null)
  114. throw new ArgumentNullException("sources");
  115. return s_impl.ForkJoin<TSource>(sources);
  116. }
  117. /// <summary>
  118. /// Runs all observable sequences in the enumerable sources sequence in parallel and collect their last elements.
  119. /// </summary>
  120. /// <typeparam name="TSource">The type of the elements in the source sequences.</typeparam>
  121. /// <param name="sources">Observable sequence to collect the last elements for.</param>
  122. /// <returns>An observable sequence with an array collecting the last elements of all the input sequences.</returns>
  123. /// <exception cref="ArgumentNullException"><paramref name="sources"/> is null.</exception>
  124. [Experimental]
  125. public static IObservable<TSource[]> ForkJoin<TSource>(this IEnumerable<IObservable<TSource>> sources)
  126. {
  127. if (sources == null)
  128. throw new ArgumentNullException("sources");
  129. return s_impl.ForkJoin<TSource>(sources);
  130. }
  131. #endregion
  132. #region Let
  133. /// <summary>
  134. /// Returns an observable sequence that is the result of invoking the selector on the source sequence, without sharing subscriptions.
  135. /// This operator allows for a fluent style of writing queries that use the same sequence multiple times.
  136. /// </summary>
  137. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  138. /// <typeparam name="TResult">The type of the elements in the result sequence.</typeparam>
  139. /// <param name="source">Source sequence that will be shared in the selector function.</param>
  140. /// <param name="selector">Selector function which can use the source sequence as many times as needed, without sharing subscriptions to the source sequence.</param>
  141. /// <returns>An observable sequence that contains the elements of a sequence produced by multicasting the source sequence within a selector function.</returns>
  142. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  143. [Experimental]
  144. public static IObservable<TResult> Let<TSource, TResult>(this IObservable<TSource> source, Func<IObservable<TSource>, IObservable<TResult>> selector)
  145. {
  146. if (source == null)
  147. throw new ArgumentNullException("source");
  148. if (selector == null)
  149. throw new ArgumentNullException("selector");
  150. return s_impl.Let<TSource, TResult>(source, selector);
  151. }
  152. #endregion
  153. #region ManySelect
  154. /// <summary>
  155. /// Comonadic bind operator.
  156. /// </summary>
  157. [Experimental]
  158. public static IObservable<TResult> ManySelect<TSource, TResult>(this IObservable<TSource> source, Func<IObservable<TSource>, TResult> selector, IScheduler scheduler)
  159. {
  160. if (source == null)
  161. throw new ArgumentNullException("source");
  162. if (selector == null)
  163. throw new ArgumentNullException("selector");
  164. if (scheduler == null)
  165. throw new ArgumentNullException("scheduler");
  166. return s_impl.ManySelect<TSource, TResult>(source, selector, scheduler);
  167. }
  168. /// <summary>
  169. /// Comonadic bind operator.
  170. /// </summary>
  171. [Experimental]
  172. public static IObservable<TResult> ManySelect<TSource, TResult>(this IObservable<TSource> source, Func<IObservable<TSource>, TResult> selector)
  173. {
  174. if (source == null)
  175. throw new ArgumentNullException("source");
  176. if (selector == null)
  177. throw new ArgumentNullException("selector");
  178. return s_impl.ManySelect<TSource, TResult>(source, selector);
  179. }
  180. #endregion
  181. #region ToListObservable
  182. /// <summary>
  183. /// Immediately subscribes to source and retains the elements in the observable sequence.
  184. /// </summary>
  185. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  186. /// <param name="source">Source sequence.</param>
  187. /// <returns>Object that's both an observable sequence and a list which can be used to access the source sequence's elements.</returns>
  188. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  189. [Experimental]
  190. public static ListObservable<TSource> ToListObservable<TSource>(this IObservable<TSource> source)
  191. {
  192. if (source == null)
  193. throw new ArgumentNullException("source");
  194. return s_impl.ToListObservable<TSource>(source);
  195. }
  196. #endregion
  197. }
  198. }