- fix: auth logic part 2

This commit is contained in:
2025-08-07 22:34:25 +03:00
parent 6a6bb4f27c
commit 6c967efd85
17 changed files with 440 additions and 397 deletions

View File

@@ -1,70 +0,0 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Nebula.Shared.Configurations;
public abstract class ComplexConVarBinder<T> : INotifyPropertyChanged, INotifyPropertyChanging
{
private readonly ConVarObserver<T> _baseConVar;
private readonly Lock _lock = new();
private readonly SemaphoreSlim _valueChangeSemaphore = new(1, 1);
public T? Value
{
get
{
lock (_lock)
{
return _baseConVar.Value;
}
}
set
{
_ = SetValueAsync(value);
}
}
protected ComplexConVarBinder(ConVarObserver<T> baseConVar)
{
_baseConVar = baseConVar ?? throw new ArgumentNullException(nameof(baseConVar));
_baseConVar.PropertyChanged += BaseConVarOnPropertyChanged;
_baseConVar.PropertyChanging += BaseConVarOnPropertyChanging;
}
private async Task SetValueAsync(T? value)
{
await _valueChangeSemaphore.WaitAsync().ConfigureAwait(false);
try
{
var newValue = await OnValueChange(value).ConfigureAwait(false);
lock (_lock)
{
_baseConVar.Value = newValue;
}
}
finally
{
_valueChangeSemaphore.Release();
}
}
protected abstract Task<T?> OnValueChange(T? newValue);
private void BaseConVarOnPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value)));
}
private void BaseConVarOnPropertyChanging(object? sender, PropertyChangingEventArgs e)
{
PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(nameof(Value)));
}
public event PropertyChangedEventHandler? PropertyChanged;
public event PropertyChangingEventHandler? PropertyChanging;
}

View File

@@ -12,6 +12,8 @@ public sealed class ConVarObserver<T> : IDisposable, INotifyPropertyChanged, INo
private T? _value;
private ConfigurationService.OnConfigurationChangedDelegate<T> _delegate;
public bool HasValue => Value != null;
public T? Value
{
get => _value;
@@ -31,6 +33,7 @@ public sealed class ConVarObserver<T> : IDisposable, INotifyPropertyChanged, INo
private void OnValueChanged(T? value)
{
OnPropertyChanging(nameof(Value));
OnPropertyChanging(nameof(HasValue));
if(value is null && _value is null)
return;
@@ -39,17 +42,13 @@ public sealed class ConVarObserver<T> : IDisposable, INotifyPropertyChanged, INo
_value = value;
OnPropertyChanged(nameof(Value));
OnPropertyChanged(nameof(HasValue));
}
public void Dispose()
{
_convar.OnValueChanged -= OnValueChanged;
}
public bool HasValue()
{
return Value != null;
}
public static implicit operator T? (ConVarObserver<T> convar) => convar.Value;