2023-12-04 13:17:13 +00:00
|
|
|
using System;
|
2019-12-21 19:52:31 +00:00
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace Ryujinx.Common
|
|
|
|
{
|
|
|
|
public class ReactiveObject<T>
|
|
|
|
{
|
2023-10-12 16:11:15 +00:00
|
|
|
private readonly ReaderWriterLockSlim _readerWriterLock = new();
|
2023-06-28 16:41:38 +00:00
|
|
|
private bool _isInitialized;
|
2020-01-05 16:35:55 +00:00
|
|
|
private T _value;
|
2019-12-21 19:52:31 +00:00
|
|
|
|
|
|
|
public event EventHandler<ReactiveEventArgs<T>> Event;
|
|
|
|
|
|
|
|
public T Value
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2023-10-12 16:11:15 +00:00
|
|
|
_readerWriterLock.EnterReadLock();
|
2019-12-21 19:52:31 +00:00
|
|
|
T value = _value;
|
2023-10-12 16:11:15 +00:00
|
|
|
_readerWriterLock.ExitReadLock();
|
2019-12-21 19:52:31 +00:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
2023-10-12 16:11:15 +00:00
|
|
|
_readerWriterLock.EnterWriteLock();
|
2019-12-21 19:52:31 +00:00
|
|
|
|
|
|
|
T oldValue = _value;
|
2020-01-05 16:35:55 +00:00
|
|
|
|
|
|
|
bool oldIsInitialized = _isInitialized;
|
|
|
|
|
|
|
|
_isInitialized = true;
|
2023-06-28 16:41:38 +00:00
|
|
|
_value = value;
|
2019-12-21 19:52:31 +00:00
|
|
|
|
2023-10-12 16:11:15 +00:00
|
|
|
_readerWriterLock.ExitWriteLock();
|
2019-12-21 19:52:31 +00:00
|
|
|
|
2022-04-15 10:58:27 +00:00
|
|
|
if (!oldIsInitialized || oldValue == null || !oldValue.Equals(_value))
|
2019-12-21 19:52:31 +00: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|