CoreDispatcherObservable.cs 17 KB

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