using System;

namespace Rowa.Observables;

public class DelegateObserver<T> : IObserver<T>
{
    public DelegateObserver(Action<T> onNext, Action onComplete = null, Action<Exception> onFailure = null)
    {
        OnNext = onNext ?? throw new ArgumentNullException(nameof(onNext));
        OnComplete = onComplete;
        OnFailure = onFailure;
    }

    public Action<T> OnNext { get; }
    public Action OnComplete { get; }
    public Action<Exception> OnFailure { get; }

    public void OnCompleted()
    {
        OnComplete?.Invoke();
    }

    public void OnError(Exception error)
    {
        OnFailure?.Invoke(error);
    }

    void IObserver<T>.OnNext(T value)
    {
        OnNext(value);
    }
}