Files
ryujinx-ryubing/src/Ryujinx.Common/ReactiveObject.cs
Evan Husted a01a06cd3f 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
2024-10-24 14:14:17 -05:00

58 lines
1.5 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>(T oldValue, T newValue)
{
public T OldValue { get; } = oldValue;
public T NewValue { get; } = newValue;
}
}