2019-12-21 20:52:31 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Common
|
|
|
|
|
{
|
|
|
|
|
public class ReactiveObject<T>
|
|
|
|
|
{
|
|
|
|
|
private ReaderWriterLock _readerWriterLock = new ReaderWriterLock();
|
2020-01-05 17:35:55 +01:00
|
|
|
|
private bool _isInitialized = false;
|
|
|
|
|
private T _value;
|
2019-12-21 20:52:31 +01:00
|
|
|
|
|
|
|
|
|
public event EventHandler<ReactiveEventArgs<T>> Event;
|
|
|
|
|
|
|
|
|
|
public T Value
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
_readerWriterLock.AcquireReaderLock(Timeout.Infinite);
|
|
|
|
|
T value = _value;
|
|
|
|
|
_readerWriterLock.ReleaseReaderLock();
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_readerWriterLock.AcquireWriterLock(Timeout.Infinite);
|
|
|
|
|
|
|
|
|
|
T oldValue = _value;
|
2020-01-05 17:35:55 +01:00
|
|
|
|
|
|
|
|
|
bool oldIsInitialized = _isInitialized;
|
|
|
|
|
|
|
|
|
|
_isInitialized = true;
|
|
|
|
|
_value = value;
|
2019-12-21 20:52:31 +01:00
|
|
|
|
|
|
|
|
|
_readerWriterLock.ReleaseWriterLock();
|
|
|
|
|
|
2022-04-15 12:58:27 +02:00
|
|
|
|
if (!oldIsInitialized || oldValue == null || !oldValue.Equals(_value))
|
2019-12-21 20:52:31 +01:00
|
|
|
|
{
|
|
|
|
|
Event?.Invoke(this, new ReactiveEventArgs<T>(oldValue, value));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static implicit operator T(ReactiveObject<T> obj)
|
|
|
|
|
{
|
|
|
|
|
return obj.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ReactiveEventArgs<T>
|
|
|
|
|
{
|
|
|
|
|
public T OldValue { get; }
|
|
|
|
|
public T NewValue { get; }
|
|
|
|
|
|
|
|
|
|
public ReactiveEventArgs(T oldValue, T newValue)
|
|
|
|
|
{
|
|
|
|
|
OldValue = oldValue;
|
|
|
|
|
NewValue = newValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|