PageRenderTime 1078ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/Rx 1.0.10605/Source/Rxx/System/Reactive/PairedObservableExtensions.cs

#
C# | 147 lines | 88 code | 16 blank | 43 comment | 24 complexity | 6d2152bca4f2a6972f76b96eec8ad4e0 MD5 | raw file
  1. using System.Diagnostics.Contracts;
  2. using System.Reactive.Linq;
  3. namespace System.Reactive
  4. {
  5. /// <summary>
  6. /// Provides extension methods for <see cref="System.Reactive.IPairedObservable{TLeft,TRight}"/>.
  7. /// </summary>
  8. public static class PairedObservableExtensions
  9. {
  10. /// <summary>
  11. /// Notifies the observable that an observer is to receive notifications.
  12. /// </summary>
  13. /// <typeparam name="TLeft">Type of the left notification channel.</typeparam>
  14. /// <typeparam name="TRight">Type of the right notification channel.</typeparam>
  15. /// <param name="source">The observable for which a subscription is created.</param>
  16. /// <param name="onNextLeft">The handler of notifications in the left channel.</param>
  17. /// <param name="onNextRight">The handler of notifications in the right channel.</param>
  18. /// <returns>The observer's interface that enables cancelation of the subscription so that it stops receiving notifications.</returns>
  19. public static IDisposable SubscribePair<TLeft, TRight>(
  20. this IObservable<Either<TLeft, TRight>> source,
  21. Action<TLeft> onNextLeft,
  22. Action<TRight> onNextRight)
  23. {
  24. Contract.Requires(source != null);
  25. Contract.Requires(onNextLeft != null);
  26. Contract.Requires(onNextRight != null);
  27. Contract.Ensures(Contract.Result<IDisposable>() != null);
  28. var disposable = source.Subscribe(PairedObserver.Create(
  29. onNextLeft,
  30. onNextRight));
  31. #if SILVERLIGHT
  32. Contract.Assume(disposable != null);
  33. #endif
  34. return disposable;
  35. }
  36. /// <summary>
  37. /// Notifies the observable that an observer is to receive notifications.
  38. /// </summary>
  39. /// <typeparam name="TLeft">Type of the left notification channel.</typeparam>
  40. /// <typeparam name="TRight">Type of the right notification channel.</typeparam>
  41. /// <param name="source">The observable for which a subscription is created.</param>
  42. /// <param name="onNextLeft">The handler of notifications in the left channel.</param>
  43. /// <param name="onNextRight">The handler of notifications in the right channel.</param>
  44. /// <param name="onError">The handler of an error notification.</param>
  45. /// <returns>The observer's interface that enables cancelation of the subscription so that it stops receiving notifications.</returns>
  46. public static IDisposable SubscribePair<TLeft, TRight>(
  47. this IObservable<Either<TLeft, TRight>> source,
  48. Action<TLeft> onNextLeft,
  49. Action<TRight> onNextRight,
  50. Action<Exception> onError)
  51. {
  52. Contract.Requires(source != null);
  53. Contract.Requires(onNextLeft != null);
  54. Contract.Requires(onNextRight != null);
  55. Contract.Requires(onError != null);
  56. Contract.Ensures(Contract.Result<IDisposable>() != null);
  57. var disposable = source.Subscribe(PairedObserver.Create(
  58. onNextLeft,
  59. onNextRight,
  60. onError));
  61. #if SILVERLIGHT
  62. Contract.Assume(disposable != null);
  63. #endif
  64. return disposable;
  65. }
  66. /// <summary>
  67. /// Notifies the observable that an observer is to receive notifications.
  68. /// </summary>
  69. /// <typeparam name="TLeft">Type of the left notification channel.</typeparam>
  70. /// <typeparam name="TRight">Type of the right notification channel.</typeparam>
  71. /// <param name="source">The observable for which a subscription is created.</param>
  72. /// <param name="onNextLeft">The handler of notifications in the left channel.</param>
  73. /// <param name="onNextRight">The handler of notifications in the right channel.</param>
  74. /// <param name="onCompleted">The handler of a completion notification.</param>
  75. /// <returns>The observer's interface that enables cancelation of the subscription so that it stops receiving notifications.</returns>
  76. public static IDisposable SubscribePair<TLeft, TRight>(
  77. this IObservable<Either<TLeft, TRight>> source,
  78. Action<TLeft> onNextLeft,
  79. Action<TRight> onNextRight,
  80. Action onCompleted)
  81. {
  82. Contract.Requires(source != null);
  83. Contract.Requires(onNextLeft != null);
  84. Contract.Requires(onNextRight != null);
  85. Contract.Requires(onCompleted != null);
  86. Contract.Ensures(Contract.Result<IDisposable>() != null);
  87. var disposable = source.Subscribe(PairedObserver.Create(
  88. onNextLeft,
  89. onNextRight,
  90. onCompleted));
  91. #if SILVERLIGHT
  92. Contract.Assume(disposable != null);
  93. #endif
  94. return disposable;
  95. }
  96. /// <summary>
  97. /// Notifies the observable that an observer is to receive notifications.
  98. /// </summary>
  99. /// <typeparam name="TLeft">Type of the left notification channel.</typeparam>
  100. /// <typeparam name="TRight">Type of the right notification channel.</typeparam>
  101. /// <param name="source">The observable for which a subscription is created.</param>
  102. /// <param name="onNextLeft">The handler of notifications in the left channel.</param>
  103. /// <param name="onNextRight">The handler of notifications in the right channel.</param>
  104. /// <param name="onError">The handler of an error notification.</param>
  105. /// <param name="onCompleted">The handler of a completion notification.</param>
  106. /// <returns>The observer's interface that enables cancelation of the subscription so that it stops receiving notifications.</returns>
  107. public static IDisposable SubscribePair<TLeft, TRight>(
  108. this IObservable<Either<TLeft, TRight>> source,
  109. Action<TLeft> onNextLeft,
  110. Action<TRight> onNextRight,
  111. Action<Exception> onError,
  112. Action onCompleted)
  113. {
  114. Contract.Requires(source != null);
  115. Contract.Requires(onNextLeft != null);
  116. Contract.Requires(onNextRight != null);
  117. Contract.Requires(onError != null);
  118. Contract.Requires(onCompleted != null);
  119. Contract.Ensures(Contract.Result<IDisposable>() != null);
  120. var disposable = source.Subscribe(PairedObserver.Create(
  121. onNextLeft,
  122. onNextRight,
  123. onError,
  124. onCompleted));
  125. #if SILVERLIGHT
  126. Contract.Assume(disposable != null);
  127. #endif
  128. return disposable;
  129. }
  130. }
  131. }