| 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 |
/// <param name="handler">Action that handles a notification.</param> |
| 17 |
/// <returns>The observer object that invokes the specified handler using a notification corresponding to each message it receives.</returns> |
| 18 |
public static IObserver<T> ToObserver<T>(this Action<Notification<T>> handler) |
| 19 |
{ |
| 20 |
if (handler == null) |
| 21 |
throw new ArgumentNullException("handler"); |
| 22 |
|
| 23 |
return new AnonymousObserver<T>( |
| 24 |
x => handler(Notification.CreateOnNext<T>(x)), |
| 25 |
exception => handler(Notification.CreateOnError<T>(exception)), |
| 26 |
() => handler(Notification.CreateOnCompleted<T>())); |
| 27 |
} |
| 28 |
|
| 29 |
/// <summary> |
| 30 |
/// Creates a notification callback from an observer. |
| 31 |
/// </summary> |
| 32 |
/// <param name="observer">Observer object.</param> |
| 33 |
/// <returns>The action that forwards its input notification to the underlying observer.</returns> |
| 34 |
public static Action<Notification<T>> ToNotifier<T>(this IObserver<T> observer) |
| 35 |
{ |
| 36 |
if (observer == null) |
| 37 |
throw new ArgumentNullException("observer"); |
| 38 |
|
| 39 |
return n => n.Accept(observer); |
| 40 |
} |
| 41 |
|
| 42 |
/// <summary> |
| 43 |
/// Creates an observer from the specified OnNext action. |
| 44 |
/// </summary> |
| 45 |
/// <param name="onNext">Observer's OnNext action implementation.</param> |
| 46 |
/// <returns>The observer object implemented using the given actions.</returns> |
| 47 |
public static IObserver<T> Create<T>(Action<T> onNext) |
| 48 |
{ |
| 49 |
if (onNext == null) |
| 50 |
throw new ArgumentNullException("onNext"); |
| 51 |
|
| 52 |
return new AnonymousObserver<T>(onNext); |
| 53 |
} |
| 54 |
|
| 55 |
/// <summary> |
| 56 |
/// Creates an observer from the specified OnNext and OnError actions. |
| 57 |
/// </summary> |
| 58 |
/// <param name="onNext">Observer's OnNext action implementation.</param> |
| 59 |
/// <param name="onError">Observer's OnError action implementation.</param> |
| 60 |
/// <returns>The observer object implemented using the given actions.</returns> |
| 61 |
public static IObserver<T> Create<T>(Action<T> onNext, Action<Exception> onError) |
| 62 |
{ |
| 63 |
if (onNext == null) |
| 64 |
throw new ArgumentNullException("onNext"); |
| 65 |
if (onError == null) |
| 66 |
throw new ArgumentNullException("onError"); |
| 67 |
|
| 68 |
return new AnonymousObserver<T>(onNext, onError); |
| 69 |
} |
| 70 |
|
| 71 |
/// <summary> |
| 72 |
/// Creates an observer from the specified OnNext and OnCompleted actions. |
| 73 |
/// </summary> |
| 74 |
/// <param name="onNext">Observer's OnNext action implementation.</param> |
| 75 |
/// <param name="onCompleted">Observer's OnCompleted action implementation.</param> |
| 76 |
/// <returns>The observer object implemented using the given actions.</returns> |
| 77 |
public static IObserver<T> Create<T>(Action<T> onNext, Action onCompleted) |
| 78 |
{ |
| 79 |
if (onNext == null) |
| 80 |
throw new ArgumentNullException("onNext"); |
| 81 |
if (onCompleted == null) |
| 82 |
throw new ArgumentNullException("onCompleted"); |
| 83 |
|
| 84 |
return new AnonymousObserver<T>(onNext, onCompleted); |
| 85 |
} |
| 86 |
|
| 87 |
/// <summary> |
| 88 |
/// Creates an observer from the specified OnNext, OnError, and OnCompleted actions. |
| 89 |
/// </summary> |
| 90 |
/// <param name="onNext">Observer's OnNext action implementation.</param> |
| 91 |
/// <param name="onError">Observer's OnError action implementation.</param> |
| 92 |
/// <param name="onCompleted">Observer's OnCompleted action implementation.</param> |
| 93 |
/// <returns>The observer object implemented using the given actions.</returns> |
| 94 |
public static IObserver<T> Create<T>(Action<T> onNext, Action<Exception> onError, Action onCompleted) |
| 95 |
{ |
| 96 |
if (onNext == null) |
| 97 |
throw new ArgumentNullException("onNext"); |
| 98 |
if (onError == null) |
| 99 |
throw new ArgumentNullException("onError"); |
| 100 |
if (onCompleted == null) |
| 101 |
throw new ArgumentNullException("onCompleted"); |
| 102 |
|
| 103 |
return new AnonymousObserver<T>(onNext, onError, onCompleted); |
| 104 |
} |
| 105 |
|
| 106 |
/// <summary> |
| 107 |
/// Hides the identity of an observer. |
| 108 |
/// </summary> |
| 109 |
/// <param name="observer">An observer whose identity to hide.</param> |
| 110 |
/// <returns>An observer that hides the identity of the specified observer.</returns> |
| 111 |
public static IObserver<T> AsObserver<T>(this IObserver<T> observer) |
| 112 |
{ |
| 113 |
if (observer == null) |
| 114 |
throw new ArgumentNullException("observer"); |
| 115 |
|
| 116 |
return new AnonymousObserver<T>(observer.OnNext, observer.OnError, observer.OnCompleted); |
| 117 |
} |
| 118 |
|
| 119 |
/// <summary> |
| 120 |
/// Synchronizes the observer messages. |
| 121 |
/// </summary> |
| 122 |
/// <param name="observer">The observer to synchronize.</param> |
| 123 |
/// <param name="gate">Gate object to synchronize each observer call on.</param> |
| 124 |
/// <returns>The observer whose messages are synchronized on the given gate object.</returns> |
| 125 |
public static IObserver<T> Synchronize<T>(IObserver<T> observer, object gate) |
| 126 |
{ |
| 127 |
if (observer == null) |
| 128 |
throw new ArgumentNullException("observer"); |
| 129 |
if (gate == null) |
| 130 |
throw new ArgumentNullException("gate"); |
| 131 |
|
| 132 |
return new SynchronizedObserver<T>(observer, gate); |
| 133 |
} |
| 134 |
|
| 135 |
/// <summary> |
| 136 |
/// Synchronizes the observer messages. |
| 137 |
/// </summary> |
| 138 |
/// <param name="observer">The observer to synchronize.</param> |
| 139 |
/// <returns>The observer whose messages are synchronized.</returns> |
| 140 |
public static IObserver<T> Synchronize<T>(IObserver<T> observer) |
| 141 |
{ |
| 142 |
if (observer == null) |
| 143 |
throw new ArgumentNullException("observer"); |
| 144 |
|
| 145 |
return Synchronize(observer, new object()); |
| 146 |
} |
| 147 |
|
| 148 |
/// <summary> |
| 149 |
/// Schedules the observer messages on the given scheduler. |
| 150 |
/// </summary> |
| 151 |
/// <param name="observer">The observer to schedule messages for.</param> |
| 152 |
/// <param name="scheduler">Scheduler to schedule observer messages on.</param> |
| 153 |
/// <returns>Observer whose messages are scheduled on the given scheduler.</returns> |
| 154 |
public static IObserver<T> NotifyOn<T>(this IObserver<T> observer, IScheduler scheduler) |
| 155 |
{ |
| 156 |
if (observer == null) |
| 157 |
throw new ArgumentNullException("observer"); |
| 158 |
if (scheduler == null) |
| 159 |
throw new ArgumentNullException("scheduler"); |
| 160 |
|
| 161 |
return new ObserveOnObserver<T>(scheduler, observer, null); |
| 162 |
} |
| 163 |
|
| 164 |
#if !NO_SYNCCTX |
| 165 |
/// <summary> |
| 166 |
/// Schedules the observer messages on the given synchonization context. |
| 167 |
/// </summary> |
| 168 |
/// <param name="observer">The observer to schedule messages for.</param> |
| 169 |
/// <param name="context">Synchonization context to schedule observer messages on.</param> |
| 170 |
/// <returns>Observer whose messages are scheduled on the given synchonization context.</returns> |
| 171 |
public static IObserver<T> NotifyOn<T>(this IObserver<T> observer, SynchronizationContext context) |
| 172 |
{ |
| 173 |
if (observer == null) |
| 174 |
throw new ArgumentNullException("observer"); |
| 175 |
if (context == null) |
| 176 |
throw new ArgumentNullException("context"); |
| 177 |
|
| 178 |
return new ObserveOnObserver<T>(new SynchronizationContextScheduler(context), observer, null); |
| 179 |
} |
| 180 |
#endif |
| 181 |
|
| 182 |
#if HAS_PROGRESS |
| 183 |
/// <summary> |
| 184 |
/// Converts an observer to a progress object. |
| 185 |
/// </summary> |
| 186 |
/// <param name="observer">The observer to convert.</param> |
| 187 |
/// <returns>Progress object whose Report messages correspond to the observer's OnNext messages.</returns> |
| 188 |
public static IProgress<T> ToProgress<T>(this IObserver<T> observer) |
| 189 |
{ |
| 190 |
if (observer == null) |
| 191 |
throw new ArgumentNullException("observer"); |
| 192 |
|
| 193 |
return new AnonymousProgress<T>(observer.OnNext); |
| 194 |
} |
| 195 |
|
| 196 |
/// <summary> |
| 197 |
/// Converts an observer to a progress object. |
| 198 |
/// </summary> |
| 199 |
/// <param name="observer">The observer to convert.</param> |
| 200 |
/// <param name="scheduler">Scheduler to report progress on.</param> |
| 201 |
/// <returns>Progress object whose Report messages correspond to the observer's OnNext messages.</returns> |
| 202 |
public static IProgress<T> ToProgress<T>(this IObserver<T> observer, IScheduler scheduler) |
| 203 |
{ |
| 204 |
if (observer == null) |
| 205 |
throw new ArgumentNullException("observer"); |
| 206 |
if (scheduler == null) |
| 207 |
throw new ArgumentNullException("scheduler"); |
| 208 |
|
| 209 |
return new AnonymousProgress<T>(new ObserveOnObserver<T>(scheduler, observer, null).OnNext); |
| 210 |
} |
| 211 |
|
| 212 |
class AnonymousProgress<T> : IProgress<T> |
| 213 |
{ |
| 214 |
private readonly Action<T> _progress; |
| 215 |
|
| 216 |
public AnonymousProgress(Action<T> progress) |
| 217 |
{ |
| 218 |
_progress = progress; |
| 219 |
} |
| 220 |
|
| 221 |
public void Report(T value) |
| 222 |
{ |
| 223 |
_progress(value); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/// <summary> |
| 228 |
/// Converts a progress object to an observer. |
| 229 |
/// </summary> |
| 230 |
/// <param name="progress">The progress object to convert.</param> |
| 231 |
/// <returns>Observer whose OnNext messages correspond to the progress object's Report messages.</returns> |
| 232 |
public static IObserver<T> ToObserver<T>(this IProgress<T> progress) |
| 233 |
{ |
| 234 |
if (progress == null) |
| 235 |
throw new ArgumentNullException("progress"); |
| 236 |
|
| 237 |
return Create<T>(progress.Report); |
| 238 |
} |
| 239 |
#endif |
| 240 |
} |
| 241 |
} |