Files
Kenji-NX/src/Ryujinx.Common/ReactiveObject.cs
T
Evan Husted e9dea85064 2 unmerged PRs from original Ryujinx:
Implement shader compile counter (currently not translated, will change, need to pull changes.)
Remove event logic in favor of a single init function.
Thanks @MutantAura
2025-01-01 00:42:48 -06:00

64 lines
1.6 KiB
C#

using System;
using System.Threading;
namespace Ryujinx.Common
{
public class ReactiveObject<T>
{
private readonly ReaderWriterLockSlim _readerWriterLock = new();
private bool _isInitialized;
private T _value;
public event EventHandler<ReactiveEventArgs<T>> Event;
public T Value
{
get
{
_readerWriterLock.EnterReadLock();
T value = _value;
_readerWriterLock.ExitReadLock();
return value;
}
set
{
_readerWriterLock.EnterWriteLock();
T oldValue = _value;
bool oldIsInitialized = _isInitialized;
_isInitialized = true;
_value = value;
_readerWriterLock.ExitWriteLock();
if (!oldIsInitialized || oldValue == null || !oldValue.Equals(_value))
{
Event?.Invoke(this, new ReactiveEventArgs<T>(oldValue, value));
}
}
}
public static implicit operator T(ReactiveObject<T> obj) => obj.Value;
}
public static class ReactiveObjectHelper
{
public static void Toggle(this ReactiveObject<bool> rBoolean) => rBoolean.Value = !rBoolean.Value;
}
public class ReactiveEventArgs<T>
{
public T OldValue { get; }
public T NewValue { get; }
public ReactiveEventArgs(T oldValue, T newValue)
{
OldValue = oldValue;
NewValue = newValue;
}
}
}