Qbservable.Joins.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. #pragma warning disable 1591
  3. using System.Collections.Generic;
  4. using System.Reactive.Joins;
  5. using System.Linq.Expressions;
  6. using System.Reflection;
  7. using System.Linq;
  8. namespace System.Reactive.Linq
  9. {
  10. public static partial class Qbservable
  11. {
  12. /* NOTE: Keep XML docs consistent with the corresponding Observable methods (modulo the IQbservableProvider parameters of course). */
  13. /// <summary>
  14. /// Creates a pattern that matches when both observable sequences have an available element.
  15. /// </summary>
  16. /// <typeparam name="TLeft">The type of the elements in the left sequence.</typeparam>
  17. /// <typeparam name="TRight">The type of the elements in the right sequence.</typeparam>
  18. /// <param name="left">Observable sequence to match with the right sequence.</param>
  19. /// <param name="right">Observable sequence to match with the left sequence.</param>
  20. /// <returns>Pattern object that matches when both observable sequences have an available element.</returns>
  21. /// <exception cref="ArgumentNullException"><paramref name="left"/> or <paramref name="right"/> is null.</exception>
  22. public static QueryablePattern<TLeft, TRight> And<TLeft, TRight>(this IQbservable<TLeft> left, IObservable<TRight> right)
  23. {
  24. if (left == null)
  25. throw new ArgumentNullException("left");
  26. if (right == null)
  27. throw new ArgumentNullException("right");
  28. return new QueryablePattern<TLeft, TRight>(
  29. Expression.Call(
  30. null,
  31. #if CRIPPLED_REFLECTION
  32. InfoOf(() => Qbservable.And<TLeft, TRight>(default(IQbservable<TLeft>), default(IObservable<TRight>))),
  33. #else
  34. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TLeft), typeof(TRight)),
  35. #endif
  36. left.Expression,
  37. GetSourceExpression(right)
  38. )
  39. );
  40. }
  41. /// <summary>
  42. /// Matches when the observable sequence has an available element and projects the element by invoking the selector function.
  43. /// </summary>
  44. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  45. /// <typeparam name="TResult">The type of the elements in the result sequence, returned by the selector function.</typeparam>
  46. /// <param name="source">Observable sequence to apply the selector on.</param>
  47. /// <param name="selector">Selector that will be invoked for elements in the source sequence.</param>
  48. /// <returns>Plan that produces the projected results, to be fed (with other plans) to the When operator.</returns>
  49. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  50. public static QueryablePlan<TResult> Then<TSource, TResult>(this IQbservable<TSource> source, Expression<Func<TSource, TResult>> selector)
  51. {
  52. if (source == null)
  53. throw new ArgumentNullException("source");
  54. if (selector == null)
  55. throw new ArgumentNullException("selector");
  56. return new QueryablePlan<TResult>(
  57. Expression.Call(
  58. null,
  59. #if CRIPPLED_REFLECTION
  60. InfoOf(() => Qbservable.Then<TSource, TResult>(default(IQbservable<TSource>), default(Expression<Func<TSource, TResult>>))),
  61. #else
  62. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TResult)),
  63. #endif
  64. source.Expression,
  65. selector
  66. )
  67. );
  68. }
  69. /// <summary>
  70. /// Joins together the results from several patterns.
  71. /// </summary>
  72. /// <typeparam name="TResult">The type of the elements in the result sequence, obtained from the specified patterns.</typeparam>
  73. /// <param name="provider">Query provider used to construct the IQbservable&lt;T&gt; data source.</param>
  74. /// <param name="plans">A series of plans created by use of the Then operator on patterns.</param>
  75. /// <returns>An observable sequence with the results from matching several patterns.</returns>
  76. /// <exception cref="ArgumentNullException"><paramref name="provider"/> or <paramref name="plans"/> is null.</exception>
  77. public static IQbservable<TResult> When<TResult>(this IQbservableProvider provider, params QueryablePlan<TResult>[] plans)
  78. {
  79. if (provider == null)
  80. throw new ArgumentNullException("provider");
  81. if (plans == null)
  82. throw new ArgumentNullException("plans");
  83. return provider.CreateQuery<TResult>(
  84. Expression.Call(
  85. null,
  86. #if CRIPPLED_REFLECTION
  87. InfoOf(() => Qbservable.When<TResult>(default(IQbservableProvider), default(QueryablePlan<TResult>[]))),
  88. #else
  89. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
  90. #endif
  91. Expression.Constant(provider, typeof(IQbservableProvider)),
  92. Expression.NewArrayInit(
  93. typeof(QueryablePlan<TResult>),
  94. plans.Select(p => p.Expression)
  95. )
  96. )
  97. );
  98. }
  99. /// <summary>
  100. /// Joins together the results from several patterns.
  101. /// </summary>
  102. /// <typeparam name="TResult">The type of the elements in the result sequence, obtained from the specified patterns.</typeparam>
  103. /// <param name="provider">Query provider used to construct the IQbservable&lt;T&gt; data source.</param>
  104. /// <param name="plans">A series of plans created by use of the Then operator on patterns.</param>
  105. /// <returns>An observable sequence with the results form matching several patterns.</returns>
  106. /// <exception cref="ArgumentNullException"><paramref name="provider"/> or <paramref name="plans"/> is null.</exception>
  107. public static IQbservable<TResult> When<TResult>(this IQbservableProvider provider, IEnumerable<QueryablePlan<TResult>> plans)
  108. {
  109. if (provider == null)
  110. throw new ArgumentNullException("provider");
  111. if (plans == null)
  112. throw new ArgumentNullException("plans");
  113. return provider.CreateQuery<TResult>(
  114. Expression.Call(
  115. null,
  116. #if CRIPPLED_REFLECTION
  117. InfoOf(() => Qbservable.When<TResult>(default(IQbservableProvider), default(IEnumerable<QueryablePlan<TResult>>))),
  118. #else
  119. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)),
  120. #endif
  121. Expression.Constant(provider, typeof(IQbservableProvider)),
  122. Expression.Constant(plans, typeof(IEnumerable<QueryablePlan<TResult>>))
  123. )
  124. );
  125. }
  126. }
  127. }
  128. #pragma warning restore 1591