Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Core/Observer.Extensions.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 using System.Reactive.Concurrency;
4 using System.Threading;
5
6 namespace System.Reactive
7 {
8 /// <summary>
9 /// Provides a set of static methods for creating observers.
10 /// </summary>
11 public static class Observer
12 {
13 /// <summary>
14 /// Creates an observer from a notification callback.
15 /// </summary>
16 /// <typeparam name="T">The type of the elements received by the observer.</typeparam>
17 /// <param name="handler">Action that handles a notification.</param>
18 /// <returns>The observer object that invokes the specified handler using a notification corresponding to each message it receives.</returns>
19 /// <exception cref="ArgumentNullException"><paramref name="handler"/> is null.</exception>
20 public static IObserver<T> ToObserver<T>(this Action<Notification<T>> handler)
21 {
22 if (handler == null)
23 throw new ArgumentNullException("handler");
24
25 return new AnonymousObserver<T>(
26 x => handler(Notification.CreateOnNext<T>(x)),
27 exception => handler(Notification.CreateOnError<T>(exception)),
28 () => handler(Notification.CreateOnCompleted<T>())
29 );
30 }
31
32 /// <summary>
33 /// Creates a notification callback from an observer.
34 /// </summary>
35 /// <typeparam name="T">The type of the elements received by the observer.</typeparam>
36 /// <param name="observer">Observer object.</param>
37 /// <returns>The action that forwards its input notification to the underlying observer.</returns>
38 /// <exception cref="ArgumentNullException"><paramref name="observer"/> is null.</exception>
39 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Notifier", Justification = "Backward compat.")]
40 public static Action<Notification<T>> ToNotifier<T>(this IObserver<T> observer)
41 {
42 if (observer == null)
43 throw new ArgumentNullException("observer");
44
45 return n => n.Accept(observer);
46 }
47
48 /// <summary>
49 /// Creates an observer from the specified OnNext action.
50 /// </summary>
51 /// <typeparam name="T">The type of the elements received by the observer.</typeparam>
52 /// <param name="onNext">Observer's OnNext action implementation.</param>
53 /// <returns>The observer object implemented using the given actions.</returns>
54 /// <exception cref="ArgumentNullException"><paramref name="onNext"/> is null.</exception>
55 public static IObserver<T> Create<T>(Action<T> onNext)
56 {
57 if (onNext == null)
58 throw new ArgumentNullException("onNext");
59
60 return new AnonymousObserver<T>(onNext);
61 }
62
63 /// <summary>
64 /// Creates an observer from the specified OnNext and OnError actions.
65 /// </summary>
66 /// <typeparam name="T">The type of the elements received by the observer.</typeparam>
67 /// <param name="onNext">Observer's OnNext action implementation.</param>
68 /// <param name="onError">Observer's OnError action implementation.</param>
69 /// <returns>The observer object implemented using the given actions.</returns>
70 /// <exception cref="ArgumentNullException"><paramref name="onNext"/> or <paramref name="onError"/> is null.</exception>
71 public static IObserver<T> Create<T>(Action<T> onNext, Action<Exception> onError)
72 {
73 if (onNext == null)
74 throw new ArgumentNullException("onNext");
75 if (onError == null)
76 throw new ArgumentNullException("onError");
77
78 return new AnonymousObserver<T>(onNext, onError);
79 }
80
81 /// <summary>
82 /// Creates an observer from the specified OnNext and OnCompleted actions.
83 /// </summary>
84 /// <typeparam name="T">The type of the elements received by the observer.</typeparam>
85 /// <param name="onNext">Observer's OnNext action implementation.</param>
86 /// <param name="onCompleted">Observer's OnCompleted action implementation.</param>
87 /// <returns>The observer object implemented using the given actions.</returns>
88 /// <exception cref="ArgumentNullException"><paramref name="onNext"/> or <paramref name="onCompleted"/> is null.</exception>
89 public static IObserver<T> Create<T>(Action<T> onNext, Action onCompleted)
90 {
91 if (onNext == null)
92 throw new ArgumentNullException("onNext");
93 if (onCompleted == null)
94 throw new ArgumentNullException("onCompleted");
95
96 return new AnonymousObserver<T>(onNext, onCompleted);
97 }
98
99 /// <summary>
100 /// Creates an observer from the specified OnNext, OnError, and OnCompleted actions.
101 /// </summary>
102 /// <typeparam name="T">The type of the elements received by the observer.</typeparam>
103 /// <param name="onNext">Observer's OnNext action implementation.</param>
104 /// <param name="onError">Observer's OnError action implementation.</param>
105 /// <param name="onCompleted">Observer's OnCompleted action implementation.</param>
106 /// <returns>The observer object implemented using the given actions.</returns>
107 /// <exception cref="ArgumentNullException"><paramref name="onNext"/> or <paramref name="onError"/> or <paramref name="onCompleted"/> is null.</exception>
108 public static IObserver<T> Create<T>(Action<T> onNext, Action<Exception> onError, Action onCompleted)
109 {
110 if (onNext == null)
111 throw new ArgumentNullException("onNext");
112 if (onError == null)
113 throw new ArgumentNullException("onError");
114 if (onCompleted == null)
115 throw new ArgumentNullException("onCompleted");
116
117 return new AnonymousObserver<T>(onNext, onError, onCompleted);
118 }
119
120 /// <summary>
121 /// Hides the identity of an observer.
122 /// </summary>
123 /// <typeparam name="T">The type of the elements received by the source observer.</typeparam>
124 /// <param name="observer">An observer whose identity to hide.</param>
125 /// <returns>An observer that hides the identity of the specified observer.</returns>
126 /// <exception cref="ArgumentNullException"><paramref name="observer"/> is null.</exception>
127 public static IObserver<T> AsObserver<T>(this IObserver<T> observer)
128 {
129 if (observer == null)
130 throw new ArgumentNullException("observer");
131
132 return new AnonymousObserver<T>(observer.OnNext, observer.OnError, observer.OnCompleted);
133 }
134
135 /// <summary>
136 /// Checks access to the observer for grammar violations. This includes checking for multiple OnError or OnCompleted calls, as well as reentrancy in any of the observer methods.
137 /// If a violation is detected, an InvalidOperationException is thrown from the offending observer method call.
138 /// </summary>
139 /// <typeparam name="T">The type of the elements received by the source observer.</typeparam>
140 /// <param name="observer">The observer whose callback invocations should be checked for grammar violations.</param>
141 /// <returns>An observer that checks callbacks invocations against the observer grammar and, if the checks pass, forwards those to the specified observer.</returns>
142 /// <exception cref="ArgumentNullException"><paramref name="observer"/> is null.</exception>
143 public static IObserver<T> Checked<T>(this IObserver<T> observer)
144 {
145 if (observer == null)
146 throw new ArgumentNullException("observer");
147
148 return new CheckedObserver<T>(observer);
149 }
150
151 /// <summary>
152 /// Synchronizes access to the observer such that its callback methods cannot be called concurrently from multiple threads. This overload is useful when coordinating access to an observer.
153 /// Notice reentrant observer callbacks on the same thread are still possible.
154 /// </summary>
155 /// <typeparam name="T">The type of the elements received by the source observer.</typeparam>
156 /// <param name="observer">The observer whose callbacks should be synchronized.</param>
157 /// <returns>An observer that delivers callbacks to the specified observer in a synchronized manner.</returns>
158 /// <exception cref="ArgumentNullException"><paramref name="observer"/> is null.</exception>
159 /// <remarks>
160 /// Because a <see cref="System.Threading.Monitor">Monitor</see> is used to perform the synchronization, there's no protection against reentrancy from the same thread.
161 /// Hence, overlapped observer callbacks are still possible, which is invalid behavior according to the observer grammar. In order to protect against this behavior as
162 /// well, use the <see cref="Synchronize{T}(IObserver{T}, bool)"/> overload, passing true for the second parameter.
163 /// </remarks>
164 public static IObserver<T> Synchronize<T>(IObserver<T> observer)
165 {
166 if (observer == null)
167 throw new ArgumentNullException("observer");
168
169 return new SynchronizedObserver<T>(observer, new object());
170 }
171
172 /// <summary>
173 /// Synchronizes access to the observer such that its callback methods cannot be called concurrently. This overload is useful when coordinating access to an observer.
174 /// The <paramref name="preventReentrancy"/> parameter configures the type of lock used for synchronization.
175 /// </summary>
176 /// <typeparam name="T">The type of the elements received by the source observer.</typeparam>
177 /// <param name="observer">The observer whose callbacks should be synchronized.</param>
178 /// <param name="preventReentrancy">If set to true, reentrant observer callbacks will be queued up and get delivered to the observer in a sequential manner.</param>
179 /// <returns>An observer that delivers callbacks to the specified observer in a synchronized manner.</returns>
180 /// <exception cref="ArgumentNullException"><paramref name="observer"/> is null.</exception>
181 /// <remarks>
182 /// When the <paramref name="preventReentrancy"/> parameter is set to false, behavior is identical to the <see cref="Synchronize{T}(IObserver{T})"/> overload which uses
183 /// a <see cref="System.Threading.Monitor">Monitor</see> for synchronization. When the <paramref name="preventReentrancy"/> parameter is set to true, an <see cref="AsyncLock"/>
184 /// is used to queue up callbacks to the specified observer if a reentrant call is made.
185 /// </remarks>
186 public static IObserver<T> Synchronize<T>(IObserver<T> observer, bool preventReentrancy)
187 {
188 if (observer == null)
189 throw new ArgumentNullException("observer");
190
191 if (preventReentrancy)
192 return new AsyncLockObserver<T>(observer, new AsyncLock());
193 else
194 return new SynchronizedObserver<T>(observer, new object());
195 }
196
197 /// <summary>
198 /// Synchronizes access to the observer such that its callback methods cannot be called concurrently by multiple threads, using the specified gate object for use by a <see cref="System.Threading.Monitor">Monitor</see>-based lock.
199 /// This overload is useful when coordinating multiple observers that access shared state by synchronizing on a common gate object.
200 /// Notice reentrant observer callbacks on the same thread are still possible.
201 /// </summary>
202 /// <typeparam name="T">The type of the elements received by the source observer.</typeparam>
203 /// <param name="observer">The observer whose callbacks should be synchronized.</param>
204 /// <param name="gate">Gate object to synchronize each observer call on.</param>
205 /// <returns>An observer that delivers callbacks to the specified observer in a synchronized manner.</returns>
206 /// <exception cref="ArgumentNullException"><paramref name="observer"/> or <paramref name="gate"/> is null.</exception>
207 /// <remarks>
208 /// Because a <see cref="System.Threading.Monitor">Monitor</see> is used to perform the synchronization, there's no protection against reentrancy from the same thread.
209 /// Hence, overlapped observer callbacks are still possible, which is invalid behavior according to the observer grammar. In order to protect against this behavior as
210 /// well, use the <see cref="Synchronize{T}(IObserver{T}, AsyncLock)"/> overload.
211 /// </remarks>
212 public static IObserver<T> Synchronize<T>(IObserver<T> observer, object gate)
213 {
214 if (observer == null)
215 throw new ArgumentNullException("observer");
216 if (gate == null)
217 throw new ArgumentNullException("gate");
218
219 return new SynchronizedObserver<T>(observer, gate);
220 }
221
222 /// <summary>
223 /// Synchronizes access to the observer such that its callback methods cannot be called concurrently, using the specified asynchronous lock to protect against concurrent and reentrant access.
224 /// This overload is useful when coordinating multiple observers that access shared state by synchronizing on a common asynchronous lock.
225 /// </summary>
226 /// <typeparam name="T">The type of the elements received by the source observer.</typeparam>
227 /// <param name="observer">The observer whose callbacks should be synchronized.</param>
228 /// <param name="asyncLock">Gate object to synchronize each observer call on.</param>
229 /// <returns>An observer that delivers callbacks to the specified observer in a synchronized manner.</returns>
230 /// <exception cref="ArgumentNullException"><paramref name="observer"/> or <paramref name="asyncLock"/> is null.</exception>
231 public static IObserver<T> Synchronize<T>(IObserver<T> observer, AsyncLock asyncLock)
232 {
233 if (observer == null)
234 throw new ArgumentNullException("observer");
235 if (asyncLock == null)
236 throw new ArgumentNullException("asyncLock");
237
238 return new AsyncLockObserver<T>(observer, asyncLock);
239 }
240
241 /// <summary>
242 /// Schedules the invocation of observer methods on the given scheduler.
243 /// </summary>
244 /// <typeparam name="T">The type of the elements received by the source observer.</typeparam>
245 /// <param name="observer">The observer to schedule messages for.</param>
246 /// <param name="scheduler">Scheduler to schedule observer messages on.</param>
247 /// <returns>Observer whose messages are scheduled on the given scheduler.</returns>
248 /// <exception cref="ArgumentNullException"><paramref name="observer"/> or <paramref name="scheduler"/> is null.</exception>
249 public static IObserver<T> NotifyOn<T>(this IObserver<T> observer, IScheduler scheduler)
250 {
251 if (observer == null)
252 throw new ArgumentNullException("observer");
253 if (scheduler == null)
254 throw new ArgumentNullException("scheduler");
255
256 return new ObserveOnObserver<T>(scheduler, observer, null);
257 }
258
259 #if !NO_SYNCCTX
260 /// <summary>
261 /// Schedules the invocation of observer methods on the given synchonization context.
262 /// </summary>
263 /// <typeparam name="T">The type of the elements received by the source observer.</typeparam>
264 /// <param name="observer">The observer to schedule messages for.</param>
265 /// <param name="context">Synchonization context to schedule observer messages on.</param>
266 /// <returns>Observer whose messages are scheduled on the given synchonization context.</returns>
267 /// <exception cref="ArgumentNullException"><paramref name="observer"/> or <paramref name="context"/> is null.</exception>
268 public static IObserver<T> NotifyOn<T>(this IObserver<T> observer, SynchronizationContext context)
269 {
270 if (observer == null)
271 throw new ArgumentNullException("observer");
272 if (context == null)
273 throw new ArgumentNullException("context");
274
275 return new ObserveOnObserver<T>(new SynchronizationContextScheduler(context), observer, null);
276 }
277 #endif
278
279 #if HAS_PROGRESS
280 /// <summary>
281 /// Converts an observer to a progress object.
282 /// </summary>
283 /// <typeparam name="T">The type of the progress objects received by the source observer.</typeparam>
284 /// <param name="observer">The observer to convert.</param>
285 /// <returns>Progress object whose Report messages correspond to the observer's OnNext messages.</returns>
286 /// <exception cref="ArgumentNullException"><paramref name="observer"/> is null.</exception>
287 public static IProgress<T> ToProgress<T>(this IObserver<T> observer)
288 {
289 if (observer == null)
290 throw new ArgumentNullException("observer");
291
292 return new AnonymousProgress<T>(observer.OnNext);
293 }
294
295 /// <summary>
296 /// Converts an observer to a progress object, using the specified scheduler to invoke the progress reporting method.
297 /// </summary>
298 /// <typeparam name="T">The type of the progress objects received by the source observer.</typeparam>
299 /// <param name="observer">The observer to convert.</param>
300 /// <param name="scheduler">Scheduler to report progress on.</param>
301 /// <returns>Progress object whose Report messages correspond to the observer's OnNext messages.</returns>
302 /// <exception cref="ArgumentNullException"><paramref name="observer"/> or <paramref name="scheduler"/> is null.</exception>
303 public static IProgress<T> ToProgress<T>(this IObserver<T> observer, IScheduler scheduler)
304 {
305 if (observer == null)
306 throw new ArgumentNullException("observer");
307 if (scheduler == null)
308 throw new ArgumentNullException("scheduler");
309
310 return new AnonymousProgress<T>(new ObserveOnObserver<T>(scheduler, observer, null).OnNext);
311 }
312
313 class AnonymousProgress<T> : IProgress<T>
314 {
315 private readonly Action<T> _progress;
316
317 public AnonymousProgress(Action<T> progress)
318 {
319 _progress = progress;
320 }
321
322 public void Report(T value)
323 {
324 _progress(value);
325 }
326 }
327
328 /// <summary>
329 /// Converts a progress object to an observer.
330 /// </summary>
331 /// <typeparam name="T">The type of the progress objects received by the progress reporter.</typeparam>
332 /// <param name="progress">The progress object to convert.</param>
333 /// <returns>Observer whose OnNext messages correspond to the progress object's Report messages.</returns>
334 /// <exception cref="ArgumentNullException"><paramref name="progress"/> is null.</exception>
335 public static IObserver<T> ToObserver<T>(this IProgress<T> progress)
336 {
337 if (progress == null)
338 throw new ArgumentNullException("progress");
339
340 return new AnonymousObserver<T>(progress.Report);
341 }
342 #endif
343 }
344 }