| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System.ComponentModel; |
| 4 |
using System.Reactive; |
| 5 |
using System.Reactive.Disposables; |
| 6 |
using System.Threading; |
| 7 |
|
| 8 |
namespace System |
| 9 |
{ |
| 10 |
/// <summary> |
| 11 |
/// Provides a set of static methods for subscribing delegates to observables. |
| 12 |
/// </summary> |
| 13 |
public static class ObservableExtensions |
| 14 |
{ |
| 15 |
#region Subscribe delegate-based overloads |
| 16 |
|
| 17 |
/// <summary> |
| 18 |
/// Subscribes to the observable sequence without specifying any handlers. |
| 19 |
/// This method can be used to evaluate the observable sequence for its side-effects only. |
| 20 |
/// </summary> |
| 21 |
/// <typeparam name="T">The type of the elements in the source sequence.</typeparam> |
| 22 |
/// <param name="source">Observable sequence to subscribe to.</param> |
| 23 |
/// <returns>IDisposable object used to unsubscribe from the observable sequence.</returns> |
| 24 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception> |
| 25 |
public static IDisposable Subscribe<T>(this IObservable<T> source) |
| 26 |
{ |
| 27 |
if (source == null) |
| 28 |
throw new ArgumentNullException("source"); |
| 29 |
|
| 30 |
// |
| 31 |
// [OK] Use of unsafe Subscribe: non-pretentious constructor for an observer; this overload is not to be used internally. |
| 32 |
// |
| 33 |
return source.Subscribe/*Unsafe*/(new AnonymousObserver<T>(Stubs<T>.Ignore, Stubs.Throw, Stubs.Nop)); |
| 34 |
} |
| 35 |
|
| 36 |
/// <summary> |
| 37 |
/// Subscribes an element handler to an observable sequence. |
| 38 |
/// </summary> |
| 39 |
/// <typeparam name="T">The type of the elements in the source sequence.</typeparam> |
| 40 |
/// <param name="source">Observable sequence to subscribe to.</param> |
| 41 |
/// <param name="onNext">Action to invoke for each element in the observable sequence.</param> |
| 42 |
/// <returns>IDisposable object used to unsubscribe from the observable sequence.</returns> |
| 43 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> is null.</exception> |
| 44 |
public static IDisposable Subscribe<T>(this IObservable<T> source, Action<T> onNext) |
| 45 |
{ |
| 46 |
if (source == null) |
| 47 |
throw new ArgumentNullException("source"); |
| 48 |
if (onNext == null) |
| 49 |
throw new ArgumentNullException("onNext"); |
| 50 |
|
| 51 |
// |
| 52 |
// [OK] Use of unsafe Subscribe: non-pretentious constructor for an observer; this overload is not to be used internally. |
| 53 |
// |
| 54 |
return source.Subscribe/*Unsafe*/(new AnonymousObserver<T>(onNext, Stubs.Throw, Stubs.Nop)); |
| 55 |
} |
| 56 |
|
| 57 |
/// <summary> |
| 58 |
/// Subscribes an element handler and an exception handler to an observable sequence. |
| 59 |
/// </summary> |
| 60 |
/// <typeparam name="T">The type of the elements in the source sequence.</typeparam> |
| 61 |
/// <param name="source">Observable sequence to subscribe to.</param> |
| 62 |
/// <param name="onNext">Action to invoke for each element in the observable sequence.</param> |
| 63 |
/// <param name="onError">Action to invoke upon exceptional termination of the observable sequence.</param> |
| 64 |
/// <returns>IDisposable object used to unsubscribe from the observable sequence.</returns> |
| 65 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> or <paramref name="onError"/> is null.</exception> |
| 66 |
public static IDisposable Subscribe<T>(this IObservable<T> source, Action<T> onNext, Action<Exception> onError) |
| 67 |
{ |
| 68 |
if (source == null) |
| 69 |
throw new ArgumentNullException("source"); |
| 70 |
if (onNext == null) |
| 71 |
throw new ArgumentNullException("onNext"); |
| 72 |
if (onError == null) |
| 73 |
throw new ArgumentNullException("onError"); |
| 74 |
|
| 75 |
// |
| 76 |
// [OK] Use of unsafe Subscribe: non-pretentious constructor for an observer; this overload is not to be used internally. |
| 77 |
// |
| 78 |
return source.Subscribe/*Unsafe*/(new AnonymousObserver<T>(onNext, onError, Stubs.Nop)); |
| 79 |
} |
| 80 |
|
| 81 |
/// <summary> |
| 82 |
/// Subscribes an element handler and a completion handler to an observable sequence. |
| 83 |
/// </summary> |
| 84 |
/// <typeparam name="T">The type of the elements in the source sequence.</typeparam> |
| 85 |
/// <param name="source">Observable sequence to subscribe to.</param> |
| 86 |
/// <param name="onNext">Action to invoke for each element in the observable sequence.</param> |
| 87 |
/// <param name="onCompleted">Action to invoke upon graceful termination of the observable sequence.</param> |
| 88 |
/// <returns>IDisposable object used to unsubscribe from the observable sequence.</returns> |
| 89 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> or <paramref name="onCompleted"/> is null.</exception> |
| 90 |
public static IDisposable Subscribe<T>(this IObservable<T> source, Action<T> onNext, Action onCompleted) |
| 91 |
{ |
| 92 |
if (source == null) |
| 93 |
throw new ArgumentNullException("source"); |
| 94 |
if (onNext == null) |
| 95 |
throw new ArgumentNullException("onNext"); |
| 96 |
if (onCompleted == null) |
| 97 |
throw new ArgumentNullException("onCompleted"); |
| 98 |
|
| 99 |
// |
| 100 |
// [OK] Use of unsafe Subscribe: non-pretentious constructor for an observer; this overload is not to be used internally. |
| 101 |
// |
| 102 |
return source.Subscribe/*Unsafe*/(new AnonymousObserver<T>(onNext, Stubs.Throw, onCompleted)); |
| 103 |
} |
| 104 |
|
| 105 |
/// <summary> |
| 106 |
/// Subscribes an element handler, an exception handler, and a completion handler to an observable sequence. |
| 107 |
/// </summary> |
| 108 |
/// <typeparam name="T">The type of the elements in the source sequence.</typeparam> |
| 109 |
/// <param name="source">Observable sequence to subscribe to.</param> |
| 110 |
/// <param name="onNext">Action to invoke for each element in the observable sequence.</param> |
| 111 |
/// <param name="onError">Action to invoke upon exceptional termination of the observable sequence.</param> |
| 112 |
/// <param name="onCompleted">Action to invoke upon graceful termination of the observable sequence.</param> |
| 113 |
/// <returns>IDisposable object used to unsubscribe from the observable sequence.</returns> |
| 114 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> or <paramref name="onError"/> or <paramref name="onCompleted"/> is null.</exception> |
| 115 |
public static IDisposable Subscribe<T>(this IObservable<T> source, Action<T> onNext, Action<Exception> onError, Action onCompleted) |
| 116 |
{ |
| 117 |
if (source == null) |
| 118 |
throw new ArgumentNullException("source"); |
| 119 |
if (onNext == null) |
| 120 |
throw new ArgumentNullException("onNext"); |
| 121 |
if (onError == null) |
| 122 |
throw new ArgumentNullException("onError"); |
| 123 |
if (onCompleted == null) |
| 124 |
throw new ArgumentNullException("onCompleted"); |
| 125 |
|
| 126 |
// |
| 127 |
// [OK] Use of unsafe Subscribe: non-pretentious constructor for an observer; this overload is not to be used internally. |
| 128 |
// |
| 129 |
return source.Subscribe/*Unsafe*/(new AnonymousObserver<T>(onNext, onError, onCompleted)); |
| 130 |
} |
| 131 |
|
| 132 |
#endregion |
| 133 |
|
| 134 |
#region Subscribe overloads with CancellationToken |
| 135 |
|
| 136 |
#if !NO_TPL |
| 137 |
/// <summary> |
| 138 |
/// Subscribes an observer to an observable sequence, using a CancellationToken to support unsubscription. |
| 139 |
/// </summary> |
| 140 |
/// <typeparam name="T">The type of the elements in the source sequence.</typeparam> |
| 141 |
/// <param name="source">Observable sequence to subscribe to.</param> |
| 142 |
/// <param name="observer">Observer to subscribe to the sequence.</param> |
| 143 |
/// <param name="token">CancellationToken that can be signaled to unsubscribe from the source sequence.</param> |
| 144 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="observer"/> is null.</exception> |
| 145 |
public static void Subscribe<T>(this IObservable<T> source, IObserver<T> observer, CancellationToken token) |
| 146 |
{ |
| 147 |
if (source == null) |
| 148 |
throw new ArgumentNullException("source"); |
| 149 |
if (observer == null) |
| 150 |
throw new ArgumentNullException("observer"); |
| 151 |
|
| 152 |
source.Subscribe_(observer, token); |
| 153 |
} |
| 154 |
|
| 155 |
/// <summary> |
| 156 |
/// Subscribes to the observable sequence without specifying any handlers, using a CancellationToken to support unsubscription. |
| 157 |
/// This method can be used to evaluate the observable sequence for its side-effects only. |
| 158 |
/// </summary> |
| 159 |
/// <typeparam name="T">The type of the elements in the source sequence.</typeparam> |
| 160 |
/// <param name="source">Observable sequence to subscribe to.</param> |
| 161 |
/// <param name="token">CancellationToken that can be signaled to unsubscribe from the source sequence.</param> |
| 162 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception> |
| 163 |
public static void Subscribe<T>(this IObservable<T> source, CancellationToken token) |
| 164 |
{ |
| 165 |
if (source == null) |
| 166 |
throw new ArgumentNullException("source"); |
| 167 |
|
| 168 |
source.Subscribe_(new AnonymousObserver<T>(Stubs<T>.Ignore, Stubs.Throw, Stubs.Nop), token); |
| 169 |
} |
| 170 |
|
| 171 |
/// <summary> |
| 172 |
/// Subscribes an element handler to an observable sequence, using a CancellationToken to support unsubscription. |
| 173 |
/// </summary> |
| 174 |
/// <typeparam name="T">The type of the elements in the source sequence.</typeparam> |
| 175 |
/// <param name="source">Observable sequence to subscribe to.</param> |
| 176 |
/// <param name="onNext">Action to invoke for each element in the observable sequence.</param> |
| 177 |
/// <param name="token">CancellationToken that can be signaled to unsubscribe from the source sequence.</param> |
| 178 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> is null.</exception> |
| 179 |
public static void Subscribe<T>(this IObservable<T> source, Action<T> onNext, CancellationToken token) |
| 180 |
{ |
| 181 |
if (source == null) |
| 182 |
throw new ArgumentNullException("source"); |
| 183 |
if (onNext == null) |
| 184 |
throw new ArgumentNullException("onNext"); |
| 185 |
|
| 186 |
source.Subscribe_(new AnonymousObserver<T>(onNext, Stubs.Throw, Stubs.Nop), token); |
| 187 |
} |
| 188 |
|
| 189 |
/// <summary> |
| 190 |
/// Subscribes an element handler and an exception handler to an observable sequence, using a CancellationToken to support unsubscription. |
| 191 |
/// </summary> |
| 192 |
/// <typeparam name="T">The type of the elements in the source sequence.</typeparam> |
| 193 |
/// <param name="source">Observable sequence to subscribe to.</param> |
| 194 |
/// <param name="onNext">Action to invoke for each element in the observable sequence.</param> |
| 195 |
/// <param name="onError">Action to invoke upon exceptional termination of the observable sequence.</param> |
| 196 |
/// <param name="token">CancellationToken that can be signaled to unsubscribe from the source sequence.</param> |
| 197 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> or <paramref name="onError"/> is null.</exception> |
| 198 |
public static void Subscribe<T>(this IObservable<T> source, Action<T> onNext, Action<Exception> onError, CancellationToken token) |
| 199 |
{ |
| 200 |
if (source == null) |
| 201 |
throw new ArgumentNullException("source"); |
| 202 |
if (onNext == null) |
| 203 |
throw new ArgumentNullException("onNext"); |
| 204 |
if (onError == null) |
| 205 |
throw new ArgumentNullException("onError"); |
| 206 |
|
| 207 |
source.Subscribe_(new AnonymousObserver<T>(onNext, onError, Stubs.Nop), token); |
| 208 |
} |
| 209 |
|
| 210 |
/// <summary> |
| 211 |
/// Subscribes an element handler and a completion handler to an observable sequence, using a CancellationToken to support unsubscription. |
| 212 |
/// </summary> |
| 213 |
/// <typeparam name="T">The type of the elements in the source sequence.</typeparam> |
| 214 |
/// <param name="source">Observable sequence to subscribe to.</param> |
| 215 |
/// <param name="onNext">Action to invoke for each element in the observable sequence.</param> |
| 216 |
/// <param name="onCompleted">Action to invoke upon graceful termination of the observable sequence.</param> |
| 217 |
/// <param name="token">CancellationToken that can be signaled to unsubscribe from the source sequence.</param> |
| 218 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> or <paramref name="onCompleted"/> is null.</exception> |
| 219 |
public static void Subscribe<T>(this IObservable<T> source, Action<T> onNext, Action onCompleted, CancellationToken token) |
| 220 |
{ |
| 221 |
if (source == null) |
| 222 |
throw new ArgumentNullException("source"); |
| 223 |
if (onNext == null) |
| 224 |
throw new ArgumentNullException("onNext"); |
| 225 |
if (onCompleted == null) |
| 226 |
throw new ArgumentNullException("onCompleted"); |
| 227 |
|
| 228 |
source.Subscribe_(new AnonymousObserver<T>(onNext, Stubs.Throw, onCompleted), token); |
| 229 |
} |
| 230 |
|
| 231 |
/// <summary> |
| 232 |
/// Subscribes an element handler, an exception handler, and a completion handler to an observable sequence, using a CancellationToken to support unsubscription. |
| 233 |
/// </summary> |
| 234 |
/// <typeparam name="T">The type of the elements in the source sequence.</typeparam> |
| 235 |
/// <param name="source">Observable sequence to subscribe to.</param> |
| 236 |
/// <param name="onNext">Action to invoke for each element in the observable sequence.</param> |
| 237 |
/// <param name="onError">Action to invoke upon exceptional termination of the observable sequence.</param> |
| 238 |
/// <param name="onCompleted">Action to invoke upon graceful termination of the observable sequence.</param> |
| 239 |
/// <param name="token">CancellationToken that can be signaled to unsubscribe from the source sequence.</param> |
| 240 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> or <paramref name="onError"/> or <paramref name="onCompleted"/> is null.</exception> |
| 241 |
public static void Subscribe<T>(this IObservable<T> source, Action<T> onNext, Action<Exception> onError, Action onCompleted, CancellationToken token) |
| 242 |
{ |
| 243 |
if (source == null) |
| 244 |
throw new ArgumentNullException("source"); |
| 245 |
if (onNext == null) |
| 246 |
throw new ArgumentNullException("onNext"); |
| 247 |
if (onError == null) |
| 248 |
throw new ArgumentNullException("onError"); |
| 249 |
if (onCompleted == null) |
| 250 |
throw new ArgumentNullException("onCompleted"); |
| 251 |
|
| 252 |
source.Subscribe_(new AnonymousObserver<T>(onNext, onError, onCompleted), token); |
| 253 |
} |
| 254 |
|
| 255 |
private static void Subscribe_<T>(this IObservable<T> source, IObserver<T> observer, CancellationToken token) |
| 256 |
{ |
| 257 |
if (token.CanBeCanceled) |
| 258 |
{ |
| 259 |
if (!token.IsCancellationRequested) |
| 260 |
{ |
| 261 |
var r = new SingleAssignmentDisposable(); |
| 262 |
|
| 263 |
// |
| 264 |
// [OK] Use of unsafe Subscribe: exception during Subscribe doesn't orphan CancellationTokenRegistration. |
| 265 |
// |
| 266 |
var d = source.Subscribe/*Unsafe*/( |
| 267 |
observer.OnNext, |
| 268 |
ex => |
| 269 |
{ |
| 270 |
using (r) |
| 271 |
observer.OnError(ex); |
| 272 |
}, |
| 273 |
() => |
| 274 |
{ |
| 275 |
using (r) |
| 276 |
observer.OnCompleted(); |
| 277 |
} |
| 278 |
); |
| 279 |
|
| 280 |
r.Disposable = token.Register(d.Dispose); |
| 281 |
} |
| 282 |
} |
| 283 |
else |
| 284 |
{ |
| 285 |
source.Subscribe(observer); |
| 286 |
} |
| 287 |
} |
| 288 |
#endif |
| 289 |
|
| 290 |
#endregion |
| 291 |
|
| 292 |
#region SubscribeSafe |
| 293 |
|
| 294 |
/// <summary> |
| 295 |
/// Subscribes to the specified source, re-routing synchronous exceptions during invocation of the Subscribe method to the observer's OnError channel. |
| 296 |
/// This method is typically used when writing query operators. |
| 297 |
/// </summary> |
| 298 |
/// <typeparam name="T">The type of the elements in the source sequence.</typeparam> |
| 299 |
/// <param name="source">Observable sequence to subscribe to.</param> |
| 300 |
/// <param name="observer">Observer that will be passed to the observable sequence, and that will be used for exception propagation.</param> |
| 301 |
/// <returns>IDisposable object used to unsubscribe from the observable sequence.</returns> |
| 302 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="observer"/> is null.</exception> |
| 303 |
[EditorBrowsable(EditorBrowsableState.Advanced)] |
| 304 |
public static IDisposable SubscribeSafe<T>(this IObservable<T> source, IObserver<T> observer) |
| 305 |
{ |
| 306 |
if (source == null) |
| 307 |
throw new ArgumentNullException("source"); |
| 308 |
if (observer == null) |
| 309 |
throw new ArgumentNullException("observer"); |
| 310 |
|
| 311 |
// |
| 312 |
// The following types are white-listed and should not exhibit exceptional behavior |
| 313 |
// for regular operation circumstances. |
| 314 |
// |
| 315 |
if (source is ObservableBase<T>) |
| 316 |
return source.Subscribe(observer); |
| 317 |
|
| 318 |
#if !NO_PERF |
| 319 |
var producer = source as IProducer<T>; |
| 320 |
if (producer != null) |
| 321 |
return producer.SubscribeRaw(observer, false); |
| 322 |
#endif |
| 323 |
|
| 324 |
var d = Disposable.Empty; |
| 325 |
|
| 326 |
try |
| 327 |
{ |
| 328 |
d = source.Subscribe(observer); |
| 329 |
} |
| 330 |
catch (Exception exception) |
| 331 |
{ |
| 332 |
// |
| 333 |
// The effect of redirecting the exception to the OnError channel is automatic |
| 334 |
// clean-up of query operator state for a large number of cases. For example, |
| 335 |
// consider a binary and temporal query operator with the following Subscribe |
| 336 |
// behavior (implemented using the Producer pattern with a Run method): |
| 337 |
// |
| 338 |
// public IDisposable Run(...) |
| 339 |
// { |
| 340 |
// var tm = _scheduler.Schedule(_due, Tick); |
| 341 |
// |
| 342 |
// var df = _fst.SubscribeSafe(new FstObserver(this, ...)); |
| 343 |
// var ds = _snd.SubscribeSafe(new SndObserver(this, ...)); // <-- fails |
| 344 |
// |
| 345 |
// return new CompositeDisposable(tm, df, ds); |
| 346 |
// } |
| 347 |
// |
| 348 |
// If the second subscription fails, we're not leaving the first subscription |
| 349 |
// or the scheduled job hanging around. Instead, the OnError propagation to |
| 350 |
// the SndObserver should take care of a Dispose call to the observer's parent |
| 351 |
// object. The handshake between Producer and Sink objects will ultimately |
| 352 |
// cause disposal of the CompositeDisposable that's returned from the method. |
| 353 |
// |
| 354 |
observer.OnError(exception); |
| 355 |
} |
| 356 |
|
| 357 |
return d; |
| 358 |
} |
| 359 |
|
| 360 |
#endregion |
| 361 |
} |
| 362 |
} |