Qbservable.Joins.cs 7.1 KB

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