Split out the Flashii interaction code into a separate library.

This commit is contained in:
flash 2025-04-26 19:42:23 +00:00
commit 2eba089a21
Signed by: flash
GPG key ID: 2C9C2C574D47FE3E
55 changed files with 769 additions and 552 deletions
SharpChatCommon/Configuration

View file

@ -0,0 +1,34 @@
namespace SharpChat.Configuration {
public class CachedValue<T>(Config config, string name, TimeSpan lifetime, T? fallback) {
private Config Config { get; } = config ?? throw new ArgumentNullException(nameof(config));
private string Name { get; } = name ?? throw new ArgumentNullException(nameof(name));
private object ConfigAccess { get; } = new();
private object? CurrentValue { get; set; } = default(T);
private DateTimeOffset LastRead { get; set; }
public T? Value {
get {
lock(ConfigAccess) { // this lock doesn't really make sense since it doesn't affect other config calls
DateTimeOffset now = DateTimeOffset.Now;
if((now - LastRead) >= lifetime) {
LastRead = now;
CurrentValue = Config.ReadValue(Name, fallback);
Logger.Debug($"Read {Name} ({CurrentValue})");
}
}
return (T?)CurrentValue;
}
}
public static implicit operator T?(CachedValue<T?> val) => val.Value;
public void Refresh() {
LastRead = DateTimeOffset.MinValue;
}
public override string ToString() {
return Value?.ToString() ?? string.Empty;
}
}
}