// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.
namespace System.Reactive
{
///
/// Class to create an instance from delegate-based implementations of the On* methods.
///
/// The type of the elements in the sequence.
public sealed class AnonymousObserver : ObserverBase
{
private readonly Action _onNext;
private readonly Action _onError;
private readonly Action _onCompleted;
///
/// Creates an observer from the specified , , and actions.
///
/// Observer's action implementation.
/// Observer's action implementation.
/// Observer's action implementation.
/// or or is null.
public AnonymousObserver(Action onNext, Action onError, Action onCompleted)
{
_onNext = onNext ?? throw new ArgumentNullException(nameof(onNext));
_onError = onError ?? throw new ArgumentNullException(nameof(onError));
_onCompleted = onCompleted ?? throw new ArgumentNullException(nameof(onCompleted));
}
///
/// Creates an observer from the specified action.
///
/// Observer's action implementation.
/// is null.
public AnonymousObserver(Action onNext)
: this(onNext, Stubs.Throw, Stubs.Nop)
{
}
///
/// Creates an observer from the specified and actions.
///
/// Observer's action implementation.
/// Observer's action implementation.
/// or is null.
public AnonymousObserver(Action onNext, Action onError)
: this(onNext, onError, Stubs.Nop)
{
}
///
/// Creates an observer from the specified and actions.
///
/// Observer's action implementation.
/// Observer's action implementation.
/// or is null.
public AnonymousObserver(Action onNext, Action onCompleted)
: this(onNext, Stubs.Throw, onCompleted)
{
}
///
/// Calls the action implementing .
///
/// Next element in the sequence.
protected override void OnNextCore(T value) => _onNext(value);
///
/// Calls the action implementing .
///
/// The error that has occurred.
protected override void OnErrorCore(Exception error) => _onError(error);
///
/// Calls the action implementing .
///
protected override void OnCompletedCore() => _onCompleted();
internal ISafeObserver MakeSafe() => new AnonymousSafeObserver(_onNext, _onError, _onCompleted);
}
}