| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
namespace System.Reactive |
| 4 |
{ |
| 5 |
/// <summary> |
| 6 |
/// Class to create an IObserver<T> instance from delegate-based implementations of the On* methods. |
| 7 |
/// </summary> |
| 8 |
/// <typeparam name="T">The type of the elements in the sequence.</typeparam> |
| 9 |
public sealed class AnonymousObserver<T> : ObserverBase<T> |
| 10 |
{ |
| 11 |
private readonly Action<T> _onNext; |
| 12 |
private readonly Action<Exception> _onError; |
| 13 |
private readonly Action _onCompleted; |
| 14 |
|
| 15 |
/// <summary> |
| 16 |
/// Creates an observer from the specified OnNext, OnError, and OnCompleted actions. |
| 17 |
/// </summary> |
| 18 |
/// <param name="onNext">Observer's OnNext action implementation.</param> |
| 19 |
/// <param name="onError">Observer's OnError action implementation.</param> |
| 20 |
/// <param name="onCompleted">Observer's OnCompleted action implementation.</param> |
| 21 |
/// <exception cref="ArgumentNullException"><paramref name="onNext"/> or <paramref name="onError"/> or <paramref name="onCompleted"/> is null.</exception> |
| 22 |
public AnonymousObserver(Action<T> onNext, Action<Exception> onError, Action onCompleted) |
| 23 |
{ |
| 24 |
if (onNext == null) |
| 25 |
throw new ArgumentNullException("onNext"); |
| 26 |
if (onError == null) |
| 27 |
throw new ArgumentNullException("onError"); |
| 28 |
if (onCompleted == null) |
| 29 |
throw new ArgumentNullException("onCompleted"); |
| 30 |
|
| 31 |
_onNext = onNext; |
| 32 |
_onError = onError; |
| 33 |
_onCompleted = onCompleted; |
| 34 |
} |
| 35 |
|
| 36 |
/// <summary> |
| 37 |
/// Creates an observer from the specified OnNext action. |
| 38 |
/// </summary> |
| 39 |
/// <param name="onNext">Observer's OnNext action implementation.</param> |
| 40 |
/// <exception cref="ArgumentNullException"><paramref name="onNext"/> is null.</exception> |
| 41 |
public AnonymousObserver(Action<T> onNext) |
| 42 |
: this(onNext, Stubs.Throw, Stubs.Nop) |
| 43 |
{ |
| 44 |
} |
| 45 |
|
| 46 |
/// <summary> |
| 47 |
/// Creates an observer from the specified OnNext and OnError actions. |
| 48 |
/// </summary> |
| 49 |
/// <param name="onNext">Observer's OnNext action implementation.</param> |
| 50 |
/// <param name="onError">Observer's OnError action implementation.</param> |
| 51 |
/// <exception cref="ArgumentNullException"><paramref name="onNext"/> or <paramref name="onError"/> is null.</exception> |
| 52 |
public AnonymousObserver(Action<T> onNext, Action<Exception> onError) |
| 53 |
: this(onNext, onError, Stubs.Nop) |
| 54 |
{ |
| 55 |
} |
| 56 |
|
| 57 |
/// <summary> |
| 58 |
/// Creates an observer from the specified OnNext and OnCompleted actions. |
| 59 |
/// </summary> |
| 60 |
/// <param name="onNext">Observer's OnNext action implementation.</param> |
| 61 |
/// <param name="onCompleted">Observer's OnCompleted action implementation.</param> |
| 62 |
/// <exception cref="ArgumentNullException"><paramref name="onNext"/> or <paramref name="onCompleted"/> is null.</exception> |
| 63 |
public AnonymousObserver(Action<T> onNext, Action onCompleted) |
| 64 |
: this(onNext, Stubs.Throw, onCompleted) |
| 65 |
{ |
| 66 |
} |
| 67 |
|
| 68 |
/// <summary> |
| 69 |
/// Calls the onNext action. |
| 70 |
/// </summary> |
| 71 |
/// <param name="value">Next element in the sequence.</param> |
| 72 |
protected override void OnNextCore(T value) |
| 73 |
{ |
| 74 |
_onNext(value); |
| 75 |
} |
| 76 |
|
| 77 |
/// <summary> |
| 78 |
/// Calls the onError action. |
| 79 |
/// </summary> |
| 80 |
/// <param name="error">The error that has occurred.</param> |
| 81 |
protected override void OnErrorCore(Exception error) |
| 82 |
{ |
| 83 |
_onError(error); |
| 84 |
} |
| 85 |
|
| 86 |
/// <summary> |
| 87 |
/// Calls the onCompleted action. |
| 88 |
/// </summary> |
| 89 |
protected override void OnCompletedCore() |
| 90 |
{ |
| 91 |
_onCompleted(); |
| 92 |
} |
| 93 |
|
| 94 |
internal IObserver<T> MakeSafe(IDisposable disposable) |
| 95 |
{ |
| 96 |
return new AnonymousSafeObserver<T>(_onNext, _onError, _onCompleted, disposable); |
| 97 |
} |
| 98 |
} |
| 99 |
} |