Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Windows.Threading/Reactive/Linq/DispatcherObservable.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 #if !WINDOWS
4 using System.Reactive.Concurrency;
5 using System.Windows;
6 using System.Windows.Threading;
7
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
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 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, Dispatcher dispatcher)
26 {
27 if (source == null)
28 throw new ArgumentNullException("source");
29 if (dispatcher == null)
30 throw new ArgumentNullException("dispatcher");
31
32 return ObserveOn_<TSource>(source, dispatcher);
33 }
34
35 #if HAS_DISPATCHER_PRIORITY
36 /// <summary>
37 /// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher.
38 /// </summary>
39 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
40 /// <param name="source">Source sequence.</param>
41 /// <param name="dispatcher">Dispatcher whose associated message loop is used to to notify observers on.</param>
42 /// <param name="priority">Priority to schedule work items at.</param>
43 /// <returns>The source sequence whose observations happen on the specified dispatcher.</returns>
44 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcher"/> is null.</exception>
45 public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, Dispatcher dispatcher, DispatcherPriority priority)
46 {
47 if (source == null)
48 throw new ArgumentNullException("source");
49 if (dispatcher == null)
50 throw new ArgumentNullException("dispatcher");
51
52 return ObserveOn_<TSource>(source, dispatcher, priority);
53 }
54 #endif
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 throw new ArgumentNullException("source");
68 if (scheduler == null)
69 throw new ArgumentNullException("scheduler");
70
71 #if HAS_DISPATCHER_PRIORITY
72 return ObserveOn_<TSource>(source, scheduler.Dispatcher, scheduler.Priority);
73 #else
74 return ObserveOn_<TSource>(source, scheduler.Dispatcher);
75 #endif
76 }
77
78 #if USE_SL_DISPATCHER
79 /// <summary>
80 /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the specified object.
81 /// </summary>
82 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
83 /// <param name="source">Source sequence.</param>
84 /// <param name="dependencyObject">Object to get the dispatcher from.</param>
85 /// <returns>The source sequence whose observations happen on the specified object's dispatcher.</returns>
86 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dependencyObject"/> is null.</exception>
87 public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DependencyObject dependencyObject)
88 {
89 if (source == null)
90 throw new ArgumentNullException("source");
91 if (dependencyObject == null)
92 throw new ArgumentNullException("dependencyObject");
93
94 return ObserveOn_<TSource>(source, dependencyObject.Dispatcher);
95 }
96 #else
97 /// <summary>
98 /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the specified object.
99 /// </summary>
100 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
101 /// <param name="source">Source sequence.</param>
102 /// <param name="dispatcherObject">Object to get the dispatcher from.</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)
106 {
107 if (source == null)
108 throw new ArgumentNullException("source");
109 if (dispatcherObject == null)
110 throw new ArgumentNullException("dispatcherObject");
111
112 return ObserveOn_<TSource>(source, dispatcherObject.Dispatcher);
113 }
114 #endif
115
116 #if HAS_DISPATCHER_PRIORITY
117 /// <summary>
118 /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the specified object.
119 /// </summary>
120 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
121 /// <param name="source">Source sequence.</param>
122 /// <param name="dispatcherObject">Object to get the dispatcher from.</param>
123 /// <param name="priority">Priority to schedule work items at.</param>
124 /// <returns>The source sequence whose observations happen on the specified object's dispatcher.</returns>
125 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcherObject"/> is null.</exception>
126 public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DispatcherObject dispatcherObject, DispatcherPriority priority)
127 {
128 if (source == null)
129 throw new ArgumentNullException("source");
130 if (dispatcherObject == null)
131 throw new ArgumentNullException("dispatcherObject");
132
133 return ObserveOn_<TSource>(source, dispatcherObject.Dispatcher, priority);
134 }
135 #endif
136
137 /// <summary>
138 /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the current thread.
139 /// </summary>
140 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
141 /// <param name="source">Source sequence.</param>
142 /// <returns>The source sequence whose observations happen on the current thread's dispatcher.</returns>
143 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
144 public static IObservable<TSource> ObserveOnDispatcher<TSource>(this IObservable<TSource> source)
145 {
146 if (source == null)
147 throw new ArgumentNullException("source");
148
149 #if USE_SL_DISPATCHER
150 return ObserveOn_<TSource>(source, System.Windows.Deployment.Current.Dispatcher);
151 #else
152 return ObserveOn_<TSource>(source, DispatcherScheduler.Current.Dispatcher);
153 #endif
154 }
155
156 #if HAS_DISPATCHER_PRIORITY
157 /// <summary>
158 /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the current thread.
159 /// </summary>
160 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
161 /// <param name="source">Source sequence.</param>
162 /// <param name="priority">Priority to schedule work items at.</param>
163 /// <returns>The source sequence whose observations happen on the current thread's dispatcher.</returns>
164 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
165 public static IObservable<TSource> ObserveOnDispatcher<TSource>(this IObservable<TSource> source, DispatcherPriority priority)
166 {
167 if (source == null)
168 throw new ArgumentNullException("source");
169
170 return ObserveOn_<TSource>(source, DispatcherScheduler.Current.Dispatcher, priority);
171 }
172
173 private static IObservable<TSource> ObserveOn_<TSource>(IObservable<TSource> source, Dispatcher dispatcher, DispatcherPriority priority)
174 {
175 return Synchronization.ObserveOn(source, new DispatcherSynchronizationContext(dispatcher, priority));
176 }
177 #endif
178
179 private static IObservable<TSource> ObserveOn_<TSource>(IObservable<TSource> source, Dispatcher dispatcher)
180 {
181 return Synchronization.ObserveOn(source, new DispatcherSynchronizationContext(dispatcher));
182 }
183
184 #endregion
185
186 #region SubscribeOn[Dispatcher]
187
188 /// <summary>
189 /// Wraps the source sequence in order to run its subscription and unsubscription logic on the specified dispatcher.
190 /// </summary>
191 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
192 /// <param name="source">Source sequence.</param>
193 /// <param name="dispatcher">Dispatcher whose associated message loop is used to to perform subscription and unsubscription actions on.</param>
194 /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified dispatcher.</returns>
195 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcher"/> is null.</exception>
196 /// <remarks>
197 /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the specified dispatcher.
198 /// 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)"/>.
199 /// </remarks>
200 public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, Dispatcher dispatcher)
201 {
202 if (source == null)
203 throw new ArgumentNullException("source");
204 if (dispatcher == null)
205 throw new ArgumentNullException("dispatcher");
206
207 return SubscribeOn_<TSource>(source, dispatcher);
208 }
209
210 #if HAS_DISPATCHER_PRIORITY
211 /// <summary>
212 /// Wraps the source sequence in order to run its subscription and unsubscription logic on the specified dispatcher.
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="dispatcher">Dispatcher whose associated message loop is used to to perform subscription and unsubscription actions on.</param>
217 /// <param name="priority">Priority to schedule work items at.</param>
218 /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified dispatcher.</returns>
219 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcher"/> is null.</exception>
220 /// <remarks>
221 /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the specified dispatcher.
222 /// 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)"/>.
223 /// </remarks>
224 public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, Dispatcher dispatcher, DispatcherPriority priority)
225 {
226 if (source == null)
227 throw new ArgumentNullException("source");
228 if (dispatcher == null)
229 throw new ArgumentNullException("dispatcher");
230
231 return SubscribeOn_<TSource>(source, dispatcher, priority);
232 }
233 #endif
234
235 /// <summary>
236 /// Wraps the source sequence in order to run its subscription and unsubscription logic on the specified dispatcher scheduler.
237 /// </summary>
238 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
239 /// <param name="source">Source sequence.</param>
240 /// <param name="scheduler">Dispatcher scheduler to perform subscription and unsubscription actions on.</param>
241 /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified dispatcher scheduler.</returns>
242 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="scheduler"/> 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 specified scheduler.
245 /// 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)"/>.
246 /// </remarks>
247 public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, DispatcherScheduler scheduler)
248 {
249 if (source == null)
250 throw new ArgumentNullException("source");
251 if (scheduler == null)
252 throw new ArgumentNullException("scheduler");
253
254 #if HAS_DISPATCHER_PRIORITY
255 return SubscribeOn_<TSource>(source, scheduler.Dispatcher, scheduler.Priority);
256 #else
257 return SubscribeOn_<TSource>(source, scheduler.Dispatcher);
258 #endif
259 }
260
261 #if USE_SL_DISPATCHER
262 /// <summary>
263 /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the specified object.
264 /// </summary>
265 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
266 /// <param name="source">Source sequence.</param>
267 /// <param name="dependencyObject">Object to get the dispatcher from.</param>
268 /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified object's dispatcher.</returns>
269 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dependencyObject"/> is null.</exception>
270 /// <remarks>
271 /// 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.
272 /// 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)"/>.
273 /// </remarks>
274 public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, DependencyObject dependencyObject)
275 {
276 if (source == null)
277 throw new ArgumentNullException("source");
278 if (dependencyObject == null)
279 throw new ArgumentNullException("dependencyObject");
280
281 return SubscribeOn_<TSource>(source, dependencyObject.Dispatcher);
282 }
283 #else
284 /// <summary>
285 /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the specified object.
286 /// </summary>
287 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
288 /// <param name="source">Source sequence.</param>
289 /// <param name="dispatcherObject">Object to get the dispatcher from.</param>
290 /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified object's dispatcher.</returns>
291 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcherObject"/> is null.</exception>
292 /// <remarks>
293 /// 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.
294 /// 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)"/>.
295 /// </remarks>
296 public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, DispatcherObject dispatcherObject)
297 {
298 if (source == null)
299 throw new ArgumentNullException("source");
300 if (dispatcherObject == null)
301 throw new ArgumentNullException("dispatcherObject");
302
303 return SubscribeOn_<TSource>(source, dispatcherObject.Dispatcher);
304 }
305 #endif
306
307 #if HAS_DISPATCHER_PRIORITY
308 /// <summary>
309 /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the specified object.
310 /// </summary>
311 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
312 /// <param name="source">Source sequence.</param>
313 /// <param name="dispatcherObject">Object to get the dispatcher from.</param>
314 /// <param name="priority">Priority to schedule work items at.</param>
315 /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified object's dispatcher.</returns>
316 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcherObject"/> is null.</exception>
317 /// <remarks>
318 /// 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.
319 /// 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)"/>.
320 /// </remarks>
321 public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, DispatcherObject dispatcherObject, DispatcherPriority priority)
322 {
323 if (source == null)
324 throw new ArgumentNullException("source");
325 if (dispatcherObject == null)
326 throw new ArgumentNullException("dispatcherObject");
327
328 return SubscribeOn_<TSource>(source, dispatcherObject.Dispatcher, priority);
329 }
330 #endif
331
332 /// <summary>
333 /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the current thread.
334 /// </summary>
335 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
336 /// <param name="source">Source sequence.</param>
337 /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the current thread's dispatcher.</returns>
338 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
339 /// <remarks>
340 /// 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.
341 /// 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})"/>.
342 /// </remarks>
343 public static IObservable<TSource> SubscribeOnDispatcher<TSource>(this IObservable<TSource> source)
344 {
345 if (source == null)
346 throw new ArgumentNullException("source");
347
348 #if USE_SL_DISPATCHER
349 return SubscribeOn_<TSource>(source, System.Windows.Deployment.Current.Dispatcher);
350 #else
351 return SubscribeOn_<TSource>(source, DispatcherScheduler.Current.Dispatcher);
352 #endif
353 }
354
355 #if HAS_DISPATCHER_PRIORITY
356 /// <summary>
357 /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the current thread.
358 /// </summary>
359 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
360 /// <param name="source">Source sequence.</param>
361 /// <param name="priority">Priority to schedule work items at.</param>
362 /// <returns>The source sequence whose observations happen on the current thread's dispatcher.</returns>
363 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
364 /// <remarks>
365 /// 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.
366 /// 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)"/>.
367 /// </remarks>
368 public static IObservable<TSource> SubscribeOnDispatcher<TSource>(this IObservable<TSource> source, DispatcherPriority priority)
369 {
370 if (source == null)
371 throw new ArgumentNullException("source");
372
373 return SubscribeOn_<TSource>(source, DispatcherScheduler.Current.Dispatcher, priority);
374 }
375
376 private static IObservable<TSource> SubscribeOn_<TSource>(IObservable<TSource> source, Dispatcher dispatcher, DispatcherPriority priority)
377 {
378 return Synchronization.SubscribeOn(source, new DispatcherSynchronizationContext(dispatcher, priority));
379 }
380 #endif
381
382 private static IObservable<TSource> SubscribeOn_<TSource>(IObservable<TSource> source, Dispatcher dispatcher)
383 {
384 return Synchronization.SubscribeOn(source, new DispatcherSynchronizationContext(dispatcher));
385 }
386
387 #endregion
388 }
389 }
390 #endif