using System; using System.Collections.Generic; using System.Reactive; namespace Wabbajack.Common; public class CustomObservable : ObservableBase { private readonly List> _observers = new(); private T _value; public T Value { get => _value; set { if (EqualityComparer.Default.Equals(value, _value)) return; _value = value; foreach (var observer in _observers) { observer.OnNext(value); } } } public CustomObservable(T value) { _value = value; } protected override IDisposable SubscribeCore(IObserver observer) { if (!_observers.Contains(observer)) { _observers.Add(observer); observer.OnNext(Value); } return new Unsubscriber(_observers, observer); } } internal sealed class Unsubscriber : IDisposable { private readonly List> _observers; private readonly IObserver _observer; public Unsubscriber(List> observers, IObserver observer) { _observers = observers; _observer = observer; } public void Dispose() { if (_observers.Contains(_observer)) _observers.Remove(_observer); } }