Observable.Joins.cs 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.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. throw new ArgumentNullException(nameof(left));
  24. if (right == null)
  25. throw new ArgumentNullException(nameof(right));
  26. return s_impl.And(left, right);
  27. }
  28. #endregion
  29. #region Then
  30. /// <summary>
  31. /// Matches when the observable sequence has an available element and projects the element by invoking the selector function.
  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 in the result sequence, returned by the selector function.</typeparam>
  35. /// <param name="source">Observable sequence to apply the selector on.</param>
  36. /// <param name="selector">Selector that will be invoked for elements in the source sequence.</param>
  37. /// <returns>Plan that produces the projected results, to be fed (with other plans) to the When operator.</returns>
  38. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  39. public static Plan<TResult> Then<TSource, TResult>(this IObservable<TSource> source, Func<TSource, TResult> selector)
  40. {
  41. if (source == null)
  42. throw new ArgumentNullException(nameof(source));
  43. if (selector == null)
  44. throw new ArgumentNullException(nameof(selector));
  45. return s_impl.Then(source, selector);
  46. }
  47. #endregion
  48. #region When
  49. /// <summary>
  50. /// Joins together the results from several patterns.
  51. /// </summary>
  52. /// <typeparam name="TResult">The type of the elements in the result sequence, obtained from the specified patterns.</typeparam>
  53. /// <param name="plans">A series of plans created by use of the Then operator on patterns.</param>
  54. /// <returns>An observable sequence with the results from matching several patterns.</returns>
  55. /// <exception cref="ArgumentNullException"><paramref name="plans"/> is null.</exception>
  56. public static IObservable<TResult> When<TResult>(params Plan<TResult>[] plans)
  57. {
  58. if (plans == null)
  59. throw new ArgumentNullException(nameof(plans));
  60. return s_impl.When(plans);
  61. }
  62. /// <summary>
  63. /// Joins together the results from several patterns.
  64. /// </summary>
  65. /// <typeparam name="TResult">The type of the elements in the result sequence, obtained from the specified patterns.</typeparam>
  66. /// <param name="plans">A series of plans created by use of the Then operator on patterns.</param>
  67. /// <returns>An observable sequence with the results form matching several patterns.</returns>
  68. /// <exception cref="ArgumentNullException"><paramref name="plans"/> is null.</exception>
  69. public static IObservable<TResult> When<TResult>(this IEnumerable<Plan<TResult>> plans)
  70. {
  71. if (plans == null)
  72. throw new ArgumentNullException(nameof(plans));
  73. return s_impl.When(plans);
  74. }
  75. #endregion
  76. }
  77. }