DispatcherObservable.cs 20 KB

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