wabbajack/Wabbajack.Common/CustomObservable.cs

31 lines
804 B
C#
Raw Normal View History

2022-01-21 13:41:37 +00:00
using System;
using System.Collections.Generic;
using System.Reactive.Subjects;
2022-01-21 13:41:37 +00:00
namespace Wabbajack.Common;
public class CustomObservable<T> : IObservable<T>
2022-01-21 13:41:37 +00:00
{
private readonly Subject<T> _subject = new();
private readonly IEqualityComparer<T> _equalityComparer;
2022-01-21 13:41:37 +00:00
private T _value;
public T Value
{
get => _value;
set
{
if (_equalityComparer.Equals(value, _value)) return;
2022-01-21 13:41:37 +00:00
_value = value;
_subject.OnNext(value);
2022-01-21 13:41:37 +00:00
}
}
public CustomObservable(T value, IEqualityComparer<T>? equalityComparer = null)
2022-01-21 13:41:37 +00:00
{
_value = value;
_equalityComparer = equalityComparer ?? EqualityComparer<T>.Default;
2022-01-21 13:41:37 +00:00
}
public IDisposable Subscribe(IObserver<T> observer) => _subject.Subscribe(observer);
2022-01-21 13:41:37 +00:00
}