mirror of
https://github.com/wabbajack-tools/wabbajack.git
synced 2024-08-30 18:42:17 +00:00
Using Subject<T> for CustomObservable<T>
This commit is contained in:
parent
b5a2354122
commit
85392d1a06
@ -1,12 +1,13 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Reactive;
|
using System.Reactive.Subjects;
|
||||||
|
|
||||||
namespace Wabbajack.Common;
|
namespace Wabbajack.Common;
|
||||||
|
|
||||||
public class CustomObservable<T> : ObservableBase<T>
|
public class CustomObservable<T> : IObservable<T>
|
||||||
{
|
{
|
||||||
private readonly List<IObserver<T>> _observers = new();
|
private readonly Subject<T> _subject = new();
|
||||||
|
private readonly IEqualityComparer<T> _equalityComparer;
|
||||||
|
|
||||||
private T _value;
|
private T _value;
|
||||||
public T Value
|
public T Value
|
||||||
@ -14,46 +15,17 @@ public class CustomObservable<T> : ObservableBase<T>
|
|||||||
get => _value;
|
get => _value;
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (EqualityComparer<T>.Default.Equals(value, _value)) return;
|
if (_equalityComparer.Equals(value, _value)) return;
|
||||||
_value = value;
|
_value = value;
|
||||||
|
_subject.OnNext(value);
|
||||||
foreach (var observer in _observers)
|
|
||||||
{
|
|
||||||
observer.OnNext(value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public CustomObservable(T value)
|
public CustomObservable(T value, IEqualityComparer<T>? equalityComparer = null)
|
||||||
{
|
{
|
||||||
_value = value;
|
_value = value;
|
||||||
|
_equalityComparer = equalityComparer ?? EqualityComparer<T>.Default;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override IDisposable SubscribeCore(IObserver<T> observer)
|
public IDisposable Subscribe(IObserver<T> observer) => _subject.Subscribe(observer);
|
||||||
{
|
|
||||||
if (!_observers.Contains(observer))
|
|
||||||
{
|
|
||||||
_observers.Add(observer);
|
|
||||||
observer.OnNext(Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Unsubscriber<T>(_observers, observer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal sealed class Unsubscriber<T> : IDisposable
|
|
||||||
{
|
|
||||||
private readonly List<IObserver<T>> _observers;
|
|
||||||
private readonly IObserver<T> _observer;
|
|
||||||
|
|
||||||
public Unsubscriber(List<IObserver<T>> observers, IObserver<T> observer)
|
|
||||||
{
|
|
||||||
_observers = observers;
|
|
||||||
_observer = observer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
if (_observers.Contains(_observer)) _observers.Remove(_observer);
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user