Qbservable.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.Linq;
  7. using System.Linq.Expressions;
  8. using System.Reactive.Concurrency;
  9. using System.Reflection;
  10. namespace System.Reactive.Linq
  11. {
  12. /// <summary>
  13. /// Provides a set of static methods for writing queries over observable sequences, allowing translation to a target query language.
  14. /// </summary>
  15. public static partial class Qbservable
  16. {
  17. /// <summary>
  18. /// Returns the input typed as an <see cref="IObservable{TSource}"/>.
  19. /// This operator is used to separate the part of the query that's captured as an expression tree from the part that's executed locally.
  20. /// </summary>
  21. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  22. /// <param name="source">An <see cref="IQbservable{TSource}"/> sequence to convert to an <see cref="IObservable{TSource}"/> sequence.</param>
  23. /// <returns>The original source object, but typed as an <see cref="IObservable{TSource}"/>.</returns>
  24. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  25. public static IObservable<TSource> AsObservable<TSource>(this IQbservable<TSource> source)
  26. {
  27. if (source == null)
  28. {
  29. throw new ArgumentNullException(nameof(source));
  30. }
  31. return source;
  32. }
  33. /// <summary>
  34. /// Converts an enumerable sequence to an observable sequence.
  35. /// </summary>
  36. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  37. /// <param name="source">Enumerable sequence to convert to an observable sequence.</param>
  38. /// <returns>The observable sequence whose elements are pulled from the given enumerable sequence.</returns>
  39. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  40. /// <remarks>This operator requires the source's <see cref="IQueryProvider"/> object (see <see cref="IQueryable.Provider"/>) to implement <see cref="IQbservableProvider"/>.</remarks>
  41. public static IQbservable<TSource> ToQbservable<TSource>(this IQueryable<TSource> source)
  42. {
  43. if (source == null)
  44. {
  45. throw new ArgumentNullException(nameof(source));
  46. }
  47. return ((IQbservableProvider)source.Provider).CreateQuery<TSource>(
  48. Expression.Call(
  49. null,
  50. #if CRIPPLED_REFLECTION
  51. InfoOf(() => ToQbservable<TSource>(default)),
  52. #else
  53. ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  54. #endif
  55. source.Expression
  56. )
  57. );
  58. }
  59. /// <summary>
  60. /// Converts an enumerable sequence to an observable sequence, using the specified scheduler to run the enumeration loop.
  61. /// </summary>
  62. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  63. /// <param name="source">Enumerable sequence to convert to an observable sequence.</param>
  64. /// <param name="scheduler">Scheduler to run the enumeration of the input sequence on.</param>
  65. /// <returns>The observable sequence whose elements are pulled from the given enumerable sequence.</returns>
  66. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="scheduler"/> is null.</exception>
  67. /// <remarks>This operator requires the source's <see cref="IQueryProvider"/> object (see <see cref="IQueryable.Provider"/>) to implement <see cref="IQbservableProvider"/>.</remarks>
  68. public static IQbservable<TSource> ToQbservable<TSource>(this IQueryable<TSource> source, IScheduler scheduler)
  69. {
  70. if (source == null)
  71. {
  72. throw new ArgumentNullException(nameof(source));
  73. }
  74. if (scheduler == null)
  75. {
  76. throw new ArgumentNullException(nameof(scheduler));
  77. }
  78. return ((IQbservableProvider)source.Provider).CreateQuery<TSource>(
  79. Expression.Call(
  80. null,
  81. #if CRIPPLED_REFLECTION
  82. InfoOf(() => ToQbservable<TSource>(default)),
  83. #else
  84. ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  85. #endif
  86. source.Expression,
  87. Expression.Constant(scheduler)
  88. )
  89. );
  90. }
  91. internal static Expression GetSourceExpression<TSource>(IObservable<TSource> source)
  92. {
  93. if (source is IQbservable<TSource> q)
  94. {
  95. return q.Expression;
  96. }
  97. return Expression.Constant(source, typeof(IObservable<TSource>));
  98. }
  99. internal static Expression GetSourceExpression<TSource>(IEnumerable<TSource> source)
  100. {
  101. if (source is IQueryable<TSource> q)
  102. {
  103. return q.Expression;
  104. }
  105. return Expression.Constant(source, typeof(IEnumerable<TSource>));
  106. }
  107. internal static Expression GetSourceExpression<TSource>(IObservable<TSource>[] sources)
  108. {
  109. return Expression.NewArrayInit(
  110. typeof(IObservable<TSource>),
  111. sources.Select(source => GetSourceExpression(source))
  112. );
  113. }
  114. internal static Expression GetSourceExpression<TSource>(IEnumerable<TSource>[] sources)
  115. {
  116. return Expression.NewArrayInit(
  117. typeof(IEnumerable<TSource>),
  118. sources.Select(source => GetSourceExpression(source))
  119. );
  120. }
  121. internal static MethodInfo InfoOf<R>(Expression<Func<R>> f)
  122. {
  123. return ((MethodCallExpression)f.Body).Method;
  124. }
  125. }
  126. }
  127. #pragma warning restore 1591