DispatcherObservable.cs 20 KB

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