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