1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-12-22 16:32:02 +00:00
Ryujinx/Ryujinx.Common/ReactiveObject.cs
Thog 40039c5631
Fix ReactiveObject initial event not being propagated with boolean types (#860)
* Fix ReactiveObject initial event not being propagated with boolean types.

This fix the logger configuration initial state being ignored.
2020-01-05 17:35:55 +01:00

61 lines
1.5 KiB
C#

using System;
using System.Threading;
namespace Ryujinx.Common
{
public class ReactiveObject<T>
{
private ReaderWriterLock _readerWriterLock = new ReaderWriterLock();
private bool _isInitialized = false;
private T _value;
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;
bool oldIsInitialized = _isInitialized;
_isInitialized = true;
_value = value;
_readerWriterLock.ReleaseWriterLock();
if (!oldIsInitialized || !oldValue.Equals(_value))
{
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;
}
}
}