Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Linq/Observable.Blocking.cs

raw
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
5 namespace System.Reactive.Linq
6 {
7 public static partial class Observable
8 {
9 #region + Chunkify +
10
11 /// <summary>
12 /// Produces an enumerable sequence of consecutive (possibly empty) chunks of the source sequence.
13 /// </summary>
14 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
15 /// <param name="source">Source observable sequence.</param>
16 /// <returns>The enumerable sequence that returns consecutive (possibly empty) chunks upon each iteration.</returns>
17 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
18 public static IEnumerable<IList<TSource>> Chunkify<TSource>(this IObservable<TSource> source)
19 {
20 if (source == null)
21 throw new ArgumentNullException("source");
22
23 return s_impl.Chunkify<TSource>(source);
24 }
25
26 #endregion
27
28 #region + Collect +
29
30 /// <summary>
31 /// Produces an enumerable sequence that returns elements collected/aggregated from the source sequence between consecutive iterations.
32 /// </summary>
33 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
34 /// <typeparam name="TResult">The type of the elements produced by the merge operation during collection.</typeparam>
35 /// <param name="source">Source observable sequence.</param>
36 /// <param name="newCollector">Factory to create a new collector object.</param>
37 /// <param name="merge">Merges a sequence element with the current collector.</param>
38 /// <returns>The enumerable sequence that returns collected/aggregated elements from the source sequence upon each iteration.</returns>
39 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="newCollector"/> or <paramref name="merge"/> is null.</exception>
40 public static IEnumerable<TResult> Collect<TSource, TResult>(this IObservable<TSource> source, Func<TResult> newCollector, Func<TResult, TSource, TResult> merge)
41 {
42 if (source == null)
43 throw new ArgumentNullException("source");
44 if (newCollector == null)
45 throw new ArgumentNullException("newCollector");
46 if (merge == null)
47 throw new ArgumentNullException("merge");
48
49 return s_impl.Collect<TSource, TResult>(source, newCollector, merge);
50 }
51
52 /// <summary>
53 /// Produces an enumerable sequence that returns elements collected/aggregated from the source sequence between consecutive iterations.
54 /// </summary>
55 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
56 /// <typeparam name="TResult">The type of the elements produced by the merge operation during collection.</typeparam>
57 /// <param name="source">Source observable sequence.</param>
58 /// <param name="getInitialCollector">Factory to create the initial collector object.</param>
59 /// <param name="merge">Merges a sequence element with the current collector.</param>
60 /// <param name="getNewCollector">Factory to replace the current collector by a new collector.</param>
61 /// <returns>The enumerable sequence that returns collected/aggregated elements from the source sequence upon each iteration.</returns>
62 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="getInitialCollector"/> or <paramref name="merge"/> or <paramref name="getNewCollector"/> is null.</exception>
63 public static IEnumerable<TResult> Collect<TSource, TResult>(this IObservable<TSource> source, Func<TResult> getInitialCollector, Func<TResult, TSource, TResult> merge, Func<TResult, TResult> getNewCollector)
64 {
65 if (source == null)
66 throw new ArgumentNullException("source");
67 if (getInitialCollector == null)
68 throw new ArgumentNullException("getInitialCollector");
69 if (merge == null)
70 throw new ArgumentNullException("merge");
71 if (getNewCollector == null)
72 throw new ArgumentNullException("getNewCollector");
73
74 return s_impl.Collect<TSource, TResult>(source, getInitialCollector, merge, getNewCollector);
75 }
76
77 #endregion
78
79 #region + First +
80
81 /// <summary>
82 /// Returns the first element of an observable sequence.
83 /// </summary>
84 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
85 /// <param name="source">Source observable sequence.</param>
86 /// <returns>The first element in the observable sequence.</returns>
87 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
88 /// <exception cref="InvalidOperationException">The source sequence is empty.</exception>
89 /// <seealso cref="Observable.FirstAsync{TSource}(IObservable{TSource})"/>
90 #if PREFER_ASYNC
91 [Obsolete(Constants_Linq.USE_ASYNC)]
92 #endif
93 public static TSource First<TSource>(this IObservable<TSource> source)
94 {
95 if (source == null)
96 throw new ArgumentNullException("source");
97
98 return s_impl.First<TSource>(source);
99 }
100
101 /// <summary>
102 /// Returns the first element of an observable sequence that satisfies the condition in the predicate.
103 /// </summary>
104 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
105 /// <param name="source">Source observable sequence.</param>
106 /// <param name="predicate">A predicate function to evaluate for elements in the source sequence.</param>
107 /// <returns>The first element in the observable sequence that satisfies the condition in the predicate.</returns>
108 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
109 /// <exception cref="InvalidOperationException">No element satisfies the condition in the predicate. -or- The source sequence is empty.</exception>
110 /// <seealso cref="Observable.FirstAsync{TSource}(IObservable{TSource}, Func{TSource, bool})"/>
111 #if PREFER_ASYNC
112 [Obsolete(Constants_Linq.USE_ASYNC)]
113 #endif
114 public static TSource First<TSource>(this IObservable<TSource> source, Func<TSource, bool> predicate)
115 {
116 if (source == null)
117 throw new ArgumentNullException("source");
118 if (predicate == null)
119 throw new ArgumentNullException("predicate");
120
121 return s_impl.First<TSource>(source, predicate);
122 }
123
124 #endregion
125
126 #region + FirstOrDefault +
127
128 /// <summary>
129 /// Returns the first element of an observable sequence, or a default value if no such element exists.
130 /// </summary>
131 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
132 /// <param name="source">Source observable sequence.</param>
133 /// <returns>The first element in the observable sequence, or a default value if no such element exists.</returns>
134 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
135 /// <seealso cref="Observable.FirstOrDefaultAsync{TSource}(IObservable{TSource})"/>
136 #if PREFER_ASYNC
137 [Obsolete(Constants_Linq.USE_ASYNC)]
138 #endif
139 public static TSource FirstOrDefault<TSource>(this IObservable<TSource> source)
140 {
141 if (source == null)
142 throw new ArgumentNullException("source");
143
144 return s_impl.FirstOrDefault<TSource>(source);
145 }
146
147 /// <summary>
148 /// Returns the first element of an observable sequence that satisfies the condition in the predicate, or a default value if no such element exists.
149 /// </summary>
150 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
151 /// <param name="source">Source observable sequence.</param>
152 /// <param name="predicate">A predicate function to evaluate for elements in the source sequence.</param>
153 /// <returns>The first element in the observable sequence that satisfies the condition in the predicate, or a default value if no such element exists.</returns>
154 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
155 /// <seealso cref="Observable.FirstOrDefaultAsync{TSource}(IObservable{TSource}, Func{TSource, bool})"/>
156 #if PREFER_ASYNC
157 [Obsolete(Constants_Linq.USE_ASYNC)]
158 #endif
159 public static TSource FirstOrDefault<TSource>(this IObservable<TSource> source, Func<TSource, bool> predicate)
160 {
161 if (source == null)
162 throw new ArgumentNullException("source");
163 if (predicate == null)
164 throw new ArgumentNullException("predicate");
165
166 return s_impl.FirstOrDefault<TSource>(source, predicate);
167 }
168
169 #endregion
170
171 #region + ForEach +
172
173 /// <summary>
174 /// Invokes an action for each element in the observable sequence, and blocks until the sequence is terminated.
175 /// </summary>
176 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
177 /// <param name="source">Source sequence.</param>
178 /// <param name="onNext">Action to invoke for each element in the observable sequence.</param>
179 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> is null.</exception>
180 /// <remarks>Because of its blocking nature, this operator is mainly used for testing.</remarks>
181 #if PREFER_ASYNC
182 [Obsolete(Constants_Linq.USE_ASYNC)]
183 #endif
184 public static void ForEach<TSource>(this IObservable<TSource> source, Action<TSource> onNext)
185 {
186 if (source == null)
187 throw new ArgumentNullException("source");
188 if (onNext == null)
189 throw new ArgumentNullException("onNext");
190
191 s_impl.ForEach<TSource>(source, onNext);
192 }
193
194 /// <summary>
195 /// Invokes an action for each element in the observable sequence, incorporating the element's index, and blocks until the sequence is terminated.
196 /// </summary>
197 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
198 /// <param name="source">Source sequence.</param>
199 /// <param name="onNext">Action to invoke for each element in the observable sequence.</param>
200 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> is null.</exception>
201 /// <remarks>Because of its blocking nature, this operator is mainly used for testing.</remarks>
202 #if PREFER_ASYNC
203 [Obsolete(Constants_Linq.USE_ASYNC)]
204 #endif
205 public static void ForEach<TSource>(this IObservable<TSource> source, Action<TSource, int> onNext)
206 {
207 if (source == null)
208 throw new ArgumentNullException("source");
209 if (onNext == null)
210 throw new ArgumentNullException("onNext");
211
212 s_impl.ForEach<TSource>(source, onNext);
213 }
214
215 #endregion
216
217 #region + GetEnumerator +
218
219 /// <summary>
220 /// Returns an enumerator that enumerates all values of the observable sequence.
221 /// </summary>
222 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
223 /// <param name="source">An observable sequence to get an enumerator for.</param>
224 /// <returns>The enumerator that can be used to enumerate over the elements in the observable sequence.</returns>
225 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
226 public static IEnumerator<TSource> GetEnumerator<TSource>(this IObservable<TSource> source)
227 {
228 if (source == null)
229 throw new ArgumentNullException("source");
230
231 return s_impl.GetEnumerator<TSource>(source);
232 }
233
234 #endregion
235
236 #region + Last +
237
238 /// <summary>
239 /// Returns the last element of an observable sequence.
240 /// </summary>
241 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
242 /// <param name="source">Source observable sequence.</param>
243 /// <returns>The last element in the observable sequence.</returns>
244 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
245 /// <exception cref="InvalidOperationException">The source sequence is empty.</exception>
246 /// <seealso cref="Observable.LastAsync{TSource}(IObservable{TSource})"/>
247 #if PREFER_ASYNC
248 [Obsolete(Constants_Linq.USE_ASYNC)]
249 #endif
250 public static TSource Last<TSource>(this IObservable<TSource> source)
251 {
252 if (source == null)
253 throw new ArgumentNullException("source");
254
255 return s_impl.Last<TSource>(source);
256 }
257
258 /// <summary>
259 /// Returns the last element of an observable sequence that satisfies the condition in the predicate.
260 /// </summary>
261 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
262 /// <param name="source">Source observable sequence.</param>
263 /// <param name="predicate">A predicate function to evaluate for elements in the source sequence.</param>
264 /// <returns>The last element in the observable sequence that satisfies the condition in the predicate.</returns>
265 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
266 /// <exception cref="InvalidOperationException">No element satisfies the condition in the predicate. -or- The source sequence is empty.</exception>
267 /// <seealso cref="Observable.LastAsync{TSource}(IObservable{TSource}, Func{TSource, bool})"/>
268 #if PREFER_ASYNC
269 [Obsolete(Constants_Linq.USE_ASYNC)]
270 #endif
271 public static TSource Last<TSource>(this IObservable<TSource> source, Func<TSource, bool> predicate)
272 {
273 if (source == null)
274 throw new ArgumentNullException("source");
275 if (predicate == null)
276 throw new ArgumentNullException("predicate");
277
278 return s_impl.Last<TSource>(source, predicate);
279 }
280
281 #endregion
282
283 #region + LastOrDefault +
284
285 /// <summary>
286 /// Returns the last element of an observable sequence, or a default value if no such element exists.
287 /// </summary>
288 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
289 /// <param name="source">Source observable sequence.</param>
290 /// <returns>The last element in the observable sequence, or a default value if no such element exists.</returns>
291 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
292 /// <seealso cref="Observable.LastOrDefaultAsync{TSource}(IObservable{TSource})"/>
293 #if PREFER_ASYNC
294 [Obsolete(Constants_Linq.USE_ASYNC)]
295 #endif
296 public static TSource LastOrDefault<TSource>(this IObservable<TSource> source)
297 {
298 if (source == null)
299 throw new ArgumentNullException("source");
300
301 return s_impl.LastOrDefault<TSource>(source);
302 }
303
304 /// <summary>
305 /// Returns the last element of an observable sequence that satisfies the condition in the predicate, or a default value if no such element exists.
306 /// </summary>
307 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
308 /// <param name="source">Source observable sequence.</param>
309 /// <param name="predicate">A predicate function to evaluate for elements in the source sequence.</param>
310 /// <returns>The last element in the observable sequence that satisfies the condition in the predicate, or a default value if no such element exists.</returns>
311 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
312 /// <seealso cref="Observable.LastOrDefaultAsync{TSource}(IObservable{TSource}, Func{TSource, bool})"/>
313 #if PREFER_ASYNC
314 [Obsolete(Constants_Linq.USE_ASYNC)]
315 #endif
316 public static TSource LastOrDefault<TSource>(this IObservable<TSource> source, Func<TSource, bool> predicate)
317 {
318 if (source == null)
319 throw new ArgumentNullException("source");
320 if (predicate == null)
321 throw new ArgumentNullException("predicate");
322
323 return s_impl.LastOrDefault<TSource>(source, predicate);
324 }
325
326 #endregion
327
328 #region + Latest +
329
330 /// <summary>
331 /// Returns an enumerable sequence whose enumeration returns the latest observed element in the source observable sequence.
332 /// Enumerators on the resulting sequence will never produce the same element repeatedly, and will block until the next element becomes available.
333 /// </summary>
334 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
335 /// <param name="source">Source observable sequence.</param>
336 /// <returns>The enumerable sequence that returns the last sampled element upon each iteration and subsequently blocks until the next element in the observable source sequence becomes available.</returns>
337 public static IEnumerable<TSource> Latest<TSource>(this IObservable<TSource> source)
338 {
339 if (source == null)
340 throw new ArgumentNullException("source");
341
342 return s_impl.Latest<TSource>(source);
343 }
344
345 #endregion
346
347 #region + MostRecent +
348
349 /// <summary>
350 /// Returns an enumerable sequence whose enumeration returns the most recently observed element in the source observable sequence, using the specified initial value in case no element has been sampled yet.
351 /// Enumerators on the resulting sequence never block and can produce the same element repeatedly.
352 /// </summary>
353 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
354 /// <param name="source">Source observable sequence.</param>
355 /// <param name="initialValue">Initial value that will be yielded by the enumerable sequence if no element has been sampled yet.</param>
356 /// <returns>The enumerable sequence that returns the last sampled element upon each iteration.</returns>
357 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
358 public static IEnumerable<TSource> MostRecent<TSource>(this IObservable<TSource> source, TSource initialValue)
359 {
360 if (source == null)
361 throw new ArgumentNullException("source");
362
363 return s_impl.MostRecent<TSource>(source, initialValue);
364 }
365
366 #endregion
367
368 #region + Next +
369
370 /// <summary>
371 /// Returns an enumerable sequence whose enumeration blocks until the next element in the source observable sequence becomes available.
372 /// Enumerators on the resulting sequence will block until the next element becomes available.
373 /// </summary>
374 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
375 /// <param name="source">Source observable sequence.</param>
376 /// <returns>The enumerable sequence that blocks upon each iteration until the next element in the observable source sequence becomes available.</returns>
377 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
378 public static IEnumerable<TSource> Next<TSource>(this IObservable<TSource> source)
379 {
380 if (source == null)
381 throw new ArgumentNullException("source");
382
383 return s_impl.Next<TSource>(source);
384 }
385
386 #endregion
387
388 #region + Single +
389
390 /// <summary>
391 /// Returns the only element of an observable sequence, and throws an exception if there is not exactly one element in the observable sequence.
392 /// </summary>
393 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
394 /// <param name="source">Source observable sequence.</param>
395 /// <returns>The single element in the observable sequence.</returns>
396 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
397 /// <exception cref="InvalidOperationException">The source sequence contains more than one element. -or- The source sequence is empty.</exception>
398 /// <seealso cref="Observable.SingleAsync{TSource}(IObservable{TSource})"/>
399 #if PREFER_ASYNC
400 [Obsolete(Constants_Linq.USE_ASYNC)]
401 #endif
402 public static TSource Single<TSource>(this IObservable<TSource> source)
403 {
404 if (source == null)
405 throw new ArgumentNullException("source");
406
407 return s_impl.Single<TSource>(source);
408 }
409
410 /// <summary>
411 /// Returns the only element of an observable sequence that satisfies the condition in the predicate, and throws an exception if there is not exactly one element matching the predicate in the observable sequence.
412 /// </summary>
413 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
414 /// <param name="source">Source observable sequence.</param>
415 /// <param name="predicate">A predicate function to evaluate for elements in the source sequence.</param>
416 /// <returns>The single element in the observable sequence that satisfies the condition in the predicate.</returns>
417 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
418 /// <exception cref="InvalidOperationException">No element satisfies the condition in the predicate. -or- More than one element satisfies the condition in the predicate. -or- The source sequence is empty.</exception>
419 /// <seealso cref="Observable.SingleAsync{TSource}(IObservable{TSource}, Func{TSource, bool})"/>
420 #if PREFER_ASYNC
421 [Obsolete(Constants_Linq.USE_ASYNC)]
422 #endif
423 public static TSource Single<TSource>(this IObservable<TSource> source, Func<TSource, bool> predicate)
424 {
425 if (source == null)
426 throw new ArgumentNullException("source");
427 if (predicate == null)
428 throw new ArgumentNullException("predicate");
429
430 return s_impl.Single<TSource>(source, predicate);
431 }
432
433 #endregion
434
435 #region + SingleOrDefault +
436
437 /// <summary>
438 /// Returns the only element of an observable sequence, or a default value if the observable sequence is empty; this method throws an exception if there is more than one element in the observable sequence.
439 /// </summary>
440 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
441 /// <param name="source">Source observable sequence.</param>
442 /// <returns>The single element in the observable sequence, or a default value if no such element exists.</returns>
443 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
444 /// <exception cref="InvalidOperationException">The source sequence contains more than one element.</exception>
445 /// <seealso cref="Observable.SingleOrDefaultAsync{TSource}(IObservable{TSource})"/>
446 #if PREFER_ASYNC
447 [Obsolete(Constants_Linq.USE_ASYNC)]
448 #endif
449 public static TSource SingleOrDefault<TSource>(this IObservable<TSource> source)
450 {
451 if (source == null)
452 throw new ArgumentNullException("source");
453
454 return s_impl.SingleOrDefault<TSource>(source);
455 }
456
457 /// <summary>
458 /// Returns the only element of an observable sequence that satisfies the condition in the predicate, or a default value if no such element exists; this method throws an exception if there is more than one element matching the predicate in the observable sequence.
459 /// </summary>
460 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
461 /// <param name="source">Source observable sequence.</param>
462 /// <param name="predicate">A predicate function to evaluate for elements in the source sequence.</param>
463 /// <returns>The single element in the observable sequence that satisfies the condition in the predicate, or a default value if no such element exists.</returns>
464 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
465 /// <exception cref="InvalidOperationException">The sequence contains more than one element that satisfies the condition in the predicate.</exception>
466 /// <seealso cref="Observable.SingleOrDefaultAsync{TSource}(IObservable{TSource}, Func{TSource, bool})"/>
467 #if PREFER_ASYNC
468 [Obsolete(Constants_Linq.USE_ASYNC)]
469 #endif
470 public static TSource SingleOrDefault<TSource>(this IObservable<TSource> source, Func<TSource, bool> predicate)
471 {
472 if (source == null)
473 throw new ArgumentNullException("source");
474 if (predicate == null)
475 throw new ArgumentNullException("predicate");
476
477 return s_impl.SingleOrDefault<TSource>(source, predicate);
478 }
479
480 #endregion
481
482 #region + Wait +
483
484 /// <summary>
485 /// Waits for the observable sequence to complete and returns the last element of the sequence.
486 /// If the sequence terminates with an OnError notification, the exception is throw.
487 /// </summary>
488 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
489 /// <param name="source">Source observable sequence.</param>
490 /// <returns>The last element in the observable sequence.</returns>
491 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
492 /// <exception cref="InvalidOperationException">The source sequence is empty.</exception>
493 public static TSource Wait<TSource>(this IObservable<TSource> source)
494 {
495 if (source == null)
496 throw new ArgumentNullException("source");
497
498 return s_impl.Wait<TSource>(source);
499 }
500
501 #endregion
502 }
503 }