Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Windows.Threading/Reactive/Linq/CoreDispatcherObservable.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 Windows.UI.Core;
6 using Windows.UI.Xaml;
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 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, CoreDispatcher dispatcher)
26 {
27 if (source == null)
28 throw new ArgumentNullException("source");
29 if (dispatcher == null)
30 throw new ArgumentNullException("dispatcher");
31
32 return Synchronization.ObserveOn(source, new CoreDispatcherScheduler(dispatcher));
33 }
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, CoreDispatcher dispatcher, CoreDispatcherPriority priority)
45 {
46 if (source == null)
47 throw new ArgumentNullException("source");
48 if (dispatcher == null)
49 throw new ArgumentNullException("dispatcher");
50
51 return Synchronization.ObserveOn(source, new CoreDispatcherScheduler(dispatcher, priority));
52 }
53
54 /// <summary>
55 /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the specified object.
56 /// </summary>
57 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
58 /// <param name="source">Source sequence.</param>
59 /// <param name="dependencyObject">Object to get the dispatcher from.</param>
60 /// <returns>The source sequence whose observations happen on the specified object's dispatcher.</returns>
61 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dependencyObject"/> is null.</exception>
62 public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DependencyObject dependencyObject)
63 {
64 if (source == null)
65 throw new ArgumentNullException("source");
66 if (dependencyObject == null)
67 throw new ArgumentNullException("dependencyObject");
68
69 return Synchronization.ObserveOn(source, new CoreDispatcherScheduler(dependencyObject.Dispatcher));
70 }
71
72 /// <summary>
73 /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the specified object.
74 /// </summary>
75 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
76 /// <param name="source">Source sequence.</param>
77 /// <param name="dependencyObject">Object to get the dispatcher from.</param>
78 /// <param name="priority">Priority to schedule work items at.</param>
79 /// <returns>The source sequence whose observations happen on the specified object's dispatcher.</returns>
80 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dependencyObject"/> is null.</exception>
81 public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DependencyObject dependencyObject, CoreDispatcherPriority priority)
82 {
83 if (source == null)
84 throw new ArgumentNullException("source");
85 if (dependencyObject == null)
86 throw new ArgumentNullException("dependencyObject");
87
88 return Synchronization.ObserveOn(source, new CoreDispatcherScheduler(dependencyObject.Dispatcher, priority));
89 }
90
91 /// <summary>
92 /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the current window.
93 /// </summary>
94 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
95 /// <param name="source">Source sequence.</param>
96 /// <returns>The source sequence whose observations happen on the current window's dispatcher.</returns>
97 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
98 public static IObservable<TSource> ObserveOnDispatcher<TSource>(this IObservable<TSource> source)
99 {
100 if (source == null)
101 throw new ArgumentNullException("source");
102
103 return Synchronization.ObserveOn(source, CoreDispatcherScheduler.Current);
104 }
105
106 /// <summary>
107 /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the current window.
108 /// </summary>
109 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
110 /// <param name="source">Source sequence.</param>
111 /// <param name="priority">Priority to schedule work items at.</param>
112 /// <returns>The source sequence whose observations happen on the current window's dispatcher.</returns>
113 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
114 public static IObservable<TSource> ObserveOnDispatcher<TSource>(this IObservable<TSource> source, CoreDispatcherPriority priority)
115 {
116 if (source == null)
117 throw new ArgumentNullException("source");
118
119 return Synchronization.ObserveOn(source, new CoreDispatcherScheduler(CoreDispatcherScheduler.Current.Dispatcher, priority));
120 }
121
122 #endregion
123
124 #region SubscribeOn[Dispatcher]
125
126 /// <summary>
127 /// Wraps the source sequence in order to run its subscription and unsubscription logic on the specified dispatcher.
128 /// </summary>
129 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
130 /// <param name="source">Source sequence.</param>
131 /// <param name="dispatcher">Dispatcher whose associated message loop is used to perform subscription and unsubscription actions on.</param>
132 /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified dispatcher.</returns>
133 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcher"/> is null.</exception>
134 /// <remarks>
135 /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the specified dispatcher.
136 /// 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)"/>.
137 /// </remarks>
138 public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, CoreDispatcher dispatcher)
139 {
140 if (source == null)
141 throw new ArgumentNullException("source");
142 if (dispatcher == null)
143 throw new ArgumentNullException("dispatcher");
144
145 return Synchronization.SubscribeOn(source, new CoreDispatcherScheduler(dispatcher));
146 }
147
148 /// <summary>
149 /// Wraps the source sequence in order to run its subscription and unsubscription logic on the specified dispatcher.
150 /// </summary>
151 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
152 /// <param name="source">Source sequence.</param>
153 /// <param name="dispatcher">Dispatcher whose associated message loop is used to perform subscription and unsubscription actions on.</param>
154 /// <param name="priority">Priority to schedule work items at.</param>
155 /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified dispatcher.</returns>
156 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcher"/> is null.</exception>
157 /// <remarks>
158 /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the specified dispatcher.
159 /// 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)"/>.
160 /// </remarks>
161 public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, CoreDispatcher dispatcher, CoreDispatcherPriority priority)
162 {
163 if (source == null)
164 throw new ArgumentNullException("source");
165 if (dispatcher == null)
166 throw new ArgumentNullException("dispatcher");
167
168 return Synchronization.SubscribeOn(source, new CoreDispatcherScheduler(dispatcher, priority));
169 }
170
171 /// <summary>
172 /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the specified object.
173 /// </summary>
174 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
175 /// <param name="source">Source sequence.</param>
176 /// <param name="dependencyObject">Object to get the dispatcher from.</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)"/>.
182 /// </remarks>
183 public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, DependencyObject dependencyObject)
184 {
185 if (source == null)
186 throw new ArgumentNullException("source");
187 if (dependencyObject == null)
188 throw new ArgumentNullException("dependencyObject");
189
190 return Synchronization.SubscribeOn(source, new CoreDispatcherScheduler(dependencyObject.Dispatcher));
191 }
192
193 /// <summary>
194 /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the specified object.
195 /// </summary>
196 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
197 /// <param name="source">Source sequence.</param>
198 /// <param name="dependencyObject">Object to get the dispatcher from.</param>
199 /// <param name="priority">Priority to schedule work items at.</param>
200 /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified object's dispatcher.</returns>
201 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dependencyObject"/> is null.</exception>
202 /// <remarks>
203 /// 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.
204 /// 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)"/>.
205 /// </remarks>
206 public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, DependencyObject dependencyObject, CoreDispatcherPriority priority)
207 {
208 if (source == null)
209 throw new ArgumentNullException("source");
210 if (dependencyObject == null)
211 throw new ArgumentNullException("dependencyObject");
212
213 return Synchronization.SubscribeOn(source, new CoreDispatcherScheduler(dependencyObject.Dispatcher, priority));
214 }
215
216 /// <summary>
217 /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the current window.
218 /// </summary>
219 /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
220 /// <param name="source">Source sequence.</param>
221 /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the current window's dispatcher.</returns>
222 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
223 /// <remarks>
224 /// 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.
225 /// 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})"/>.
226 /// </remarks>
227 public static IObservable<TSource> SubscribeOnDispatcher<TSource>(this IObservable<TSource> source)
228 {
229 if (source == null)
230 throw new ArgumentNullException("source");
231
232 return Synchronization.SubscribeOn(source, CoreDispatcherScheduler.Current);
233 }
234
235 /// <summary>
236 /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the current window.
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="priority">Priority to schedule work items at.</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="DispatcherObservable.ObserveOnDispatcher{TSource}(IObservable{TSource}, CoreDispatcherPriority)"/>.
246 /// </remarks>
247 public static IObservable<TSource> SubscribeOnDispatcher<TSource>(this IObservable<TSource> source, CoreDispatcherPriority priority)
248 {
249 if (source == null)
250 throw new ArgumentNullException("source");
251
252 return Synchronization.SubscribeOn(source, new CoreDispatcherScheduler(CoreDispatcherScheduler.Current.Dispatcher, priority));
253 }
254
255 #endregion
256 }
257 }
258 #endif