using System;

namespace Rowa.Observables;

public class MappedObservable<TIn, TOut> : IObservable<TOut>
{
    private readonly Func<TIn, TOut> _map;
    private readonly IObservable<TIn> _observable;

    public MappedObservable(Func<TIn, TOut> map, IObservable<TIn> observable)
    {
        _map = map;
        _observable = observable;
        
    }

    public IDisposable Subscribe(IObserver<TOut> observer)
    {
        return _observable.Subscribe(new DelegateObserver<TIn>((v) =>
        {
            var mapped = _map(v);
            observer.OnNext(mapped);
        }));
    }
}