1
0

Observable.Joins.cs 4.5 KB

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