| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- using System.Collections.Generic;
- using System.Reactive.Concurrency;
- namespace System.Reactive.Linq
- {
- /// <summary>
- /// Provides a set of static methods for writing in-memory queries over observable sequences.
- /// </summary>
- public static class ObservableEx
- {
- #pragma warning disable IDE1006 // Naming Styles: 3rd party code is known to reflect for this specific field name
- private static IQueryLanguageEx s_impl = QueryServices.GetQueryImpl<IQueryLanguageEx>(new QueryLanguageEx());
- #pragma warning restore IDE1006 // Naming Styles
- #region Create
- /// <summary>
- /// 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.
- /// </summary>
- /// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
- /// <param name="iteratorMethod">Iterator method that produces elements in the resulting sequence by calling the given observer.</param>
- /// <returns>An observable sequence obtained by running the iterator and returning the elements that were sent to the observer.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="iteratorMethod"/> is null.</exception>
- [Experimental]
- public static IObservable<TResult> Create<TResult>(Func<IObserver<TResult>, IEnumerable<IObservable<object>>> iteratorMethod)
- {
- if (iteratorMethod == null)
- {
- throw new ArgumentNullException(nameof(iteratorMethod));
- }
- return s_impl.Create(iteratorMethod);
- }
- /// <summary>
- /// 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.
- /// </summary>
- /// <param name="iteratorMethod">Iterator method that drives the resulting observable sequence.</param>
- /// <returns>An observable sequence obtained by running the iterator and returning Unit values for each iteration step.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="iteratorMethod"/> is null.</exception>
- [Experimental]
- public static IObservable<Unit> Create(Func<IEnumerable<IObservable<object>>> iteratorMethod)
- {
- if (iteratorMethod == null)
- {
- throw new ArgumentNullException(nameof(iteratorMethod));
- }
- return s_impl.Create(iteratorMethod);
- }
- #endregion
- #region Expand
- /// <summary>
- /// Expands an observable sequence by recursively invoking selector, using the specified scheduler to enumerate the queue of obtained sequences.
- /// </summary>
- /// <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>
- /// <param name="source">Source sequence with the initial elements.</param>
- /// <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>
- /// <param name="scheduler">Scheduler on which to perform the expansion by enumerating the internal queue of obtained sequences.</param>
- /// <returns>An observable sequence containing all the elements produced by the recursive expansion.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> or <paramref name="scheduler"/> is null.</exception>
- [Experimental]
- public static IObservable<TSource> Expand<TSource>(this IObservable<TSource> source, Func<TSource, IObservable<TSource>> selector, IScheduler scheduler)
- {
- if (source == null)
- {
- throw new ArgumentNullException(nameof(source));
- }
- if (selector == null)
- {
- throw new ArgumentNullException(nameof(selector));
- }
- if (scheduler == null)
- {
- throw new ArgumentNullException(nameof(scheduler));
- }
- return s_impl.Expand(source, selector, scheduler);
- }
- /// <summary>
- /// Expands an observable sequence by recursively invoking selector.
- /// </summary>
- /// <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>
- /// <param name="source">Source sequence with the initial elements.</param>
- /// <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>
- /// <returns>An observable sequence containing all the elements produced by the recursive expansion.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
- [Experimental]
- public static IObservable<TSource> Expand<TSource>(this IObservable<TSource> source, Func<TSource, IObservable<TSource>> selector)
- {
- if (source == null)
- {
- throw new ArgumentNullException(nameof(source));
- }
- if (selector == null)
- {
- throw new ArgumentNullException(nameof(selector));
- }
- return s_impl.Expand(source, selector);
- }
- #endregion
- #region ForkJoin
- /// <summary>
- /// Runs two observable sequences in parallel and combines their last elements.
- /// </summary>
- /// <typeparam name="TSource1">The type of the elements in the first source sequence.</typeparam>
- /// <typeparam name="TSource2">The type of the elements in the second source sequence.</typeparam>
- /// <typeparam name="TResult">The type of the elements in the result sequence, returned by the selector function.</typeparam>
- /// <param name="first">First observable sequence.</param>
- /// <param name="second">Second observable sequence.</param>
- /// <param name="resultSelector">Result selector function to invoke with the last elements of both sequences.</param>
- /// <returns>An observable sequence with the result of calling the selector function with the last elements of both input sequences.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> or <paramref name="resultSelector"/> is null.</exception>
- [Experimental]
- public static IObservable<TResult> ForkJoin<TSource1, TSource2, TResult>(this IObservable<TSource1> first, IObservable<TSource2> second, Func<TSource1, TSource2, TResult> resultSelector)
- {
- if (first == null)
- {
- throw new ArgumentNullException(nameof(first));
- }
- if (second == null)
- {
- throw new ArgumentNullException(nameof(second));
- }
- if (resultSelector == null)
- {
- throw new ArgumentNullException(nameof(resultSelector));
- }
- return s_impl.ForkJoin(first, second, resultSelector);
- }
- /// <summary>
- /// Runs all specified observable sequences in parallel and collects their last elements.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements in the source sequences.</typeparam>
- /// <param name="sources">Observable sequence to collect the last elements for.</param>
- /// <returns>An observable sequence with an array collecting the last elements of all the input sequences.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="sources"/> is null.</exception>
- [Experimental]
- public static IObservable<TSource[]> ForkJoin<TSource>(params IObservable<TSource>[] sources)
- {
- if (sources == null)
- {
- throw new ArgumentNullException(nameof(sources));
- }
- return s_impl.ForkJoin(sources);
- }
- /// <summary>
- /// Runs all observable sequences in the enumerable sources sequence in parallel and collect their last elements.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements in the source sequences.</typeparam>
- /// <param name="sources">Observable sequence to collect the last elements for.</param>
- /// <returns>An observable sequence with an array collecting the last elements of all the input sequences.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="sources"/> is null.</exception>
- [Experimental]
- public static IObservable<TSource[]> ForkJoin<TSource>(this IEnumerable<IObservable<TSource>> sources)
- {
- if (sources == null)
- {
- throw new ArgumentNullException(nameof(sources));
- }
- return s_impl.ForkJoin(sources);
- }
- #endregion
- #region Let
- /// <summary>
- /// Returns an observable sequence that is the result of invoking the selector on the source sequence, without sharing subscriptions.
- /// This operator allows for a fluent style of writing queries that use the same sequence multiple times.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
- /// <typeparam name="TResult">The type of the elements in the result sequence.</typeparam>
- /// <param name="source">Source sequence that will be shared in the selector function.</param>
- /// <param name="selector">Selector function which can use the source sequence as many times as needed, without sharing subscriptions to the source sequence.</param>
- /// <returns>An observable sequence that contains the elements of a sequence produced by multicasting the source sequence within a selector function.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
- [Experimental]
- public static IObservable<TResult> Let<TSource, TResult>(this IObservable<TSource> source, Func<IObservable<TSource>, IObservable<TResult>> selector)
- {
- if (source == null)
- {
- throw new ArgumentNullException(nameof(source));
- }
- if (selector == null)
- {
- throw new ArgumentNullException(nameof(selector));
- }
- return s_impl.Let(source, selector);
- }
- #endregion
- #region ManySelect
- /// <summary>
- /// Comonadic bind operator.
- /// </summary>
- [Experimental]
- public static IObservable<TResult> ManySelect<TSource, TResult>(this IObservable<TSource> source, Func<IObservable<TSource>, TResult> selector, IScheduler scheduler)
- {
- if (source == null)
- {
- throw new ArgumentNullException(nameof(source));
- }
- if (selector == null)
- {
- throw new ArgumentNullException(nameof(selector));
- }
- if (scheduler == null)
- {
- throw new ArgumentNullException(nameof(scheduler));
- }
- return s_impl.ManySelect(source, selector, scheduler);
- }
- /// <summary>
- /// Comonadic bind operator.
- /// </summary>
- [Experimental]
- public static IObservable<TResult> ManySelect<TSource, TResult>(this IObservable<TSource> source, Func<IObservable<TSource>, TResult> selector)
- {
- if (source == null)
- {
- throw new ArgumentNullException(nameof(source));
- }
- if (selector == null)
- {
- throw new ArgumentNullException(nameof(selector));
- }
- return s_impl.ManySelect(source, selector);
- }
- #endregion
- #region ToListObservable
- /// <summary>
- /// Immediately subscribes to source and retains the elements in the observable sequence.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
- /// <param name="source">Source sequence.</param>
- /// <returns>Object that's both an observable sequence and a list which can be used to access the source sequence's elements.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
- [Experimental]
- public static ListObservable<TSource> ToListObservable<TSource>(this IObservable<TSource> source)
- {
- if (source == null)
- {
- throw new ArgumentNullException(nameof(source));
- }
- return s_impl.ToListObservable(source);
- }
- #endregion
- }
- }
|