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