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