Is there a Subject
implementation in Rx.NET that functionally resembles BehaviorSubject
but emits the next value only if it has changed?
I'm rather new to Reactive Extensions and I can't seem to find anything like that, although this pattern feels like a natural replacement for INotifyPropertyChanged
.
My naive implementation is to encapsulate BehaviorSubject<T>
like below. Is there any disadvantages in this, compared to creating a composable observable with Observable.DistinctUntilChanged
?
public class DistinctSubject<T> : SubjectBase<T>
{
private readonly BehaviorSubject<T> _subject;
public DistinctSubject(T initialValue) =>
_subject = new BehaviorSubject<T>(initialValue);
public T Value
{
get => _subject.Value;
set => this.OnNext(value);
}
public override bool HasObservers => _subject.HasObservers;
public override bool IsDisposed => _subject.IsDisposed;
public override void Dispose() => _subject.Dispose();
public override void OnCompleted() => _subject.OnCompleted();
public override void OnError(Exception error) => _subject.OnError(error);
public override void OnNext(T value)
{
if (!EqualityComparer<T>.Default.Equals(value, _subject.Value))
{
_subject.OnNext(value);
}
}
public override IDisposable Subscribe(IObserver<T> observer) =>
_subject.Subscribe(observer);
}