2022-01-21 13:41:37 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-01-22 18:44:19 +00:00
|
|
|
|
using System.Reactive.Subjects;
|
2022-01-21 13:41:37 +00:00
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Common;
|
|
|
|
|
|
2022-01-22 18:44:19 +00:00
|
|
|
|
public class CustomObservable<T> : IObservable<T>
|
2022-01-21 13:41:37 +00:00
|
|
|
|
{
|
2022-01-22 18:44:19 +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
|
|
|
|
|
{
|
2022-01-22 18:44:19 +00:00
|
|
|
|
if (_equalityComparer.Equals(value, _value)) return;
|
2022-01-21 13:41:37 +00:00
|
|
|
|
_value = value;
|
2022-01-22 18:44:19 +00:00
|
|
|
|
_subject.OnNext(value);
|
2022-01-21 13:41:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-22 18:44:19 +00:00
|
|
|
|
public CustomObservable(T value, IEqualityComparer<T>? equalityComparer = null)
|
2022-01-21 13:41:37 +00:00
|
|
|
|
{
|
|
|
|
|
_value = value;
|
2022-01-22 18:44:19 +00:00
|
|
|
|
_equalityComparer = equalityComparer ?? EqualityComparer<T>.Default;
|
2022-01-21 13:41:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-22 18:44:19 +00:00
|
|
|
|
public IDisposable Subscribe(IObserver<T> observer) => _subject.Subscribe(observer);
|
2022-01-21 13:41:37 +00:00
|
|
|
|
}
|