CoreDispatcherObservable.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #if WINDOWS
  5. using System.Reactive.Concurrency;
  6. using Windows.UI.Core;
  7. using Windows.UI.Xaml;
  8. namespace System.Reactive.Linq
  9. {
  10. /// <summary>
  11. /// Provides a set of extension methods for scheduling actions performed through observable sequences on UI dispatchers.
  12. /// </summary>
  13. [CLSCompliant(false)]
  14. public static class DispatcherObservable
  15. {
  16. #region ObserveOn[Dispatcher]
  17. /// <summary>
  18. /// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher.
  19. /// </summary>
  20. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  21. /// <param name="source">Source sequence.</param>
  22. /// <param name="dispatcher">Dispatcher whose associated message loop is used to notify observers on.</param>
  23. /// <returns>The source sequence whose observations happen on the specified dispatcher.</returns>
  24. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcher"/> is null.</exception>
  25. public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, CoreDispatcher dispatcher)
  26. {
  27. if (source == null)
  28. throw new ArgumentNullException("source");
  29. if (dispatcher == null)
  30. throw new ArgumentNullException("dispatcher");
  31. return Synchronization.ObserveOn(source, new CoreDispatcherScheduler(dispatcher));
  32. }
  33. /// <summary>
  34. /// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher.
  35. /// </summary>
  36. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  37. /// <param name="source">Source sequence.</param>
  38. /// <param name="dispatcher">Dispatcher whose associated message loop is used to notify observers on.</param>
  39. /// <param name="priority">Priority to schedule work items at.</param>
  40. /// <returns>The source sequence whose observations happen on the specified dispatcher.</returns>
  41. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcher"/> is null.</exception>
  42. public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, CoreDispatcher dispatcher, CoreDispatcherPriority priority)
  43. {
  44. if (source == null)
  45. throw new ArgumentNullException("source");
  46. if (dispatcher == null)
  47. throw new ArgumentNullException("dispatcher");
  48. return Synchronization.ObserveOn(source, new CoreDispatcherScheduler(dispatcher, priority));
  49. }
  50. /// <summary>
  51. /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the specified object.
  52. /// </summary>
  53. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  54. /// <param name="source">Source sequence.</param>
  55. /// <param name="dependencyObject">Object to get the dispatcher from.</param>
  56. /// <returns>The source sequence whose observations happen on the specified object's dispatcher.</returns>
  57. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dependencyObject"/> is null.</exception>
  58. public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DependencyObject dependencyObject)
  59. {
  60. if (source == null)
  61. throw new ArgumentNullException("source");
  62. if (dependencyObject == null)
  63. throw new ArgumentNullException("dependencyObject");
  64. return Synchronization.ObserveOn(source, new CoreDispatcherScheduler(dependencyObject.Dispatcher));
  65. }
  66. /// <summary>
  67. /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the specified object.
  68. /// </summary>
  69. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  70. /// <param name="source">Source sequence.</param>
  71. /// <param name="dependencyObject">Object to get the dispatcher from.</param>
  72. /// <param name="priority">Priority to schedule work items at.</param>
  73. /// <returns>The source sequence whose observations happen on the specified object's dispatcher.</returns>
  74. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dependencyObject"/> is null.</exception>
  75. public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DependencyObject dependencyObject, CoreDispatcherPriority priority)
  76. {
  77. if (source == null)
  78. throw new ArgumentNullException("source");
  79. if (dependencyObject == null)
  80. throw new ArgumentNullException("dependencyObject");
  81. return Synchronization.ObserveOn(source, new CoreDispatcherScheduler(dependencyObject.Dispatcher, priority));
  82. }
  83. /// <summary>
  84. /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the current window.
  85. /// </summary>
  86. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  87. /// <param name="source">Source sequence.</param>
  88. /// <returns>The source sequence whose observations happen on the current window's dispatcher.</returns>
  89. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  90. public static IObservable<TSource> ObserveOnDispatcher<TSource>(this IObservable<TSource> source)
  91. {
  92. if (source == null)
  93. throw new ArgumentNullException("source");
  94. return Synchronization.ObserveOn(source, CoreDispatcherScheduler.Current);
  95. }
  96. /// <summary>
  97. /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the current window.
  98. /// </summary>
  99. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  100. /// <param name="source">Source sequence.</param>
  101. /// <param name="priority">Priority to schedule work items at.</param>
  102. /// <returns>The source sequence whose observations happen on the current window's dispatcher.</returns>
  103. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  104. public static IObservable<TSource> ObserveOnDispatcher<TSource>(this IObservable<TSource> source, CoreDispatcherPriority priority)
  105. {
  106. if (source == null)
  107. throw new ArgumentNullException("source");
  108. return Synchronization.ObserveOn(source, new CoreDispatcherScheduler(CoreDispatcherScheduler.Current.Dispatcher, priority));
  109. }
  110. #endregion
  111. #region SubscribeOn[Dispatcher]
  112. /// <summary>
  113. /// Wraps the source sequence in order to run its subscription and unsubscription logic on the specified dispatcher.
  114. /// </summary>
  115. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  116. /// <param name="source">Source sequence.</param>
  117. /// <param name="dispatcher">Dispatcher whose associated message loop is used to perform subscription and unsubscription actions on.</param>
  118. /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified dispatcher.</returns>
  119. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcher"/> is null.</exception>
  120. /// <remarks>
  121. /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the specified dispatcher.
  122. /// In order to invoke observer callbacks on the specified dispatcher, e.g. to render results in a control, use <see cref="DispatcherObservable.ObserveOn{TSource}(IObservable{TSource}, CoreDispatcher)"/>.
  123. /// </remarks>
  124. public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, CoreDispatcher dispatcher)
  125. {
  126. if (source == null)
  127. throw new ArgumentNullException("source");
  128. if (dispatcher == null)
  129. throw new ArgumentNullException("dispatcher");
  130. return Synchronization.SubscribeOn(source, new CoreDispatcherScheduler(dispatcher));
  131. }
  132. /// <summary>
  133. /// Wraps the source sequence in order to run its subscription and unsubscription logic on the specified dispatcher.
  134. /// </summary>
  135. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  136. /// <param name="source">Source sequence.</param>
  137. /// <param name="dispatcher">Dispatcher whose associated message loop is used to perform subscription and unsubscription actions on.</param>
  138. /// <param name="priority">Priority to schedule work items at.</param>
  139. /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified dispatcher.</returns>
  140. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcher"/> is null.</exception>
  141. /// <remarks>
  142. /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the specified dispatcher.
  143. /// In order to invoke observer callbacks on the specified dispatcher, e.g. to render results in a control, use <see cref="DispatcherObservable.ObserveOn{TSource}(IObservable{TSource}, CoreDispatcher, CoreDispatcherPriority)"/>.
  144. /// </remarks>
  145. public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, CoreDispatcher dispatcher, CoreDispatcherPriority priority)
  146. {
  147. if (source == null)
  148. throw new ArgumentNullException("source");
  149. if (dispatcher == null)
  150. throw new ArgumentNullException("dispatcher");
  151. return Synchronization.SubscribeOn(source, new CoreDispatcherScheduler(dispatcher, priority));
  152. }
  153. /// <summary>
  154. /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the specified object.
  155. /// </summary>
  156. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  157. /// <param name="source">Source sequence.</param>
  158. /// <param name="dependencyObject">Object to get the dispatcher from.</param>
  159. /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified object's dispatcher.</returns>
  160. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dependencyObject"/> is null.</exception>
  161. /// <remarks>
  162. /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the dispatcher associated with the specified object.
  163. /// In order to invoke observer callbacks on the dispatcher associated with the specified object, e.g. to render results in a control, use <see cref="DispatcherObservable.ObserveOn{TSource}(IObservable{TSource}, DependencyObject)"/>.
  164. /// </remarks>
  165. public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, DependencyObject dependencyObject)
  166. {
  167. if (source == null)
  168. throw new ArgumentNullException("source");
  169. if (dependencyObject == null)
  170. throw new ArgumentNullException("dependencyObject");
  171. return Synchronization.SubscribeOn(source, new CoreDispatcherScheduler(dependencyObject.Dispatcher));
  172. }
  173. /// <summary>
  174. /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the specified object.
  175. /// </summary>
  176. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  177. /// <param name="source">Source sequence.</param>
  178. /// <param name="dependencyObject">Object to get the dispatcher from.</param>
  179. /// <param name="priority">Priority to schedule work items at.</param>
  180. /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified object's dispatcher.</returns>
  181. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dependencyObject"/> is null.</exception>
  182. /// <remarks>
  183. /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the dispatcher associated with the specified object.
  184. /// In order to invoke observer callbacks on the dispatcher associated with the specified object, e.g. to render results in a control, use <see cref="DispatcherObservable.ObserveOn{TSource}(IObservable{TSource}, DependencyObject, CoreDispatcherPriority)"/>.
  185. /// </remarks>
  186. public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, DependencyObject dependencyObject, CoreDispatcherPriority priority)
  187. {
  188. if (source == null)
  189. throw new ArgumentNullException("source");
  190. if (dependencyObject == null)
  191. throw new ArgumentNullException("dependencyObject");
  192. return Synchronization.SubscribeOn(source, new CoreDispatcherScheduler(dependencyObject.Dispatcher, priority));
  193. }
  194. /// <summary>
  195. /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the current window.
  196. /// </summary>
  197. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  198. /// <param name="source">Source sequence.</param>
  199. /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the current window's dispatcher.</returns>
  200. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  201. /// <remarks>
  202. /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the dispatcher associated with the current window.
  203. /// In order to invoke observer callbacks on the dispatcher associated with the current window, e.g. to render results in a control, use <see cref="DispatcherObservable.ObserveOnDispatcher{TSource}(IObservable{TSource})"/>.
  204. /// </remarks>
  205. public static IObservable<TSource> SubscribeOnDispatcher<TSource>(this IObservable<TSource> source)
  206. {
  207. if (source == null)
  208. throw new ArgumentNullException("source");
  209. return Synchronization.SubscribeOn(source, CoreDispatcherScheduler.Current);
  210. }
  211. /// <summary>
  212. /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the current window.
  213. /// </summary>
  214. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  215. /// <param name="source">Source sequence.</param>
  216. /// <param name="priority">Priority to schedule work items at.</param>
  217. /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the current window's dispatcher.</returns>
  218. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  219. /// <remarks>
  220. /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the dispatcher associated with the current window.
  221. /// In order to invoke observer callbacks on the dispatcher associated with the current window, e.g. to render results in a control, use <see cref="DispatcherObservable.ObserveOnDispatcher{TSource}(IObservable{TSource}, CoreDispatcherPriority)"/>.
  222. /// </remarks>
  223. public static IObservable<TSource> SubscribeOnDispatcher<TSource>(this IObservable<TSource> source, CoreDispatcherPriority priority)
  224. {
  225. if (source == null)
  226. throw new ArgumentNullException("source");
  227. return Synchronization.SubscribeOn(source, new CoreDispatcherScheduler(CoreDispatcherScheduler.Current.Dispatcher, priority));
  228. }
  229. #endregion
  230. }
  231. }
  232. #endif