Files
SPT-Server-Build/Server/Context/ApplicationContext.cs
T
2025-01-05 02:12:38 +00:00

63 lines
1.6 KiB
C#

using Types.Annotations;
using Types.Context;
namespace Server.Context;
[Injectable(InjectionType.Singleton)]
public class ApplicationContext : IApplicationContext
{
private const short MaxSavedValues = 10;
private Dictionary<ContextVariableType, LinkedList<ContextVariable>> variables = new();
private object variablesLock = new();
public ContextVariable? GetLatestValue(ContextVariableType type)
{
lock (variablesLock)
{
if (variables.TryGetValue(type, out var savedValues))
return savedValues.Last!.Value;
}
return null;
}
public ICollection<ContextVariable> GetValues(ContextVariableType type)
{
lock (variablesLock)
{
var values = new List<ContextVariable>();
if (variables.TryGetValue(type, out var savedValues))
{
values.AddRange(savedValues);
}
return values;
}
}
public void AddValue(ContextVariableType type, object value)
{
lock (variablesLock)
{
if (!variables.TryGetValue(type, out var savedValues))
{
savedValues = [];
variables.Add(type, savedValues);
}
if (savedValues.Count >= MaxSavedValues)
savedValues.RemoveFirst();
savedValues.AddLast(new ContextVariable(value, type));
}
}
public void ClearValues(ContextVariableType type)
{
lock (variablesLock)
{
variables.Remove(type);
}
}
}