CoreDispatcherObservable.cs 16 KB

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