Observable.Joins.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. using System.Collections.Generic;
  5. using System.Reactive.Disposables;
  6. using System.Reactive.Joins;
  7. namespace System.Reactive.Linq
  8. {
  9. public static partial class Observable
  10. {
  11. #region And
  12. /// <summary>
  13. /// Creates a pattern that matches when both observable sequences have an available element.
  14. /// </summary>
  15. /// <typeparam name="TLeft">The type of the elements in the left sequence.</typeparam>
  16. /// <typeparam name="TRight">The type of the elements in the right sequence.</typeparam>
  17. /// <param name="left">Observable sequence to match with the right sequence.</param>
  18. /// <param name="right">Observable sequence to match with the left sequence.</param>
  19. /// <returns>Pattern object that matches when both observable sequences have an available element.</returns>
  20. /// <exception cref="ArgumentNullException"><paramref name="left"/> or <paramref name="right"/> is null.</exception>
  21. public static Pattern<TLeft, TRight> And<TLeft, TRight>(this IObservable<TLeft> left, IObservable<TRight> right)
  22. {
  23. if (left == null)
  24. throw new ArgumentNullException(nameof(left));
  25. if (right == null)
  26. throw new ArgumentNullException(nameof(right));
  27. return s_impl.And<TLeft, TRight>(left, right);
  28. }
  29. #endregion
  30. #region Then
  31. /// <summary>
  32. /// Matches when the observable sequence has an available element and projects the element by invoking the selector function.
  33. /// </summary>
  34. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  35. /// <typeparam name="TResult">The type of the elements in the result sequence, returned by the selector function.</typeparam>
  36. /// <param name="source">Observable sequence to apply the selector on.</param>
  37. /// <param name="selector">Selector that will be invoked for elements in the source sequence.</param>
  38. /// <returns>Plan that produces the projected results, to be fed (with other plans) to the When operator.</returns>
  39. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  40. public static Plan<TResult> Then<TSource, TResult>(this IObservable<TSource> source, Func<TSource, TResult> selector)
  41. {
  42. if (source == null)
  43. throw new ArgumentNullException(nameof(source));
  44. if (selector == null)
  45. throw new ArgumentNullException(nameof(selector));
  46. return s_impl.Then<TSource, TResult>(source, selector);
  47. }
  48. #endregion
  49. #region When
  50. /// <summary>
  51. /// Joins together the results from several patterns.
  52. /// </summary>
  53. /// <typeparam name="TResult">The type of the elements in the result sequence, obtained from the specified patterns.</typeparam>
  54. /// <param name="plans">A series of plans created by use of the Then operator on patterns.</param>
  55. /// <returns>An observable sequence with the results from matching several patterns.</returns>
  56. /// <exception cref="ArgumentNullException"><paramref name="plans"/> is null.</exception>
  57. public static IObservable<TResult> When<TResult>(params Plan<TResult>[] plans)
  58. {
  59. if (plans == null)
  60. throw new ArgumentNullException(nameof(plans));
  61. return s_impl.When<TResult>(plans);
  62. }
  63. /// <summary>
  64. /// Joins together the results from several patterns.
  65. /// </summary>
  66. /// <typeparam name="TResult">The type of the elements in the result sequence, obtained from the specified patterns.</typeparam>
  67. /// <param name="plans">A series of plans created by use of the Then operator on patterns.</param>
  68. /// <returns>An observable sequence with the results form matching several patterns.</returns>
  69. /// <exception cref="ArgumentNullException"><paramref name="plans"/> is null.</exception>
  70. public static IObservable<TResult> When<TResult>(this IEnumerable<Plan<TResult>> plans)
  71. {
  72. if (plans == null)
  73. throw new ArgumentNullException(nameof(plans));
  74. return s_impl.When<TResult>(plans);
  75. }
  76. #endregion
  77. }
  78. }